Skip to content
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

Support Finding of Multiple elements from ShadowRoot #125

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/aquality/selenium/browser/JavaScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum JavaScript {
GET_COMBOBOX_SELECTED_TEXT("getCmbText.js"),
GET_COMBOBOX_TEXTS("getCmbValues.js"),
GET_ELEMENT_BY_XPATH("getElementByXpath.js"),
GET_ELEMENT_CSS_SELECTOR("getElementCssSelector.js"),
GET_ELEMENT_XPATH("getElementXPath.js"),
GET_ELEMENT_TEXT("getElementText.js"),
GET_TEXT_FIRST_CHILD("getTextFirstChild.js"),
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/aquality/selenium/elements/ElementFactory.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package aquality.selenium.elements;

import aquality.selenium.browser.AqualityServices;
import aquality.selenium.browser.JavaScript;
import aquality.selenium.core.elements.interfaces.IElementFinder;
import aquality.selenium.core.elements.interfaces.IElementSupplier;
import aquality.selenium.core.localization.ILocalizationManager;
import aquality.selenium.core.waitings.IConditionalWait;
import aquality.selenium.elements.interfaces.*;
import com.google.inject.Inject;
import org.openqa.selenium.By;
import org.openqa.selenium.*;
import org.openqa.selenium.By.ByClassName;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.By.ByName;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ByIdOrName;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class ElementFactory extends aquality.selenium.core.elements.ElementFactory implements IElementFactory {

private final IConditionalWait conditionalWait;
private final IElementFinder elementFinder;

@Inject
public ElementFactory(IConditionalWait conditionalWait, IElementFinder elementFinder, ILocalizationManager localizationManager) {
super(conditionalWait, elementFinder, localizationManager);
this.conditionalWait = conditionalWait;
this.elementFinder = elementFinder;
}

Expand Down Expand Up @@ -60,9 +62,20 @@ protected Map<Class<? extends aquality.selenium.core.elements.interfaces.IElemen
*/
@Override
protected By generateXpathLocator(By multipleElementsLocator, WebElement webElement, int elementIndex) {
mialeska marked this conversation as resolved.
Show resolved Hide resolved
return isLocatorSupportedForXPathExtraction(multipleElementsLocator)
? super.generateXpathLocator(multipleElementsLocator, webElement, elementIndex)
: By.xpath((String) AqualityServices.getBrowser().executeScript(JavaScript.GET_ELEMENT_XPATH, webElement));
try {
if (isLocatorSupportedForXPathExtraction(multipleElementsLocator)) {
By locator = super.generateXpathLocator(multipleElementsLocator, webElement, elementIndex);
if (elementFinder.findElements(locator).size() == 1) {
return locator;
}
}
return By.xpath((String) conditionalWait.waitFor(driver -> ((RemoteWebDriver) Objects.requireNonNull(driver))
.executeScript(JavaScript.GET_ELEMENT_XPATH.getScript(), webElement), "XPath generation failed"));
}
catch (InvalidArgumentException | JavascriptException ex) {
return By.cssSelector((String) conditionalWait.waitFor(driver -> ((RemoteWebDriver) Objects.requireNonNull(driver))
.executeScript(JavaScript.GET_ELEMENT_CSS_SELECTOR.getScript(), webElement), ex.getMessage() + ". CSS selector generation failed too."));
}
}

/**
Expand Down
40 changes: 40 additions & 0 deletions src/main/resources/js/getElementCssSelector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function previousElementSibling (element) {
if (element.previousElementSibling !== 'undefined') {
return element.previousElementSibling;
} else {
// Loop through ignoring anything not an element
while (element = element.previousSibling) {
if (element.nodeType === 1) {
return element;
}
}
}
}
function getCssPath (element) {
// Empty on non-elements
if (!(element instanceof HTMLElement)) { return ''; }
let path = [];
while (element.nodeType === Node.ELEMENT_NODE) {
let selector = element.nodeName;
if (element.id) { selector += ('#' + element.id); }
else {
// Walk backwards until there is no previous sibling
let sibling = element;
// Will hold nodeName to join for adjacent selection
let siblingSelectors = [];
while (sibling !== null && sibling.nodeType === Node.ELEMENT_NODE) {
siblingSelectors.unshift(sibling.nodeName);
sibling = previousElementSibling(sibling);
}
// :first-child does not apply to HTML
if (siblingSelectors[0] !== 'HTML') {
siblingSelectors[0] = siblingSelectors[0] + ':first-child';
}
selector = siblingSelectors.join(' + ');
}
path.unshift(selector);
element = element.parentNode;
}
return path.join(' > ');
}
return getCssPath(arguments[0]);
26 changes: 24 additions & 2 deletions src/test/java/forms/ChromeDownloadsForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;

import java.util.List;

public class ChromeDownloadsForm extends Form {
private static final String ADDRESS = "chrome://downloads/";
public static final By NESTED_SHADOW_ROOT_LOCATOR = By.id("moreActionsMenu");
public static final By DIV_ELEMENTS_LOCATOR = By.cssSelector("div");

private final ILabel lblDownloadsToolbar = getFormLabel().findElementInShadowRoot(By.cssSelector("downloads-toolbar"), "Downloads toolbar", ILabel.class);
private final ILabel lblMainContainer = getFormLabel().findElementInShadowRoot(By.id("mainContainer"), "Main container", ILabel.class);

private final ILabel lblDownloadsToolbarFromJs = getFormLabel().getJsActions().findElementInShadowRoot(By.cssSelector("downloads-toolbar"), "Downloads toolbar", ILabel.class);
private final ILabel lblMainContainerFromJs = getFormLabel().getJsActions().findElementInShadowRoot(By.id("mainContainer"), "Main container", ILabel.class);
Expand All @@ -31,11 +37,11 @@ public SearchContext expandShadowRootViaJs() {
}

public ILabel getDownloadsToolbarLabel() {
return getFormLabel().findElementInShadowRoot(By.cssSelector("downloads-toolbar"), "Downloads toolbar", ILabel.class);
return lblDownloadsToolbar;
}

public ILabel getMainContainerLabel() {
return getFormLabel().findElementInShadowRoot(By.id("mainContainer"), "main container", ILabel.class);
return lblMainContainer;
}

public ILabel getDownloadsToolbarLabelFromJs() {
Expand All @@ -45,4 +51,20 @@ public ILabel getDownloadsToolbarLabelFromJs() {
public ILabel getMainContainerLabelFromJs() {
return lblMainContainerFromJs;
}

public List<ILabel> getDivElementLabels() {
return getFormLabel().findElementsInShadowRoot(DIV_ELEMENTS_LOCATOR, "div", ILabel.class);
}

public List<ILabel> getDivElementLabelsFromJs() {
return getFormLabel().getJsActions().findElementsInShadowRoot(DIV_ELEMENTS_LOCATOR, "div", ILabel.class);
}

public List<ILabel> getMainContainerLabels() {
return getFormLabel().findElementsInShadowRoot(lblMainContainer.getLocator(), lblMainContainer.getName(), ILabel.class);
}

public List<ILabel> getMainContainerLabelsFromJs() {
return getFormLabel().getJsActions().findElementsInShadowRoot(lblMainContainer.getLocator(), lblMainContainer.getName(), ILabel.class);
}
}
21 changes: 21 additions & 0 deletions src/test/java/tests/usecases/ShadowRootTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import aquality.selenium.elements.interfaces.ILabel;
import forms.ChromeDownloadsForm;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import tests.BaseTest;

import java.util.List;

public class ShadowRootTests extends BaseTest {
private static final ChromeDownloadsForm form = new ChromeDownloadsForm();

Expand All @@ -19,16 +22,34 @@ public void beforeMethod() {
@Test
public void testExpandShadowRoot() {
Assert.assertNotNull(form.expandShadowRoot(), "Should be possible to expand shadow root and get Selenium native ShadowRoot object");
}

@Test
public void testFindElementInShadowRoot() {
Assert.assertNotNull(form.getDownloadsToolbarLabel().getElement(), "Should be possible do get the element hidden under the shadow");
Assert.assertNotNull(form.getDownloadsToolbarLabel().findElementInShadowRoot(ChromeDownloadsForm.NESTED_SHADOW_ROOT_LOCATOR, "More actions menu", ILabel.class).getElement(),
"Should be possible to expand the nested shadow root and get the element from it");
Assert.assertTrue(form.getMainContainerLabel().state().isDisplayed(), "Should be possible to check that element under the shadow is displayed");
}

@Test
public void testFindElementsInShadowRoot() {
List<ILabel> elementLabels = form.getDivElementLabels();
Assert.assertTrue(elementLabels.size() > 1, "Should be possible to find multiple elements hidden under the shadow");
Assert.assertTrue(elementLabels.get(0).getLocator() instanceof By.ByCssSelector, "Unique locator of correct type should be generated");
Assert.assertEquals(elementLabels.get(0).getElement().getTagName(), "div", "Should be possible to work with one of found elements");
Assert.assertEquals(form.getMainContainerLabels().get(0).getElement().getTagName(), "div", "Should be possible to work with one of found elements found by id");
}

@Test
public void testExpandShadowRootViaJs() {
Assert.assertNotNull(form.expandShadowRootViaJs(), "Should be possible to expand shadow root and get Selenium native ShadowRoot object");
Assert.assertNotNull(form.getDownloadsToolbarLabelFromJs().getElement(), "Should be possible do get the element hidden under the shadow");
List<ILabel> elementLabels = form.getDivElementLabelsFromJs();
Assert.assertTrue(elementLabels.size() > 1, "Should be possible to find multiple elements hidden under the shadow");
Assert.assertTrue(elementLabels.get(0).getLocator() instanceof By.ByCssSelector, "Unique locator of correct type should be generated");
Assert.assertEquals(elementLabels.get(0).getElement().getTagName(), "div", "Should be possible to work with one of found elements");
Assert.assertEquals(form.getMainContainerLabelsFromJs().get(0).getElement().getTagName(), "div", "Should be possible to work with one of found elements found by id");
Assert.assertNotNull(form.getDownloadsToolbarLabelFromJs().findElementInShadowRoot(ChromeDownloadsForm.NESTED_SHADOW_ROOT_LOCATOR, "More actions menu", ILabel.class).getElement(),
"Should be possible to expand the nested shadow root and get the element from it");
Assert.assertTrue(form.getMainContainerLabelFromJs().state().isDisplayed(), "Should be possible to check that element under the shadow is displayed");
Expand Down