Fix for Penetration test _ Session and cookie management
[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     public static Boolean isOptionSelectedInMultiSelect(String dataTestId, String option) {
101         return GeneralUIUtils.isOptionSelectedInMultiSelect(dataTestId, option);
102     }
103
104     public static List<WebElement> byClass(String className) {
105         return GeneralUIUtils.getWebElementsListByContainsClassName(className);
106     }
107
108     public static WebElement byCssSelector(String css) {
109         return GeneralUIUtils.getDriver().findElement(By.cssSelector(css));
110     }
111
112     public static List<String> tableHeaderValuesByTestId(String tableId) {
113         return tableValuesById(tableId, "thead", "th").get(0);
114     }
115
116     public static List<List<String>> tableBodyValuesByTestId(String tableId) {
117         return tableValuesById(tableId, "tbody", "td");
118     }
119
120     private static List<WebElement> rowsByTableId(String tableId,String section, String column) {
121         try {
122             return GeneralUIUtils.getElemenetsFromTable(By.xpath("//table[@data-tests-id=\"" + tableId + "\"]/" + section + "/tr"));
123         } catch (Exception var2) {
124             return null;
125         }
126     }
127     
128     private static List<List<String>> tableValuesById(String tableId, String section, String column) {
129         List<WebElement> rows = rowsByTableId(tableId, section, column);
130         if(rows != null) {
131             List<List<String>> tableContent = new ArrayList<List<String>>();
132             for(WebElement row:rows) {
133                 List<WebElement> columns = row.findElements(By.xpath(column));
134                 tableContent.add(GeneralUIUtils.getWebElementListText(columns));
135             }
136             return tableContent;
137         }
138         else {
139             return null;
140         }
141     }
142     public static String alertText() {
143         WebDriverWait wait = new WebDriverWait(GeneralUIUtils.getDriver(), 2);
144         wait.until(alertIsPresent());
145         Alert alert = GeneralUIUtils.getDriver().switchTo().alert();
146         Assert.assertTrue(alert != null);
147         return alert.getText();
148     }
149     
150         public static Function<WebDriver, Alert> alertIsPresent() {
151                 return new Function<WebDriver, Alert>() {
152                         public String toString() {
153                                 return "alert to be present";
154                         }
155
156                         @Override
157                         public Alert apply(WebDriver driver) {
158                                 try {
159                                         return driver.switchTo().alert();
160                                 } catch (NoAlertPresentException arg2) {
161                                         return null;
162                                 }
163                         }
164                 };
165         }
166
167     public static List<WebElement> listByTestId(String dataTestId) {
168         try {
169             return GeneralUIUtils.getDriver().findElements(getXpathForDataTestId(dataTestId));
170         } catch (Exception var2) {
171             return null;
172         }
173     }
174
175     public static By getXpathForDataTestId(String dataTestId) {
176         return By.xpath("//*[@data-tests-id='" + dataTestId + "']");
177     }
178 }