From f1025fb9ab59857eebe69b848039503315cf1645 Mon Sep 17 00:00:00 2001 From: Aliaksej Mialeshka Date: Thu, 25 Jan 2024 18:41:48 +0100 Subject: [PATCH] Update ElementFactory to use generate CSS locator logic in generateLocator method instead of generateXPathLocator --- pom.xml | 2 +- .../selenium/elements/ElementFactory.java | 34 +++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index a8d9318..ac2724c 100644 --- a/pom.xml +++ b/pom.xml @@ -82,7 +82,7 @@ com.github.aquality-automation aquality-selenium-core - 3.1.1 + 3.1.2 org.apache.commons diff --git a/src/main/java/aquality/selenium/elements/ElementFactory.java b/src/main/java/aquality/selenium/elements/ElementFactory.java index d7f3cb6..ed99699 100644 --- a/src/main/java/aquality/selenium/elements/ElementFactory.java +++ b/src/main/java/aquality/selenium/elements/ElementFactory.java @@ -61,23 +61,35 @@ protected Map ((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. *