Merge automation from ECOMP's repository
[vid.git] / vid-automation / src / main / java / org / onap / sdc / ci / tests / utilities / AdditionalConditions.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.ci.tests.utilities;
22
23 import com.paulhammant.ngwebdriver.NgWebDriver;
24 import org.openqa.selenium.By;
25 import org.openqa.selenium.JavascriptExecutor;
26 import org.openqa.selenium.WebDriver;
27 import org.openqa.selenium.WebElement;
28 import org.openqa.selenium.support.ui.ExpectedCondition;
29
30 import java.util.List;
31
32 public class AdditionalConditions {
33         
34         public static ExpectedCondition<WebElement> searchForOneElement(List<By> ByList){
35                  return new ExpectedCondition<WebElement>() {
36                         @Override
37                         public WebElement apply(WebDriver driver) {
38                                 for (By by : ByList){
39                                         WebElement findElement = driver.findElement(by);
40                                         if (findElement != null){
41                                                 return findElement;
42                                         }
43                                 }
44                                 return null;
45                         }
46                 };
47         }
48         
49         public static ExpectedCondition<Boolean> jQueryAJAXCallsHaveCompleted() {
50             return new ExpectedCondition<Boolean>() {
51                 @Override
52                         public Boolean apply(WebDriver driver) {
53                         return (Boolean) ((JavascriptExecutor)driver).
54                                         executeScript("return (window.jQuery!= null) && (jQuery.active === 0);");
55                         }
56                 };
57         }
58         
59         public static ExpectedCondition <Boolean> angularHasFinishedProcessing() {
60            return new ExpectedCondition<Boolean>() {
61                         @Override
62                         public Boolean apply(WebDriver driver) {
63 //                              String scriptJS = "return (window.angular !==undefined) &&"
64 //                                              + " (angular.element(document).injector() !==undefined) &&"
65 //                                              + " (angular.element(document).injector().get('$http').pendingRequests.length === 0)";
66 //                              return Boolean.valueOf(( (JavascriptExecutor) driver).executeScript(scriptJS).toString());
67                                 new NgWebDriver((JavascriptExecutor) driver).waitForAngularRequestsToFinish();
68                                 return true;
69                         }
70            };
71         }
72         
73         public static ExpectedCondition <Boolean> angular2HasFinishedProcessing() {
74            return driver -> {
75                    new NgWebDriver((JavascriptExecutor) driver).waitForAngular2RequestsToFinish();
76                    return true;
77            };
78         }
79
80     public static ExpectedCondition <Boolean> pageLoadWait() {
81         return new ExpectedCondition<Boolean>() {
82                    @Override
83                    public Boolean apply(WebDriver driver) {
84                           String scriptJS = 
85                                        "try {\r\n" + 
86                                        "  if (document.readyState !== 'complete') {\r\n" + 
87                                        "    return false; // Page not loaded yet\r\n" + 
88                                        "  }\r\n" + 
89                                        "  if (window.jQuery) {\r\n" + 
90                                        "    if (window.jQuery.active) {\r\n" + 
91                                        "      return false;\r\n" + 
92                                        "    } else if (window.jQuery.ajax && window.jQuery.ajax.active) {\r\n" + 
93                                        "      return false;\r\n" + 
94                                        "    }\r\n" + 
95                                        "  }\r\n" + 
96                                        "  if (window.angular) {\r\n" + 
97                                        "    if (!window.qa) {\r\n" + 
98                                        "      // Used to track the render cycle finish after loading is complete\r\n" + 
99                                        "      window.qa = {\r\n" + 
100                                        "        doneRendering: false\r\n" + 
101                                        "      };\r\n" + 
102                                        "    }\r\n" + 
103                                        "    // Get the angular injector for this app (change element if necessary)\r\n" + 
104                                        "    var injector = window.angular.element(document).find('body').injector();\r\n" +
105                                        "    // Store providers to use for these checks\r\n" + 
106                                        "    var $rootScope = injector.get('$rootScope');\r\n" + 
107                                        "    var $http = injector.get('$http');\r\n" + 
108                                        "    var $timeout = injector.get('$timeout');\r\n" + 
109                                        "    // Check if digest\r\n" + 
110                                        "    if ($rootScope.$$phase === '$apply' || $rootScope.$$phase === '$digest' || $http.pendingRequests.length !== 0) {\r\n" + 
111                                        "      window.qa.doneRendering = false;\r\n" + 
112                                        "      return false; // Angular digesting or loading data\r\n" + 
113                                        "    }\r\n" + 
114                                        "    if (!window.qa.doneRendering) {\r\n" + 
115                                        "      // Set timeout to mark angular rendering as finished\r\n" + 
116                                        "      $timeout(function() {\r\n" + 
117                                        "        window.qa.doneRendering = true;\r\n" + 
118                                        "      }, 0);\r\n" + 
119                                        "      return false;\r\n" + 
120                                        "    }\r\n" + 
121                                        "  }\r\n" + 
122                                        "  return true;\r\n" + 
123                                        "} catch (ex) {\r\n" + 
124                                        "  return false;\r\n" + 
125                                        "}";
126                           return Boolean.valueOf(( (JavascriptExecutor) driver).executeScript(scriptJS).toString());
127                    }
128         };
129      }
130
131         
132         
133 }