-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing pluggable element locator factories for .NET PageFactory
This change allows the user to specify a custom IElementLocatorFactory for locating elements when used with the PageFactory. This gives much more control over the algorithm used to locate elements, and allows the incorporation of things like retries or handling of specific exceptions.
- Loading branch information
Showing
9 changed files
with
379 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
dotnet/src/support/PageObjects/DefaultElementLocatorFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// <copyright file="DefaultElementLocatorFactory.cs" company="WebDriver Committers"> | ||
// Copyright 2014 Software Freedom Conservancy | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace OpenQA.Selenium.Support.PageObjects | ||
{ | ||
/// <summary> | ||
/// A default locator for elements for use with the <see cref="PageFactory"/>. This locator | ||
/// implements no retry logic for elements not being found, nor for elements being stale. | ||
/// </summary> | ||
public class DefaultElementLocatorFactory : IElementLocatorFactory | ||
{ | ||
/// <summary> | ||
/// Locates an element using the given <see cref="ISearchContext"/> and list of <see cref="By"/> criteria. | ||
/// </summary> | ||
/// <param name="searchContext">The <see cref="ISearchContext"/> object within which to search for an element.</param> | ||
/// <param name="bys">The list of methods by which to search for the element.</param> | ||
/// <returns>An <see cref="IWebElement"/> which is the first match under the desired criteria.</returns> | ||
public IWebElement LocateElement(ISearchContext searchContext, IEnumerable<By> bys) | ||
{ | ||
if (searchContext == null) | ||
{ | ||
throw new ArgumentNullException("searchContext", "searchContext may not be null"); | ||
} | ||
|
||
if (bys == null) | ||
{ | ||
throw new ArgumentNullException("bys", "List of criteria may not be null"); | ||
} | ||
|
||
string errorString = null; | ||
foreach (var by in bys) | ||
{ | ||
try | ||
{ | ||
return searchContext.FindElement(by); | ||
} | ||
catch (NoSuchElementException) | ||
{ | ||
errorString = (errorString == null ? "Could not find element by: " : errorString + ", or: ") + by; | ||
} | ||
} | ||
|
||
throw new NoSuchElementException(errorString); | ||
} | ||
|
||
/// <summary> | ||
/// Locates a list of elements using the given <see cref="ISearchContext"/> and list of <see cref="By"/> criteria. | ||
/// </summary> | ||
/// <param name="searchContext">The <see cref="ISearchContext"/> object within which to search for elements.</param> | ||
/// <param name="bys">The list of methods by which to search for the elements.</param> | ||
/// <returns>An list of all elements which match the desired criteria.</returns> | ||
public ReadOnlyCollection<IWebElement> LocateElements(ISearchContext searchContext, IEnumerable<By> bys) | ||
{ | ||
if (searchContext == null) | ||
{ | ||
throw new ArgumentNullException("searchContext", "searchContext may not be null"); | ||
} | ||
|
||
if (bys == null) | ||
{ | ||
throw new ArgumentNullException("bys", "List of criteria may not be null"); | ||
} | ||
|
||
List<IWebElement> collection = new List<IWebElement>(); | ||
foreach (var by in bys) | ||
{ | ||
ReadOnlyCollection<IWebElement> list = searchContext.FindElements(by); | ||
collection.AddRange(list); | ||
} | ||
|
||
return collection.AsReadOnly(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// <copyright file="IElementLocatorFactory.cs" company="WebDriver Committers"> | ||
// Copyright 2014 Software Freedom Conservancy | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace OpenQA.Selenium.Support.PageObjects | ||
{ | ||
/// <summary> | ||
/// Interface describing how elements are to be located by a <see cref="PageFactory"/> | ||
/// </summary> | ||
public interface IElementLocatorFactory | ||
{ | ||
/// <summary> | ||
/// Locates an element using the given <see cref="ISearchContext"/> and list of <see cref="By"/> criteria. | ||
/// </summary> | ||
/// <param name="searchContext">The <see cref="ISearchContext"/> object within which to search for an element.</param> | ||
/// <param name="bys">The list of methods by which to search for the element.</param> | ||
/// <returns>An <see cref="IWebElement"/> which is the first match under the desired criteria.</returns> | ||
IWebElement LocateElement(ISearchContext searchContext, IEnumerable<By> bys); | ||
|
||
/// <summary> | ||
/// Locates a list of elements using the given <see cref="ISearchContext"/> and list of <see cref="By"/> criteria. | ||
/// </summary> | ||
/// <param name="searchContext">The <see cref="ISearchContext"/> object within which to search for elements.</param> | ||
/// <param name="bys">The list of methods by which to search for the elements.</param> | ||
/// <returns>An list of all elements which match the desired criteria.</returns> | ||
ReadOnlyCollection<IWebElement> LocateElements(ISearchContext searchContext, IEnumerable<By> bys); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.