Fix xpath selectors and format strings in ui-ci 08/100608/3
authorandre.schmid <andre.schmid@est.tech>
Wed, 22 Jan 2020 16:17:07 +0000 (16:17 +0000)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Sun, 26 Jan 2020 09:12:04 +0000 (09:12 +0000)
Change-Id: I70209d0c87dcb4277cb1355010a881553704b3f2
Issue-ID: SDC-2731
Signed-off-by: andre.schmid <andre.schmid@est.tech>
ui-ci/src/main/java/org/openecomp/sdc/ci/tests/utilities/GeneralUIUtils.java

index d4766c9..57112dc 100644 (file)
 
 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<WebElement> 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<WebElement> getWebElementsListByContainTestID(String dataTestId) {
+    public static List<WebElement> 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<WebElement>();
+            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<WebElement> getWebElementsListByContainsClassName(String containedText) {
-        WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
-        return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[contains(@class, '" + containedText + "')]")));
+    public static List<WebElement> 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<WebElement> 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<WebElement> 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<List<WebElement>> 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) {