vid-automation selenium tests
[vid.git] / vid-automation / src / main / java / vid / automation / test / infra / Get.java
1 package vid.automation.test.infra;
2
3 import org.junit.Assert;
4 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
5 import org.openqa.selenium.*;
6 import org.openqa.selenium.support.ui.WebDriverWait;
7
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.function.Function;
11
12 public class Get {
13     public static WebElement byId(String id) {
14         try {
15             return GeneralUIUtils.getDriver().findElement(By.id(id));
16         } catch (Exception var2) {
17             return null;
18         }
19     }
20
21     public static WebElement byTestId(String dataTestId) {
22         try {
23             return GeneralUIUtils.getDriver().findElement(By.xpath("//*[@data-tests-id='" + dataTestId + "']"));
24         } catch (Exception var2) {
25             return null;
26         }
27     }
28
29     public static WebElement byXpath(String xpath) {
30         try {
31             return GeneralUIUtils.getWebElementBy(By.xpath(xpath));
32         } catch (Exception var2) {
33             return null;
34         }
35     }
36
37     public static WebElement byXpath(String xpath, int timeout) {
38         try {
39             return GeneralUIUtils.getWebElementBy(By.xpath(xpath), timeout);
40         } catch (Exception var2) {
41             return null;
42         }
43     }
44
45
46     public static List<WebElement> multipleElementsByTestId(String dataTestId) {
47         try {
48             return GeneralUIUtils.getWebElementsListByTestID(dataTestId);
49         } catch (Exception var2) {
50             return null;
51         }
52     }
53
54     public static WebElement byClassAndText(String className, String text) {
55         return byClassAndText(className, text, null);
56     }
57
58     public static WebElement byClassAndText(String className, String text, Integer timeoutInSeconds) {
59         WebElement result = null;
60         List<WebElement> elements;
61         if (timeoutInSeconds!=null) {
62             elements = GeneralUIUtils.getWebElementsListByContainsClassName(className, timeoutInSeconds);
63         }
64         else {
65             elements = GeneralUIUtils.getWebElementsListByContainsClassName(className);
66         }
67
68         for(WebElement element : elements) {
69             if (element.getText().contains(text)) {
70                 result = element;
71                 break;
72             }
73         }
74
75         return result;
76     }
77
78     public static WebElement byCssSelectorAndText(String css, String text) {
79         WebElement element = GeneralUIUtils.getDriver().findElement(By.cssSelector(css));
80
81         if (element != null && element.getText().contains(text)) {
82             return element;
83         }
84
85         return null;
86     }
87
88     public static String selectedOptionText(String dataTestId) {
89         return GeneralUIUtils.getSelectedElementFromDropDown(dataTestId).getText();
90     }
91
92
93     public static List<WebElement> byClass(String className) {
94         return GeneralUIUtils.getWebElementsListByContainsClassName(className);
95     }
96
97     public static WebElement byCssSelector(String css) {
98         return GeneralUIUtils.getDriver().findElement(By.cssSelector(css));
99     }
100
101     public static List<String> tableHeaderValuesByTestId(String tableId) {
102         return tableValuesById(tableId, "thead", "th").get(0);
103     }
104
105     public static List<List<String>> tableBodyValuesByTestId(String tableId) {
106         return tableValuesById(tableId, "tbody", "td");
107     }
108
109     private static List<WebElement> rowsByTableId(String tableId,String section, String column) {
110         try {
111             return GeneralUIUtils.getElemenetsFromTable(By.xpath("//table[@data-tests-id=\"" + tableId + "\"]/" + section + "/tr"));
112         } catch (Exception var2) {
113             return null;
114         }
115     }
116     
117     private static List<List<String>> tableValuesById(String tableId, String section, String column) {
118         List<WebElement> rows = rowsByTableId(tableId, section, column);
119         if(rows != null) {
120             List<List<String>> tableContent = new ArrayList<List<String>>();
121             for(WebElement row:rows) {
122                 List<WebElement> columns = row.findElements(By.xpath(column));
123                 tableContent.add(GeneralUIUtils.getWebElementListText(columns));
124             }
125             return tableContent;
126         }
127         else {
128             return null;
129         }
130     }
131     public static String alertText() {
132         WebDriverWait wait = new WebDriverWait(GeneralUIUtils.getDriver(), 2);
133         wait.until(alertIsPresent());
134         Alert alert = GeneralUIUtils.getDriver().switchTo().alert();
135         Assert.assertTrue(alert != null);
136         return alert.getText();
137     }
138     
139         public static Function<WebDriver, Alert> alertIsPresent() {
140                 return new Function<WebDriver, Alert>() {
141                         public String toString() {
142                                 return "alert to be present";
143                         }
144
145                         @Override
146                         public Alert apply(WebDriver driver) {
147                                 try {
148                                         return driver.switchTo().alert();
149                                 } catch (NoAlertPresentException arg2) {
150                                         return null;
151                                 }
152                         }
153                 };
154         }
155 }