re base code
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / setup / WebDriverThread.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.execute.setup;
22
23 import net.lightbody.bmp.BrowserMobProxyServer;
24 import net.lightbody.bmp.client.ClientUtil;
25 import net.lightbody.bmp.proxy.CaptureType;
26 import org.openecomp.sdc.ci.tests.config.Config;
27 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
28 import org.openqa.selenium.Platform;
29 import org.openqa.selenium.UnexpectedAlertBehaviour;
30 import org.openqa.selenium.WebDriver;
31 import org.openqa.selenium.firefox.FirefoxDriver;
32 import org.openqa.selenium.firefox.FirefoxProfile;
33 import org.openqa.selenium.remote.CapabilityType;
34 import org.openqa.selenium.remote.DesiredCapabilities;
35 import org.openqa.selenium.remote.LocalFileDetector;
36 import org.openqa.selenium.remote.RemoteWebDriver;
37
38 import java.io.File;
39 import java.net.MalformedURLException;
40 import java.net.URL;
41 import java.util.UUID;
42
43 //import org.openqa.selenium.firefox.FirefoxOptions; // Selenium 3.4.0 change
44
45 public class WebDriverThread {
46
47         public static final String AUTOMATION_DOWNLOAD_DIR = "automationDownloadDir";
48         private WebDriver webdriver;
49         private FirefoxProfile firefoxProfile;
50         public static final String SELENIUM_NODE_URL = "http://%s:%s/wd/hub";
51         
52         public WebDriverThread(Config config) {
53                 initDriver(config);
54                 webdriver.manage().window().maximize();
55         }
56         
57         public WebDriver getDriver() throws Exception {
58                 return webdriver;
59         }
60         
61         public void quitDriver() {
62                 if (webdriver != null) {
63                         webdriver.quit();
64                         webdriver = null;
65                 }
66         }
67         
68         
69         public void initDriver(Config config){
70                 try {
71                         boolean remoteTesting = config.isRemoteTesting();
72                         if (!remoteTesting) {
73                                 boolean mobProxyStatus = config.getUseBrowserMobProxy();
74                                 if (mobProxyStatus){
75                                         setWebDriverWithMobProxy();
76                                 } else {
77                                         
78                     // Selenium 3.4.0 change, add firefox options : set, add firefox 5.X location and enable 
79 //                                      FirefoxOptions options = new FirefoxOptions();
80 //                                      options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox5\\firefox.exe"); //Location where Firefox is installed
81 //                                      options.addPreference("dom.file.createInChild", true); // Enable file upload with sendKeys
82                                         // End of Selenium 3.4.0 change 
83                                         
84                                         System.out.println("Opening LOCAL browser");
85                                         DesiredCapabilities cap = new DesiredCapabilities();
86                                                                         
87                                         cap = DesiredCapabilities.firefox();
88                                         cap.setBrowserName("firefox");
89                                         cap.setCapability(FirefoxDriver.PROFILE, initFirefoxProfile());
90                                         //unexpected model dialog fix.
91                                         cap.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
92 //                                      cap.setCapability("moz:firefoxOptions", options); //Add options to Capabilities, Selenium 3.4.0 change
93                                         
94                                         firefoxProfile.setPreference("network.proxy.type", 2);
95 //                                      firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://emea-auto.proxy.att.com:8001/");
96                                         firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://autoproxy.sbc.com/autoproxy.cgi");
97                                         firefoxProfile.setPreference("network.proxy.no_proxies_on", "localhost");
98                                         
99                                         webdriver = new FirefoxDriver(cap);
100                                 }                                                               
101                         } else {
102                                 System.out.println("Opening REMOTE browser");
103                                 String remoteEnvIP = config.getRemoteTestingMachineIP();
104                                 String remoteEnvPort = config.getRemoteTestingMachinePort();
105                                 
106                                 DesiredCapabilities cap = new DesiredCapabilities();
107                                 cap = DesiredCapabilities.firefox();
108                                 cap.setPlatform(Platform.ANY);
109                                 cap.setBrowserName("firefox");
110                                 
111                                 String remoteNodeUrl = String.format(SELENIUM_NODE_URL, remoteEnvIP, remoteEnvPort);
112                                 RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL(remoteNodeUrl), cap);
113                                 remoteWebDriver.setFileDetector(new LocalFileDetector());
114                                 webdriver = remoteWebDriver;
115                         }
116                         
117
118                 } catch (MalformedURLException e) {
119                         throw new RuntimeException(e);
120                 }
121         }
122
123         private FirefoxProfile initFirefoxProfile() {
124                 firefoxProfile = new FirefoxProfile();
125                 firefoxProfile.setPreference("browser.download.folderList",2);
126                 firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
127                 firefoxProfile.setPreference("browser.download.dir", getDownloadDirectory());
128                 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
129                 return firefoxProfile;
130         }
131
132         private String getDownloadDirectory() {
133                 String downloadDirectory = FileHandling.getBasePath() + File.separator + AUTOMATION_DOWNLOAD_DIR + UUID.randomUUID().toString().split("-")[0] + File.separator;
134                 File dir = new File(downloadDirectory);
135                 if(!dir.exists()) {
136                         dir.mkdirs();
137                 }
138                 return dir.getAbsolutePath();
139         }
140
141         public FirefoxProfile getFirefoxProfile() {
142                 return firefoxProfile;
143         }
144         
145         private void setWebDriverWithMobProxy(){
146                 WebDriver driver = null;
147                 MobProxy.setProxyServer();
148                 BrowserMobProxyServer proxyServer = MobProxy.getPoxyServer();
149                 
150                 firefoxProfile = new FirefoxProfile();
151                 firefoxProfile.setPreference("browser.download.folderList",2);
152                 firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
153                 firefoxProfile.setPreference("browser.download.dir", getDownloadDirectory());
154                 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
155                 firefoxProfile.setAcceptUntrustedCertificates(true);
156                 firefoxProfile.setAssumeUntrustedCertificateIssuer(true);
157 //              firefoxProfile.setPreference("network.proxy.http", "localhost");
158 //              firefoxProfile.setPreference("network.proxy.http_port", proxyServer.getPort());
159 //              firefoxProfile.setPreference("network.proxy.ssl", "localhost");
160 //              firefoxProfile.setPreference("network.proxy.ssl_port", proxyServer.getPort());
161 //              firefoxProfile.setPreference("network.proxy.type", 1);
162 //              firefoxProfile.setPreference("network.proxy.no_proxies_on", "");
163
164         DesiredCapabilities capabilities = new DesiredCapabilities();
165         
166         capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
167         capabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxyServer));
168         capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
169
170         webdriver = new FirefoxDriver(capabilities);
171                 proxyServer.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT, CaptureType.REQUEST_COOKIES, CaptureType.REQUEST_BINARY_CONTENT,
172                                                           CaptureType.REQUEST_HEADERS, CaptureType.RESPONSE_COOKIES, CaptureType.RESPONSE_HEADERS, CaptureType.RESPONSE_BINARY_CONTENT);
173         }
174
175 }