Skip to content

Commit

Permalink
Update ElementFactory to use generate CSS locator logic in generateLo…
Browse files Browse the repository at this point in the history
…cator method instead of generateXPathLocator
  • Loading branch information
mialeska committed Jan 25, 2024
1 parent 57318e3 commit f1025fb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<dependency>
<groupId>com.github.aquality-automation</groupId>
<artifactId>aquality-selenium-core</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
34 changes: 23 additions & 11 deletions src/main/java/aquality/selenium/elements/ElementFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,35 @@ protected Map<Class<? extends aquality.selenium.core.elements.interfaces.IElemen
* @return target element's locator
*/
@Override
protected By generateXpathLocator(By multipleElementsLocator, WebElement webElement, int elementIndex) {
protected By generateLocator(By multipleElementsLocator, WebElement webElement, int elementIndex) {
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 generateXpathLocator(multipleElementsLocator, webElement, elementIndex);
} 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."));
}
}

/**
* Generates xpath locator for target element.
*
* @param multipleElementsLocator locator used to find elements.
* @param webElement target element.
* @param elementIndex index of target element.
* @return target element's locator
*/
@Override
protected By generateXpathLocator(By multipleElementsLocator, WebElement webElement, int elementIndex) {
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"));
}

/**
* Defines is the locator can be transformed to xpath or not.
*
Expand Down

0 comments on commit f1025fb

Please sign in to comment.