From c113cc3f10f72013a66c2eb5a52ab8460da196f9 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Wed, 22 Jan 2020 16:17:07 +0000 Subject: Fix xpath selectors and format strings in ui-ci Change-Id: I70209d0c87dcb4277cb1355010a881553704b3f2 Issue-ID: SDC-2731 Signed-off-by: andre.schmid --- .../sdc/ci/tests/utilities/GeneralUIUtils.java | 128 ++++++++++----------- 1 file changed, 61 insertions(+), 67 deletions(-) diff --git a/ui-ci/src/main/java/org/openecomp/sdc/ci/tests/utilities/GeneralUIUtils.java b/ui-ci/src/main/java/org/openecomp/sdc/ci/tests/utilities/GeneralUIUtils.java index d4766c9bba..57112dc594 100644 --- a/ui-ci/src/main/java/org/openecomp/sdc/ci/tests/utilities/GeneralUIUtils.java +++ b/ui-ci/src/main/java/org/openecomp/sdc/ci/tests/utilities/GeneralUIUtils.java @@ -20,7 +20,21 @@ package org.openecomp.sdc.ci.tests.utilities; +import static org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest.getExtendTest; +import static org.testng.AssertJUnit.assertTrue; + import com.aventstack.extentreports.Status; +import java.awt.Toolkit; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.StringSelection; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; import org.apache.commons.io.FileUtils; import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum; import org.openecomp.sdc.ci.tests.exception.GeneralUiRuntimeException; @@ -44,26 +58,14 @@ import org.openqa.selenium.support.ui.WebDriverWait; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.awt.*; -import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.StringSelection; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; -import java.util.concurrent.TimeUnit; -import java.util.function.Supplier; - -import static org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest.getExtendTest; -import static org.testng.AssertJUnit.assertTrue; - public final class GeneralUIUtils { private static final Logger LOGGER = LoggerFactory.getLogger(GeneralUIUtils.class); - private static final String DATA_TESTS_ID = "//*[@data-tests-id='"; + private static final String TEST_ID_XPATH = "//*[@data-tests-id='%s']"; + private static final String TEST_ID_CHILD_XPATH = "//*[@data-tests-id='%s']//*"; + private static final String TEST_ID_ATTRIBUTE_NAME = "data-tests-id"; private static final String COLOR_YELLOW_BORDER_4PX_SOLID_YELLOW = "color: yellow; border: 4px solid yellow;"; private static final int TIME_OUT = (int) (60 * 1.5); @@ -120,16 +122,17 @@ public final class GeneralUIUtils { public static WebElement getWebElementByTestID(final String dataTestId, final int timeout) { try { final WebDriverWait wait = new WebDriverWait(getDriver(), timeout); - return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']")))); + return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(String.format(TEST_ID_XPATH, dataTestId)))); } - catch (Exception e) { - System.out.println("Element with dataTestId name: "+dataTestId+" not found"); - return null; + catch (final Exception e) { + LOGGER + .debug(String.format("Element with attribute %s=%s not found", TEST_ID_ATTRIBUTE_NAME, dataTestId), e); } + return null; } public static boolean isWebElementExistByTestId(String dataTestId) { - return getDriver().findElements(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']"))).size() != 0; + return getDriver().findElements(By.xpath(String.format(TEST_ID_XPATH, dataTestId))).size() != 0; } public static boolean isWebElementExistByClass(String className) { @@ -139,7 +142,7 @@ public final class GeneralUIUtils { public static WebElement getInputElement(String dataTestId) { try { ultimateWait(); - return getDriver().findElement(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']"))); + return getDriver().findElement(By.xpath(String.format(TEST_ID_XPATH, dataTestId))); } catch (Exception e) { return null; } @@ -147,7 +150,7 @@ public final class GeneralUIUtils { public static List getInputElements(String dataTestId) { ultimateWait(); - return getDriver().findElements(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']"))); + return getDriver().findElements(By.xpath(String.format(TEST_ID_XPATH, dataTestId))); } @@ -182,22 +185,27 @@ public final class GeneralUIUtils { return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by)); } - public static List getWebElementsListByContainTestID(String dataTestId) { + public static List getWebElementsListByContainTestID(final String dataTestId) { try { - WebDriverWait wait = new WebDriverWait(getDriver(), WEB_DRIVER_WAIT_TIME_OUT); - return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(String.format("//*[contains(@data-tests-id, '", dataTestId, "')]")))); - } catch (Exception e) { - return new ArrayList(); + final WebDriverWait wait = new WebDriverWait(getDriver(), WEB_DRIVER_WAIT_TIME_OUT); + final String xpath = String.format("//*[contains(@%s, '%s')]", TEST_ID_ATTRIBUTE_NAME, dataTestId); + return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(xpath))); + } catch (final Exception e) { + final String message = String.format("Could not find element containing the attribute '%s' as '%s'", + TEST_ID_ATTRIBUTE_NAME, dataTestId); + LOGGER.debug(message, e); + return Collections.emptyList(); } } - public static List getWebElementsListByContainsClassName(String containedText) { - WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT); - return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[contains(@class, '" + containedText + "')]"))); + public static List getWebElementsListByContainsClassName(final String containedText) { + final WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT); + return wait.until(ExpectedConditions. + presenceOfAllElementsLocatedBy(By.xpath(String.format("//*[contains(@class, '%s')]", containedText)))); } - public static WebElement getWebElementByContainsClassName(String containedText) { - return getWebElementBy(By.xpath("//*[contains(@class, '" + containedText + "')]")); + public static WebElement getWebElementByContainsClassName(final String containedText) { + return getWebElementBy(By.xpath(String.format("//*[contains(@class, '%s')]", containedText))); } public static WebElement getWebElementByClassName(String className) { @@ -207,7 +215,7 @@ public final class GeneralUIUtils { public static List getWebElementsListByTestID(String dataTestId) { WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT); - return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']")))); + return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(String.format(TEST_ID_XPATH, dataTestId)))); } public static List getWebElementsListByClassName(String className) { @@ -219,13 +227,13 @@ public final class GeneralUIUtils { public static Boolean isElementInvisibleByTestId(String dataTestId) { WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT); return wait.until( - ExpectedConditions.invisibilityOfElementLocated(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']")))); + ExpectedConditions.invisibilityOfElementLocated(By.xpath(String.format(TEST_ID_XPATH, dataTestId)))); } public static Boolean isElementVisibleByTestId(String dataTestId) { try { WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT); - return wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']"))))).isDisplayed(); + return wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath(String.format(TEST_ID_XPATH, dataTestId))))).isDisplayed(); } catch (Exception e) { return false; } @@ -240,47 +248,34 @@ public final class GeneralUIUtils { } } - public static boolean tryToclickOnElementByTestId(String dataTestId, int timeOut) { - try { - clickOnElementByTestId(dataTestId, timeOut); - ultimateWait(); - return true; - } catch (Exception e) { - LOGGER.debug("", e); - return false; - } - } - - public static void clickOnElementByTestIdWithoutWait(String dataTestId) { - WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT); + public static void clickOnElementByTestIdWithoutWait(final String dataTestId) { try { + final WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT); wait.until(ExpectedConditions - .elementToBeClickable(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']")))).click(); - getExtendTest().log(Status.INFO, String.format("Click on element ", DATA_TESTS_ID, dataTestId, "']")); - } catch (Exception e) { + .elementToBeClickable(By.xpath(String.format(TEST_ID_XPATH, dataTestId)))).click(); + final String message = + String.format("Click on element with attribute '%s' value '%s'", TEST_ID_XPATH, dataTestId); + getExtendTest().log(Status.INFO, message); + } catch (final Exception e) { ExtentTestActions.log(Status.FAIL, dataTestId + " element isn't clickable"); ExtentTestActions.log(Status.FAIL, e); } } - public static void clickOnElementByInputTestIdWithoutWait(String dataTestId) { - WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT); - wait.until(ExpectedConditions.elementToBeClickable(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']//*")))).click(); + public static void clickOnElementByInputTestIdWithoutWait(final String dataTestId) { + final WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT); + final String xPath = String.format(TEST_ID_CHILD_XPATH, dataTestId); + wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xPath))).click(); } public static void clickOnElementByTestId(String dataTestId, int customTimeout) { WebDriverWait wait = new WebDriverWait(getDriver(), customTimeout); - wait.until(ExpectedConditions.elementToBeClickable(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']")))).click(); + wait.until(ExpectedConditions.elementToBeClickable(By.xpath(String.format(TEST_ID_XPATH, dataTestId)))).click(); } public static WebElement waitForElementVisibilityByTestId(String dataTestId) { WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT); - return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']")))); - } - - public static WebElement waitForElementVisibilityByClass(String className) { - WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT); - return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(String.format("//*[contains(@class, '", className, "')]")))); + return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(String.format(TEST_ID_XPATH, dataTestId)))); } public static Boolean waitForElementInVisibilityByTestId(String dataTestId) { @@ -289,10 +284,9 @@ public final class GeneralUIUtils { public static Boolean waitForElementInVisibilityByTestId(String dataTestId, int timeOut) { WebDriverWait wait = new WebDriverWait(getDriver(), timeOut); - boolean displayed = getDriver().findElements(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']"))).isEmpty(); + boolean displayed = getDriver().findElements(By.xpath(String.format(TEST_ID_XPATH, dataTestId))).isEmpty(); if (!displayed) { - Boolean until = wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "'])")))); - return until; + return wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(String.format(TEST_ID_XPATH, dataTestId)))); } return false; } @@ -416,7 +410,7 @@ public final class GeneralUIUtils { public static WebElement getSelectedElementFromDropDown(String dataTestId) { GeneralUIUtils.ultimateWait(); - return new Select(getDriver().findElement(By.xpath(String.format(DATA_TESTS_ID, dataTestId, "']")))).getFirstSelectedOption(); + return new Select(getDriver().findElement(By.xpath(String.format(TEST_ID_XPATH, dataTestId)))).getFirstSelectedOption(); } public static boolean checkElementsCountInTable(int expectedElementsCount, Supplier> func) { @@ -442,7 +436,7 @@ public final class GeneralUIUtils { func.run(); long estimateTime = System.nanoTime(); long duration = TimeUnit.NANOSECONDS.toSeconds(estimateTime - startTime); - return String.format("%02`d:%02d", duration / DURATION_FORMATIN, duration % DURATION_FORMATIN); + return String.format("%02d:%02d", duration / DURATION_FORMATIN, duration % DURATION_FORMATIN); } public static WebElement clickOnAreaJS(String areaId) { @@ -592,7 +586,7 @@ public final class GeneralUIUtils { } public static String getDataTestIdAttributeValue(WebElement element) { - return element.getAttribute("data-tests-id"); + return element.getAttribute(TEST_ID_ATTRIBUTE_NAME); } public static String getTextContentAttributeValue(WebElement element) { -- cgit 1.2.3-korg