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