[SDC-29] rebase continue work to align source
[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 java.io.File;
24 import java.io.FileNotFoundException;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.util.UUID;
28
29 import org.openecomp.sdc.ci.tests.config.Config;
30 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
31 import org.openecomp.sdc.ci.tests.utils.Utils;
32 import org.openqa.selenium.Platform;
33 import org.openqa.selenium.WebDriver;
34 import org.openqa.selenium.firefox.FirefoxDriver;
35 import org.openqa.selenium.firefox.FirefoxProfile;
36 import org.openqa.selenium.remote.CapabilityType;
37 import org.openqa.selenium.remote.DesiredCapabilities;
38 import org.openqa.selenium.remote.LocalFileDetector;
39 import org.openqa.selenium.remote.RemoteWebDriver;
40
41 import net.lightbody.bmp.BrowserMobProxyServer;
42 import net.lightbody.bmp.client.ClientUtil;
43 import net.lightbody.bmp.proxy.CaptureType;
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                                         System.out.println("Opening LOCAL browser");
78                                         DesiredCapabilities cap = new DesiredCapabilities();
79                                                                         
80                                         cap = DesiredCapabilities.firefox();
81                                         cap.setBrowserName("firefox");
82                                         cap.setCapability(FirefoxDriver.PROFILE, initFirefoxProfile());
83                                         
84                                 firefoxProfile.setPreference("network.proxy.type", 2);
85                                         firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://emea-auto.proxy.att.com:8001/");
86                                         firefoxProfile.setPreference("network.proxy.no_proxies_on", "localhost");
87                                         
88                                         webdriver = new FirefoxDriver(cap);
89                                 }                                                               
90                         } else {
91                                 System.out.println("Opening REMOTE browser");
92                                 String remoteEnvIP = config.getRemoteTestingMachineIP();
93                                 String remoteEnvPort = config.getRemoteTestingMachinePort();
94                                 
95                                 DesiredCapabilities cap = new DesiredCapabilities();
96                                 cap = DesiredCapabilities.firefox();
97                                 cap.setPlatform(Platform.ANY);
98                                 cap.setBrowserName("firefox");
99                                 
100                                 String remoteNodeUrl = String.format(SELENIUM_NODE_URL, remoteEnvIP, remoteEnvPort);
101                                 RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL(remoteNodeUrl), cap);
102                                 remoteWebDriver.setFileDetector(new LocalFileDetector());
103                                 webdriver = remoteWebDriver;
104                         }
105                         
106
107                 } catch (MalformedURLException e) {
108                         throw new RuntimeException(e);
109                 }
110         }
111
112         private FirefoxProfile initFirefoxProfile() {
113                 firefoxProfile = new FirefoxProfile();
114                 firefoxProfile.setPreference("browser.download.folderList",2);
115                 firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
116                 firefoxProfile.setPreference("browser.download.dir", getDownloadDirectory());
117                 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
118                 return firefoxProfile;
119         }
120
121         private String getDownloadDirectory() {
122                 String downloadDirectory = FileHandling.getBasePath() + File.separator + AUTOMATION_DOWNLOAD_DIR + UUID.randomUUID().toString().split("-")[0] + File.separator;
123                 File dir = new File(downloadDirectory);
124                 if(!dir.exists()) {
125                         dir.mkdirs();
126                 }
127                 return dir.getAbsolutePath();
128         }
129
130         public FirefoxProfile getFirefoxProfile() {
131                 return firefoxProfile;
132         }
133         
134         private void setWebDriverWithMobProxy(){
135                 WebDriver driver = null;
136                 MobProxy.setProxyServer();
137                 BrowserMobProxyServer proxyServer = MobProxy.getPoxyServer();
138                 
139                 firefoxProfile = new FirefoxProfile();
140                 firefoxProfile.setPreference("browser.download.folderList",2);
141                 firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
142                 firefoxProfile.setPreference("browser.download.dir", getDownloadDirectory());
143                 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
144                 firefoxProfile.setAcceptUntrustedCertificates(true);
145                 firefoxProfile.setAssumeUntrustedCertificateIssuer(true);
146 //              firefoxProfile.setPreference("network.proxy.http", "localhost");
147 //              firefoxProfile.setPreference("network.proxy.http_port", proxyServer.getPort());
148 //              firefoxProfile.setPreference("network.proxy.ssl", "localhost");
149 //              firefoxProfile.setPreference("network.proxy.ssl_port", proxyServer.getPort());
150 //              firefoxProfile.setPreference("network.proxy.type", 1);
151 //              firefoxProfile.setPreference("network.proxy.no_proxies_on", "");
152
153         DesiredCapabilities capabilities = new DesiredCapabilities();
154         
155         capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
156         capabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxyServer));
157         capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
158
159         webdriver = new FirefoxDriver(capabilities);
160                 proxyServer.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT, CaptureType.REQUEST_COOKIES, CaptureType.REQUEST_BINARY_CONTENT,
161                                                           CaptureType.REQUEST_HEADERS, CaptureType.RESPONSE_COOKIES, CaptureType.RESPONSE_HEADERS, CaptureType.RESPONSE_BINARY_CONTENT);
162         }
163
164 }