aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/org/onap/sdc/ci/tests/utilities/AdditionalConditions.java
blob: 9a070e2b05cdf0e1772f666627cca421aef461cc (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdc.ci.tests.utilities;

import com.paulhammant.ngwebdriver.NgWebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;

import java.util.List;

public class AdditionalConditions {
	
	public static ExpectedCondition<WebElement> searchForOneElement(List<By> ByList){
		 return new ExpectedCondition<WebElement>() {
			@Override
			public WebElement apply(WebDriver driver) {
				for (By by : ByList){
					WebElement findElement = driver.findElement(by);
					if (findElement != null){
						return findElement;
					}
				}
				return null;
			}
		};
	}
	
	public static ExpectedCondition<Boolean> jQueryAJAXCallsHaveCompleted() {
	    return new ExpectedCondition<Boolean>() {
	        @Override
			public Boolean apply(WebDriver driver) {
	        	return (Boolean) ((JavascriptExecutor)driver).
	        			executeScript("return (window.jQuery!= null) && (jQuery.active === 0);");
			}
		};
	}
	
	public static ExpectedCondition <Boolean> angularHasFinishedProcessing() {
	   return new ExpectedCondition<Boolean>() {
		   	@Override
		   	public Boolean apply(WebDriver driver) {
//		   		String scriptJS = "return (window.angular !==undefined) &&"
//		   				+ " (angular.element(document).injector() !==undefined) &&"
//		   				+ " (angular.element(document).injector().get('$http').pendingRequests.length === 0)";
//		   		return Boolean.valueOf(( (JavascriptExecutor) driver).executeScript(scriptJS).toString());
		   		new NgWebDriver((JavascriptExecutor) driver).waitForAngularRequestsToFinish();
		   		return true;
		   	}
	   };
	}
	
	public static ExpectedCondition <Boolean> angular2HasFinishedProcessing() {
	   return driver -> {
		   new NgWebDriver((JavascriptExecutor) driver).waitForAngular2RequestsToFinish();
		   return true;
	   };
	}

    public static ExpectedCondition <Boolean> pageLoadWait() {
        return new ExpectedCondition<Boolean>() {
                   @Override
                   public Boolean apply(WebDriver driver) {
                          String scriptJS = 
                                       "try {\r\n" + 
                                       "  if (document.readyState !== 'complete') {\r\n" + 
                                       "    return false; // Page not loaded yet\r\n" + 
                                       "  }\r\n" + 
                                       "  if (window.jQuery) {\r\n" + 
                                       "    if (window.jQuery.active) {\r\n" + 
                                       "      return false;\r\n" + 
                                       "    } else if (window.jQuery.ajax && window.jQuery.ajax.active) {\r\n" + 
                                       "      return false;\r\n" + 
                                       "    }\r\n" + 
                                       "  }\r\n" + 
                                       "  if (window.angular) {\r\n" + 
                                       "    if (!window.qa) {\r\n" + 
                                       "      // Used to track the render cycle finish after loading is complete\r\n" + 
                                       "      window.qa = {\r\n" + 
                                       "        doneRendering: false\r\n" + 
                                       "      };\r\n" + 
                                       "    }\r\n" + 
                                       "    // Get the angular injector for this app (change element if necessary)\r\n" + 
                                       "    var injector = window.angular.element(document).find('body').injector();\r\n" +
                                       "    // Store providers to use for these checks\r\n" + 
                                       "    var $rootScope = injector.get('$rootScope');\r\n" + 
                                       "    var $http = injector.get('$http');\r\n" + 
                                       "    var $timeout = injector.get('$timeout');\r\n" + 
                                       "    // Check if digest\r\n" + 
                                       "    if ($rootScope.$$phase === '$apply' || $rootScope.$$phase === '$digest' || $http.pendingRequests.length !== 0) {\r\n" + 
                                       "      window.qa.doneRendering = false;\r\n" + 
                                       "      return false; // Angular digesting or loading data\r\n" + 
                                       "    }\r\n" + 
                                       "    if (!window.qa.doneRendering) {\r\n" + 
                                       "      // Set timeout to mark angular rendering as finished\r\n" + 
                                       "      $timeout(function() {\r\n" + 
                                       "        window.qa.doneRendering = true;\r\n" + 
                                       "      }, 0);\r\n" + 
                                       "      return false;\r\n" + 
                                       "    }\r\n" + 
                                       "  }\r\n" + 
                                       "  return true;\r\n" + 
                                       "} catch (ex) {\r\n" + 
                                       "  return false;\r\n" + 
                                       "}";
                          return Boolean.valueOf(( (JavascriptExecutor) driver).executeScript(scriptJS).toString());
                   }
        };
     }

	
	
}