cab3a2d5bdf86922afc56e4998d894c1bb7fd1a8
[vid.git] / vid-automation / src / main / java / vid / automation / test / infra / Wait.java
1 package vid.automation.test.infra;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.onap.sdc.ci.tests.utilities.GeneralUIUtils;
5 import org.openqa.selenium.JavascriptExecutor;
6 import org.openqa.selenium.NoSuchElementException;
7 import vid.automation.test.Constants;
8
9 import java.util.concurrent.TimeUnit;
10 import java.util.function.Predicate;
11
12 public class Wait {
13     public static boolean byText(String text) {
14         return GeneralUIUtils.findAndWaitByText(text, Constants.generalTimeout);
15     }
16
17     public static <T> boolean waitFor(Predicate<T> predicate, T input, int numOfRetries, int interval, TimeUnit intervalUnit) {
18         for (int i=0; i<numOfRetries; i++) {
19             try {
20                 if (predicate.test(input)) {
21                     return true;
22                 }
23             }
24             catch (Throwable t) {
25                 System.out.println(String.format("a retry failed due to: %s %s", t, t.getMessage()));
26             }
27             try {
28                 intervalUnit.sleep(interval);
29             } catch (InterruptedException e) {
30                 e.printStackTrace();
31             }
32         }
33         return false;
34     }
35
36     public static <T> boolean waitFor(Predicate<T> predicate, T input, int numOfRetries, int interval) {
37         return waitFor(predicate, input, numOfRetries, interval, TimeUnit.SECONDS);
38     }
39
40     public static boolean waitByClassAndText(String className, String text, int timeoutInSeconds) {
41         return waitFor((x->Get.byClassAndText(className, text, 1)!=null), null, timeoutInSeconds, 1);
42     }
43
44     public static boolean waitByClassAndTextXpathOnly(String className, String text, int timeoutInSeconds) {
45         try {
46             return CollectionUtils.isNotEmpty(
47                     GeneralUIUtils.getWebElementsListByContainsClassNameAndText(className, text, timeoutInSeconds));
48         }
49         catch (RuntimeException exception) {
50             System.out.println(
51                 String.format("Failed to waitByClassAndText, after %d seconds. Class name: %s, Text: %s. Cause: %s",
52                     timeoutInSeconds, className, text, exception.toString()));
53             return false;
54         }
55     }
56
57     public static boolean waitByTestId(String dataTestId,  int timeoutInSeconds) {
58         return waitFor((x->Get.byTestId(dataTestId)!=null),null, timeoutInSeconds, 1);
59     }
60
61     public static boolean waitByIdAndText(String id,  String text, int timeoutInSeconds) {
62         return waitFor((x->Get.byId(id).getText().equals(text)),null, timeoutInSeconds, 1);
63     }
64
65     public static void angularHttpRequestsLoaded() {
66         JavascriptExecutor js = (JavascriptExecutor) GeneralUIUtils.getDriver();
67         for (int i=0; i<Constants.generalRetries; i++) {
68             Object result = js.executeScript("return window.angular.element('body').injector().get('$http').pendingRequests.length;");
69             if(result.toString().equals("0")) {
70                 break;
71             } else {
72                 try {
73                     Thread.sleep(1000);
74                 } catch (InterruptedException e) {
75                     e.printStackTrace();
76                 }
77             }
78         }
79     }
80
81     public static void modalToDisappear() {
82         for (int i=0; i<Constants.generalRetries; i++) {
83             try {
84                 Object modalElement =  Get.byCssSelector(Constants.Modals.modalClass);
85                 if(modalElement == null) {
86                     break;
87                 } else {
88                     Thread.sleep(1000);
89                 }
90             } catch (NoSuchElementException e) {
91                 break;
92             } catch (InterruptedException e) {
93                 e.printStackTrace();
94             }
95         }
96     }
97
98     public static void modalToBeDisplayed() {
99         for (int i=0; i<Constants.generalRetries; i++) {
100             try {
101                 Object modalElement =  Get.byCssSelector(Constants.Modals.modalClass);
102                 if(modalElement == null) {
103                     Thread.sleep(1000);
104                 } else {
105                     break;
106                 }
107             } catch (Exception e) {
108                 try {
109                     Thread.sleep(1000);
110                 } catch (InterruptedException e1) {
111                     e1.printStackTrace();
112                 }
113                 e.printStackTrace();
114             }
115         }
116     }
117 }