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