[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci / src / main / java / org / openecomp / 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.openecomp.sdc.ci.tests.utilities;
22
23 import java.util.concurrent.TimeUnit;
24
25 import org.openqa.selenium.JavascriptExecutor;
26 import org.openqa.selenium.WebDriver;
27 import org.openqa.selenium.support.ui.ExpectedCondition;
28
29 import com.paulhammant.ngwebdriver.NgWebDriver;
30
31 public class AdditionalConditions {
32                 
33         public static ExpectedCondition<Boolean> jQueryAJAXCallsHaveCompleted() {
34             return new ExpectedCondition<Boolean>() {
35                 @Override
36                         public Boolean apply(WebDriver driver) {
37                         return (Boolean) ((JavascriptExecutor)driver).
38                                         executeScript("return (window.jQuery!= null) && (jQuery.active === 0);");
39                         }
40                 };
41         }
42         
43         public static ExpectedCondition <Boolean> angularHasFinishedProcessing() {
44            return new ExpectedCondition<Boolean>() {
45                         @Override
46                         public Boolean apply(WebDriver driver) {
47 //                              String scriptJS = "return (window.angular !==undefined) &&"
48 //                                              + " (angular.element(document).injector() !==undefined) &&"
49 //                                              + " (angular.element(document).injector().get('$http').pendingRequests.length === 0)";
50 //                              return Boolean.valueOf(( (JavascriptExecutor) driver).executeScript(scriptJS).toString());
51                                 new NgWebDriver((JavascriptExecutor) driver).waitForAngularRequestsToFinish();
52                                 return true;
53                         }
54            };
55         }
56         
57     public static ExpectedCondition <Boolean> pageLoadWait() {
58         return new ExpectedCondition<Boolean>() {
59                    @Override
60                    public Boolean apply(WebDriver driver) {
61                           String scriptJS = 
62                                        "try {\r\n" + 
63                                        "  if (document.readyState !== 'complete') {\r\n" + 
64                                        "    return false; // Page not loaded yet\r\n" + 
65                                        "  }\r\n" + 
66                                        "  if (window.jQuery) {\r\n" + 
67                                        "    if (window.jQuery.active) {\r\n" + 
68                                        "      return false;\r\n" + 
69                                        "    } else if (window.jQuery.ajax && window.jQuery.ajax.active) {\r\n" + 
70                                        "      return false;\r\n" + 
71                                        "    }\r\n" + 
72                                        "  }\r\n" + 
73                                        "  if (window.angular) {\r\n" + 
74                                        "    if (!window.qa) {\r\n" + 
75                                        "      // Used to track the render cycle finish after loading is complete\r\n" + 
76                                        "      window.qa = {\r\n" + 
77                                        "        doneRendering: false\r\n" + 
78                                        "      };\r\n" + 
79                                        "    }\r\n" + 
80                                        "    // Get the angular injector for this app (change element if necessary)\r\n" + 
81                                        "    var injector = window.angular.element('body').injector();\r\n" + 
82                                        "    // Store providers to use for these checks\r\n" + 
83                                        "    var $rootScope = injector.get('$rootScope');\r\n" + 
84                                        "    var $http = injector.get('$http');\r\n" + 
85                                        "    var $timeout = injector.get('$timeout');\r\n" + 
86                                        "    // Check if digest\r\n" + 
87                                        "    if ($rootScope.$$phase === '$apply' || $rootScope.$$phase === '$digest' || $http.pendingRequests.length !== 0) {\r\n" + 
88                                        "      window.qa.doneRendering = false;\r\n" + 
89                                        "      return false; // Angular digesting or loading data\r\n" + 
90                                        "    }\r\n" + 
91                                        "    if (!window.qa.doneRendering) {\r\n" + 
92                                        "      // Set timeout to mark angular rendering as finished\r\n" + 
93                                        "      $timeout(function() {\r\n" + 
94                                        "        window.qa.doneRendering = true;\r\n" + 
95                                        "      }, 0);\r\n" + 
96                                        "      return false;\r\n" + 
97                                        "    }\r\n" + 
98                                        "  }\r\n" + 
99                                        "  return true;\r\n" + 
100                                        "} catch (ex) {\r\n" + 
101                                        "  return false;\r\n" + 
102                                        "}";
103                           return Boolean.valueOf(( (JavascriptExecutor) driver).executeScript(scriptJS).toString());
104                    }
105         };
106      }
107
108         
109         
110 }