-
Notifications
You must be signed in to change notification settings - Fork 15
Add IElementFinder implementation #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e8fa12
Add IElementFinder to resolve #23
mialeska 8042041
Fix review comments in ElementFinder
mialeska 7502305
inline variable in ElementFinder
mialeska ffe97bd
add return documentation into IElementFinder
mialeska 52ec7ad
Feature/implement conditional wait (#26)
knysh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
Aquality.Selenium/src/Aquality.Selenium/Elements/ElementFinder.cs
This file contains hidden or 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,99 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.ObjectModel; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using Aquality.Selenium.Browsers; | ||
| using Aquality.Selenium.Configurations; | ||
| using Aquality.Selenium.Elements.Interfaces; | ||
| using Aquality.Selenium.Waitings; | ||
| using OpenQA.Selenium; | ||
|
|
||
| namespace Aquality.Selenium.Elements | ||
| { | ||
| internal sealed class ElementFinder : IElementFinder | ||
| { | ||
| private static readonly ThreadLocal<ElementFinder> InstanceHolder = new ThreadLocal<ElementFinder>(); | ||
|
|
||
| private ElementFinder() | ||
| { | ||
| } | ||
|
|
||
| public static ElementFinder Instance | ||
| { | ||
| get | ||
| { | ||
| if (!InstanceHolder.IsValueCreated) | ||
| { | ||
| InstanceHolder.Value = new ElementFinder(); | ||
| } | ||
|
|
||
| return InstanceHolder.Value; | ||
| } | ||
| } | ||
|
|
||
| private ITimeoutConfiguration TimeoutConfiguration => Configuration.Instance.TimeoutConfiguration; | ||
|
|
||
| private TimeSpan DefaultTimeout => TimeoutConfiguration.Condition; | ||
|
|
||
| private Browser Browser => BrowserManager.Browser; | ||
|
|
||
| public IWebElement FindElement(By locator, TimeSpan? timeout = null, ElementState state = ElementState.ExistsInAnyState) | ||
| { | ||
| var clearTimeout = timeout ?? DefaultTimeout; | ||
| var elements = FindElements(locator, clearTimeout, state); | ||
| if (elements.Any()) | ||
| { | ||
| return elements.First(); | ||
| } | ||
|
|
||
| throw new NoSuchElementException($"Element was not found in {clearTimeout.Seconds} seconds in state {state} by locator {locator}"); | ||
| } | ||
|
|
||
| public IWebElement FindElement(By by) | ||
| { | ||
| return FindElement(by, timeout: null); | ||
| } | ||
|
|
||
| public ReadOnlyCollection<IWebElement> FindElements(By locator, TimeSpan? timeout = null, ElementState state = ElementState.ExistsInAnyState) | ||
| { | ||
| var resultElements = new List<IWebElement>(); | ||
| Browser.ImplicitWaitTimeout = TimeSpan.Zero; | ||
| ConditionalWait.WaitForTrue(driver => | ||
| { | ||
| var foundElements = driver.FindElements(locator); | ||
| var filteredElements = FilterByState(foundElements, state); | ||
| resultElements.AddRange(filteredElements); | ||
| return filteredElements.Any(); | ||
| }, timeout ?? DefaultTimeout); | ||
| Browser.ImplicitWaitTimeout = TimeoutConfiguration.Implicit; | ||
| return resultElements.ToList().AsReadOnly(); | ||
| } | ||
|
|
||
| private IList<IWebElement> FilterByState(IList<IWebElement> foundElements, ElementState state) | ||
| { | ||
| var filteredElements = new List<IWebElement>(); | ||
| if (foundElements.Any()) | ||
| { | ||
| switch (state) | ||
| { | ||
| case ElementState.Displayed: | ||
| filteredElements.AddRange(foundElements.Where(element => element.Displayed)); | ||
| break; | ||
| case ElementState.ExistsInAnyState: | ||
| filteredElements.AddRange(foundElements); | ||
| break; | ||
| default: | ||
| throw new InvalidOperationException($"{state} state is not recognized"); | ||
| } | ||
| } | ||
|
|
||
| return filteredElements; | ||
| } | ||
|
|
||
| public ReadOnlyCollection<IWebElement> FindElements(By by) | ||
| { | ||
| return FindElements(by, timeout: null); | ||
| } | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
Aquality.Selenium/src/Aquality.Selenium/Elements/Interfaces/IElementFinder.cs
This file contains hidden or 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,31 @@ | ||
| using OpenQA.Selenium; | ||
| using System; | ||
| using System.Collections.ObjectModel; | ||
|
|
||
| namespace Aquality.Selenium.Elements.Interfaces | ||
| { | ||
| /// <summary> | ||
| /// Provides ability to find elements in desired ElementState | ||
| /// </summary> | ||
| public interface IElementFinder : ISearchContext | ||
| { | ||
| /// <summary> | ||
| /// Finds elements in desired ElementState | ||
| /// </summary> | ||
| /// <param name="locator">elements locator</param> | ||
| /// <param name="timeout">timeout for search</param> | ||
| /// <param name="state">desired ElementState</param> | ||
| /// <returns>List of found elements</returns> | ||
| ReadOnlyCollection<IWebElement> FindElements(By locator, TimeSpan? timeout = null, ElementState state = ElementState.ExistsInAnyState); | ||
|
|
||
| /// <summary> | ||
| /// Finds element in desired ElementState | ||
| /// </summary> | ||
| /// <param name="locator">elements locator</param> | ||
| /// <param name="timeout">timeout for search</param> | ||
| /// <param name="state">desired ElementState</param> | ||
| /// <exception cref="OpenQA.Selenium.NoSuchElementException">Thrown if element was not found in time in desired state</exception> | ||
| /// <returns></returns> | ||
| IWebElement FindElement(By locator, TimeSpan? timeout = null, ElementState state = ElementState.ExistsInAnyState); | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.