-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Finding of Multiple elements from ShadowRoot (#236)
* Support Finding of Multiple elements from ShadowRoot - add JavaScript to generate CSS selector from element - try to generate CSS selector if XPath generation fails - necessary for ShadowRoot elements since * Replace GetElementCssSelector for old browsers compatibility (like InternetExplorer) * refactor JavaScript function to generate CSS selector from element * Refactoring, Introduce IShadowRootExpander interface to reduce duplication * move common code to IShadowRootExpander extensions to reduce duplications * Fix locator generation in case when generated XPath was invalid (e.g. when use FindElements with By.Id or By.ClassName locator) * Update ElementFactory to use Generate CSS locator logic in GenerateLocator method instead of GenerateXPathLocator
- Loading branch information
Showing
11 changed files
with
236 additions
and
89 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
90 changes: 59 additions & 31 deletions
90
Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
71 changes: 71 additions & 0 deletions
71
Aquality.Selenium/src/Aquality.Selenium/Elements/Interfaces/IShadowRootExpander.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,71 @@ | ||
using Aquality.Selenium.Browsers; | ||
using Aquality.Selenium.Core.Elements; | ||
using Aquality.Selenium.Core.Localization; | ||
using OpenQA.Selenium; | ||
using System.Collections.Generic; | ||
|
||
namespace Aquality.Selenium.Elements.Interfaces | ||
{ | ||
/// <summary> | ||
/// Shadow Root expander. | ||
/// </summary> | ||
public interface IShadowRootExpander | ||
{ | ||
/// <summary> | ||
/// Expands shadow root. | ||
/// </summary> | ||
/// <returns>ShadowRoot search context.</returns> | ||
ShadowRoot ExpandShadowRoot(); | ||
} | ||
|
||
/// <summary> | ||
/// Extensions for Shadow Root expander (like element or JS Actions). | ||
/// </summary> | ||
public static class ShadowRootExpanderExtensions | ||
{ | ||
/// <summary> | ||
/// Provides <see cref="IElementFactory"/> to find elements in the shadow root of the current element. | ||
/// </summary> | ||
public static IElementFactory GetShadowRootElementFactory(this IShadowRootExpander shadowRootExpander) | ||
{ | ||
var shadowRootRelativeFinder = new RelativeElementFinder(AqualityServices.LocalizedLogger, AqualityServices.ConditionalWait, shadowRootExpander.ExpandShadowRoot); | ||
return new ElementFactory(AqualityServices.ConditionalWait, shadowRootRelativeFinder, AqualityServices.Get<ILocalizationManager>()); | ||
} | ||
|
||
/// <summary> | ||
/// Finds element in the shadow root of the current element. | ||
/// </summary> | ||
/// <typeparam name="T">Type of the target element that has to implement <see cref="IElement"/>.</typeparam> | ||
/// <param name="shadowRootExpander">Current instance of the Shadow root expander.</param> | ||
/// <param name="locator">Locator of the target element. | ||
/// Note that some browsers don't support XPath locator for shadow elements (e.g. Chrome).</param> | ||
/// <param name="name">Name of the target element.</param> | ||
/// <param name="supplier">Delegate that defines constructor of element.</param> | ||
/// <param name="state">State of the target element.</param> | ||
/// <returns>Instance of element.</returns> | ||
public static T FindElementInShadowRoot<T>(this IShadowRootExpander shadowRootExpander, By locator, string name, ElementSupplier<T> supplier = null, ElementState state = ElementState.Displayed) | ||
where T : IElement | ||
{ | ||
return shadowRootExpander.GetShadowRootElementFactory().Get(locator, name, supplier, state); | ||
} | ||
|
||
/// <summary> | ||
/// Finds elements in the shadow root of the current element. | ||
/// </summary> | ||
/// <typeparam name="T">Type of the target elements that has to implement <see cref="IElement"/>.</typeparam> | ||
/// <param name="shadowRootExpander">Current instance of the Shadow root expander.</param> | ||
/// <param name="locator">Locator of target elements. | ||
/// Note that some browsers don't support XPath locator for shadow elements. | ||
/// Therefore, we suggest to use CSS selectors</param> | ||
/// <param name="name">Name of target elements.</param> | ||
/// <param name="supplier">Delegate that defines constructor of element.</param> | ||
/// <param name="expectedCount">Expected number of elements that have to be found (zero, more then zero, any).</param> | ||
/// <param name="state">State of target elements.</param> | ||
/// <returns>List of found elements.</returns> | ||
public static IList<T> FindElementsInShadowRoot<T>(this IShadowRootExpander shadowRootExpander, By locator, string name = null, ElementSupplier<T> supplier = null, ElementsCount expectedCount = ElementsCount.Any, ElementState state = ElementState.Displayed) | ||
where T : IElement | ||
{ | ||
return shadowRootExpander.GetShadowRootElementFactory().FindElements(locator, name, supplier, expectedCount, state); | ||
} | ||
} | ||
} |
Oops, something went wrong.