aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/vid/automation/test/infra/Wait.java
blob: 848107c8338b9e49155a7c7734ac6d1ef4403e98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package vid.automation.test.infra;

import org.apache.commons.collections.CollectionUtils;
import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import vid.automation.test.Constants;

import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

public class Wait {
    public static boolean byText(String text) {
        return GeneralUIUtils.findAndWaitByText(text, Constants.generalTimeout);
    }

    public static <T> boolean waitFor(Predicate<T> predicate, T input, int numOfRetries, int interval, TimeUnit intervalUnit) {
        for (int i=0; i<numOfRetries; i++) {
            try {
                if (predicate.test(input)) {
                    return true;
                }
            }
            catch (Throwable t) {
                System.out.println(String.format("a retry failed due to: %s %s", t, t.getMessage()));
            }
            try {
                intervalUnit.sleep(interval);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static <T> boolean waitFor(Predicate<T> predicate, T input, int numOfRetries, int interval) {
        return waitFor(predicate, input, numOfRetries, interval, TimeUnit.SECONDS);
    }

    public static boolean waitByClassAndText(String className, String text, int timeoutInSeconds) {
        return waitFor((x->Get.byClassAndText(className, text, 1)!=null), null, timeoutInSeconds, 1);
    }

    public static boolean waitByClassAndTextXpathOnly(String className, String text, int timeoutInSeconds) {
        try {
            return CollectionUtils.isNotEmpty(
                    GeneralUIUtils.getWebElementsListByContainsClassNameAndText(className, text, timeoutInSeconds));
        }
        catch (RuntimeException exception) {
            System.out.println(
                String.format("Failed to waitByClassAndText, after %d seconds. Class name: %s, Text: %s. Cause: %s",
                    timeoutInSeconds, className, text, exception.toString()));
            return false;
        }
    }

    public static boolean waitByTestId(String dataTestId,  int timeoutInSeconds) {
        return waitFor((x->Get.byTestId(dataTestId)!=null),null, timeoutInSeconds, 1);
    }

    public static void angularHttpRequestsLoaded() {
        JavascriptExecutor js = (JavascriptExecutor) GeneralUIUtils.getDriver();
        for (int i=0; i<Constants.generalRetries; i++) {
            Object result = js.executeScript("return window.angular.element('body').injector().get('$http').pendingRequests.length;");
            if(result.toString().equals("0")) {
                break;
            } else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void modalToDisappear() {
        for (int i=0; i<Constants.generalRetries; i++) {
            try {
                Object modalElement =  Get.byCssSelector(Constants.Modals.modalClass);
                if(modalElement == null) {
                    break;
                } else {
                    Thread.sleep(1000);
                }
            } catch (NoSuchElementException e) {
                break;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void modalToBeDisplayed() {
        for (int i=0; i<Constants.generalRetries; i++) {
            try {
                Object modalElement =  Get.byCssSelector(Constants.Modals.modalClass);
                if(modalElement == null) {
                    Thread.sleep(1000);
                } else {
                    break;
                }
            } catch (Exception e) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }
        }
    }
}