9bcad7b6432b196abd7419027ac432b4cd50269c
[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.onap.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(getXpathForDataTestId(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(WebElement context, String xpath) {
38         try {
39             return context.findElement(By.xpath(xpath));
40         } catch (Exception var2) {
41             return null;
42         }
43     }
44
45     public static WebElement byXpath(String xpath, int timeout) {
46         try {
47             return GeneralUIUtils.getWebElementBy(By.xpath(xpath), timeout);
48         } catch (Exception var2) {
49             return null;
50         }
51     }
52
53
54     public static List<WebElement> multipleElementsByTestId(String dataTestId) {
55         try {
56             return GeneralUIUtils.getWebElementsListByTestID(dataTestId);
57         } catch (Exception var2) {
58             return null;
59         }
60     }
61
62     public static WebElement byClassAndText(String className, String text) {
63         return byClassAndText(className, text, null);
64     }
65
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);
71         }
72         else {
73             elements = GeneralUIUtils.getWebElementsListByContainsClassName(className);
74         }
75
76         for(WebElement element : elements) {
77             if (element.getText().contains(text)) {
78                 result = element;
79                 break;
80             }
81         }
82
83         return result;
84     }
85
86     public static WebElement byCssSelectorAndText(String css, String text) {
87         WebElement element = GeneralUIUtils.getDriver().findElement(By.cssSelector(css));
88
89         if (element != null && element.getText().contains(text)) {
90             return element;
91         }
92
93         return null;
94     }
95
96     public static String selectedOptionText(String dataTestId) {
97         return GeneralUIUtils.getSelectedElementFromDropDown(dataTestId).getText();
98     }
99
100
101     public static List<WebElement> byClass(String className) {
102         return GeneralUIUtils.getWebElementsListByContainsClassName(className);
103     }
104
105     public static WebElement byCssSelector(String css) {
106         return GeneralUIUtils.getDriver().findElement(By.cssSelector(css));
107     }
108
109     public static List<String> tableHeaderValuesByTestId(String tableId) {
110         return tableValuesById(tableId, "thead", "th").get(0);
111     }
112
113     public static List<List<String>> tableBodyValuesByTestId(String tableId) {
114         return tableValuesById(tableId, "tbody", "td");
115     }
116
117     private static List<WebElement> rowsByTableId(String tableId,String section, String column) {
118         try {
119             return GeneralUIUtils.getElemenetsFromTable(By.xpath("//table[@data-tests-id=\"" + tableId + "\"]/" + section + "/tr"));
120         } catch (Exception var2) {
121             return null;
122         }
123     }
124     
125     private static List<List<String>> tableValuesById(String tableId, String section, String column) {
126         List<WebElement> rows = rowsByTableId(tableId, section, column);
127         if(rows != null) {
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));
132             }
133             return tableContent;
134         }
135         else {
136             return null;
137         }
138     }
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();
145     }
146     
147         public static Function<WebDriver, Alert> alertIsPresent() {
148                 return new Function<WebDriver, Alert>() {
149                         public String toString() {
150                                 return "alert to be present";
151                         }
152
153                         @Override
154                         public Alert apply(WebDriver driver) {
155                                 try {
156                                         return driver.switchTo().alert();
157                                 } catch (NoAlertPresentException arg2) {
158                                         return null;
159                                 }
160                         }
161                 };
162         }
163
164     public static List<WebElement> listByTestId(String dataTestId) {
165         try {
166             return GeneralUIUtils.getDriver().findElements(getXpathForDataTestId(dataTestId));
167         } catch (Exception var2) {
168             return null;
169         }
170     }
171
172     public static By getXpathForDataTestId(String dataTestId) {
173         return By.xpath("//*[@data-tests-id='" + dataTestId + "']");
174     }
175 }