1 package vid.automation.test.infra;
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;
13 public static boolean byText(String text) {
14 return GeneralUIUtils.findAndWaitByText(text, Constants.generalTimeout);
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++) {
21 if (predicate.test(input)) {
27 System.out.println(String.format("a retry failed due to: %s %s", t, t.getMessage()));
30 intervalUnit.sleep(interval);
31 } catch (InterruptedException e) {
36 if (lastError != null) {
37 throw ExceptionUtils.<RuntimeException>rethrow(lastError);
43 public static <T> boolean waitFor(Predicate<T> predicate, T input, int numOfRetries, int interval) {
44 return waitFor(predicate, input, numOfRetries, interval, TimeUnit.SECONDS);
47 public static boolean waitByClassAndText(String className, String text, int timeoutInSeconds) {
48 return waitFor((x->Get.byClassAndText(className, text, 1)!=null), null, timeoutInSeconds, 1);
51 public static boolean waitByClassAndTextXpathOnly(String className, String text, int timeoutInSeconds) {
53 return CollectionUtils.isNotEmpty(
54 GeneralUIUtils.getWebElementsListByContainsClassNameAndText(className, text, timeoutInSeconds));
56 catch (RuntimeException exception) {
58 String.format("Failed to waitByClassAndText, after %d seconds. Class name: %s, Text: %s. Cause: %s",
59 timeoutInSeconds, className, text, exception.toString()));
64 public static boolean waitByTestId(String dataTestId, int timeoutInSeconds) {
65 return waitFor((x->Get.byTestId(dataTestId)!=null),null, timeoutInSeconds, 1);
68 public static boolean waitByIdAndText(String id, String text, int timeoutInSeconds) {
69 return waitFor((x->Get.byId(id).getText().equals(text)),null, timeoutInSeconds, 1);
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")) {
81 } catch (InterruptedException e) {
88 public static void modalToDisappear() {
89 for (int i=0; i<Constants.generalRetries; i++) {
91 Object modalElement = Get.byCssSelector(Constants.Modals.modalClass);
92 if(modalElement == null) {
97 } catch (NoSuchElementException e) {
99 } catch (InterruptedException e) {
105 public static void modalToBeDisplayed() {
106 for (int i=0; i<Constants.generalRetries; i++) {
108 Object modalElement = Get.byCssSelector(Constants.Modals.modalClass);
109 if(modalElement == null) {
114 } catch (Exception e) {
117 } catch (InterruptedException e1) {
118 e1.printStackTrace();