Fix xpath selectors and format strings in ui-ci
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / utilities / GeneralUIUtils.java
index ef91c1c..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;
 import org.openecomp.sdc.ci.tests.execute.setup.DriverFactory;
+import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
 import org.openecomp.sdc.ci.tests.pages.HomePage;
 import org.openecomp.sdc.ci.tests.utils.Utils;
 import org.openqa.selenium.By;
@@ -40,29 +55,17 @@ import org.openqa.selenium.interactions.Actions;
 import org.openqa.selenium.support.ui.ExpectedConditions;
 import org.openqa.selenium.support.ui.Select;
 import org.openqa.selenium.support.ui.WebDriverWait;
-
-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.List;
-import java.util.UUID;
-import java.util.concurrent.TimeUnit;
-import java.util.function.Supplier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-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='%1$s' or @data-test-id='%1$s']";
+    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);
@@ -117,13 +120,19 @@ public final class GeneralUIUtils {
     }
 
     public static WebElement getWebElementByTestID(final String dataTestId, final int timeout) {
-        final WebDriverWait wait = new WebDriverWait(getDriver(), timeout);
-        return wait
-            .until(ExpectedConditions.visibilityOfElementLocated(By.xpath(String.format(DATA_TESTS_ID, dataTestId))));
+        try {
+            final WebDriverWait wait = new WebDriverWait(getDriver(), timeout);
+            return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(String.format(TEST_ID_XPATH, dataTestId))));
+        }
+        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) {
@@ -133,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;
         }
@@ -141,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)));
 
     }
 
@@ -176,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, '%1$s') or contains(@data-test-id, '%1$s')]", 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) {
@@ -201,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) {
@@ -213,50 +227,55 @@ 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;
         }
     }
 
     public static void clickOnElementByTestId(String dataTestId) {
-        LOGGER.debug("Clicking on the element by test id " + dataTestId);
-        clickOnElementByTestIdWithoutWait(dataTestId);
-        LOGGER.debug("Waiting after clicking element by test id " + dataTestId);
-        ultimateWait();
-        LOGGER.debug(String.format("Waiting after clicking element by test id '%s' finished", dataTestId));
-    }
-
-    public static void clickOnElementChildByTestId(String dataTestId) {
-        clickOnElementChildByTestIdWithoutWait(dataTestId);
-        ultimateWait();
+        try {
+            clickOnElementByTestIdWithoutWait(dataTestId);
+            ultimateWait();
+        }catch (Exception e) {
+            LOGGER.debug("", e);
+        }
     }
 
     public static void clickOnElementByTestIdWithoutWait(final String dataTestId) {
-        final WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
-        wait
-            .until(ExpectedConditions.elementToBeClickable(By.xpath(String.format(DATA_TESTS_ID, dataTestId)))).click();
+        try {
+            final WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
+            wait.until(ExpectedConditions
+                    .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 clickOnElementChildByTestIdWithoutWait(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))));
+        return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(String.format(TEST_ID_XPATH, dataTestId))));
     }
 
     public static Boolean waitForElementInVisibilityByTestId(String dataTestId) {
@@ -265,11 +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))));
-            ultimateWait();
-            return until;
+            return wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(String.format(TEST_ID_XPATH, dataTestId))));
         }
         return false;
     }
@@ -363,7 +380,7 @@ public final class GeneralUIUtils {
     }
 
     public static void moveToStep(final String dataTestId) {
-        clickOnElementChildByTestId(dataTestId);
+        clickOnElementByTestId(dataTestId);
     }
 
 
@@ -393,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) {
@@ -569,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) {