1 package vid.automation.test.infra;
3 import org.junit.Assert;
4 import org.onap.sdc.ci.tests.utilities.GeneralUIUtils;
5 import org.openqa.selenium.*;
6 import org.openqa.selenium.support.ui.WebDriverWait;
8 import java.util.ArrayList;
10 import java.util.function.Function;
13 public static WebElement byId(String id) {
15 return GeneralUIUtils.getDriver().findElement(By.id(id));
16 } catch (Exception var2) {
21 public static WebElement byTestId(String dataTestId) {
23 return GeneralUIUtils.getDriver().findElement(getXpathForDataTestId(dataTestId));
24 } catch (Exception var2) {
29 public static WebElement byXpath(String xpath) {
31 return GeneralUIUtils.getWebElementBy(By.xpath(xpath));
32 } catch (Exception var2) {
37 public static WebElement byXpath(WebElement context, String xpath) {
39 return context.findElement(By.xpath(xpath));
40 } catch (Exception var2) {
45 public static WebElement byXpath(String xpath, int timeout) {
47 return GeneralUIUtils.getWebElementBy(By.xpath(xpath), timeout);
48 } catch (Exception var2) {
54 public static List<WebElement> multipleElementsByTestId(String dataTestId) {
56 return GeneralUIUtils.getWebElementsListByTestID(dataTestId);
57 } catch (Exception var2) {
62 public static WebElement byClassAndText(String className, String text) {
63 return byClassAndText(className, text, null);
66 public static WebElement byClassAndText(String className, String text, Integer timeoutInSeconds) {
67 WebElement result = null;
68 List<WebElement> elements;
69 if (timeoutInSeconds!=null) {
70 elements = GeneralUIUtils.getWebElementsListByContainsClassName(className, timeoutInSeconds);
73 elements = GeneralUIUtils.getWebElementsListByContainsClassName(className);
76 for(WebElement element : elements) {
77 if (element.getText().contains(text)) {
86 public static WebElement byCssSelectorAndText(String css, String text) {
87 WebElement element = GeneralUIUtils.getDriver().findElement(By.cssSelector(css));
89 if (element != null && element.getText().contains(text)) {
96 public static String selectedOptionText(String dataTestId) {
97 return GeneralUIUtils.getSelectedElementFromDropDown(dataTestId).getText();
101 public static List<WebElement> byClass(String className) {
102 return GeneralUIUtils.getWebElementsListByContainsClassName(className);
105 public static WebElement byCssSelector(String css) {
106 return GeneralUIUtils.getDriver().findElement(By.cssSelector(css));
109 public static List<String> tableHeaderValuesByTestId(String tableId) {
110 return tableValuesById(tableId, "thead", "th").get(0);
113 public static List<List<String>> tableBodyValuesByTestId(String tableId) {
114 return tableValuesById(tableId, "tbody", "td");
117 private static List<WebElement> rowsByTableId(String tableId,String section, String column) {
119 return GeneralUIUtils.getElemenetsFromTable(By.xpath("//table[@data-tests-id=\"" + tableId + "\"]/" + section + "/tr"));
120 } catch (Exception var2) {
125 private static List<List<String>> tableValuesById(String tableId, String section, String column) {
126 List<WebElement> rows = rowsByTableId(tableId, section, column);
128 List<List<String>> tableContent = new ArrayList<List<String>>();
129 for(WebElement row:rows) {
130 List<WebElement> columns = row.findElements(By.xpath(column));
131 tableContent.add(GeneralUIUtils.getWebElementListText(columns));
139 public static String alertText() {
140 WebDriverWait wait = new WebDriverWait(GeneralUIUtils.getDriver(), 2);
141 wait.until(alertIsPresent());
142 Alert alert = GeneralUIUtils.getDriver().switchTo().alert();
143 Assert.assertTrue(alert != null);
144 return alert.getText();
147 public static Function<WebDriver, Alert> alertIsPresent() {
148 return new Function<WebDriver, Alert>() {
149 public String toString() {
150 return "alert to be present";
154 public Alert apply(WebDriver driver) {
156 return driver.switchTo().alert();
157 } catch (NoAlertPresentException arg2) {
164 public static List<WebElement> listByTestId(String dataTestId) {
166 return GeneralUIUtils.getDriver().findElements(getXpathForDataTestId(dataTestId));
167 } catch (Exception var2) {
172 public static By getXpathForDataTestId(String dataTestId) {
173 return By.xpath("//*[@data-tests-id='" + dataTestId + "']");