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