501faa8de866b1e2ed97ebc34acb0a1ff7e1226f
[vid.git] / vid-automation / src / main / java / org / onap / 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.onap.sdc.ci.tests.execute.setup;
22
23 import java.io.File;
24 import java.net.MalformedURLException;
25 import java.net.URL;
26 import java.util.UUID;
27 import net.lightbody.bmp.BrowserMobProxyServer;
28 import net.lightbody.bmp.client.ClientUtil;
29 import net.lightbody.bmp.proxy.CaptureType;
30 import org.onap.sdc.ci.tests.datatypes.Configuration;
31 import org.onap.sdc.ci.tests.utilities.FileHandling;
32 import org.openqa.selenium.Capabilities;
33 import org.openqa.selenium.Dimension;
34 import org.openqa.selenium.Platform;
35 import org.openqa.selenium.WebDriver;
36 import org.openqa.selenium.firefox.FirefoxDriver;
37 import org.openqa.selenium.firefox.FirefoxOptions;
38 import org.openqa.selenium.firefox.FirefoxProfile;
39 import org.openqa.selenium.remote.CapabilityType;
40 import org.openqa.selenium.remote.LocalFileDetector;
41 import org.openqa.selenium.remote.RemoteWebDriver;
42
43 public class WebDriverThread {
44
45         public static final String AUTOMATION_DOWNLOAD_DIR = "automationDownloadDir";
46         private WebDriver webdriver;
47         private FirefoxProfile firefoxProfile;
48         public static final String SELENIUM_NODE_URL = "http://%s:%s/wd/hub";
49         public static final String MARIONETTE_CAPABILITY= "marionette";
50         
51         public WebDriverThread(Configuration config) {
52                 initDriver(config);
53                 if (isHeadless()) {
54                         webdriver.manage().window().setSize(new Dimension(1920, 1080));
55                 } else {
56                         webdriver.manage().window().maximize();
57                 }
58         }
59
60         private boolean isHeadless() {
61                 if (webdriver instanceof RemoteWebDriver) {
62                         Capabilities capabilities = ((RemoteWebDriver) webdriver).getCapabilities();
63                         Object headless = capabilities.getCapability("moz:headless");
64                         return Boolean.TRUE == headless;
65                 } else {
66                         return false;
67                 }
68         }
69
70         public WebDriver getDriver() throws Exception {
71                 return webdriver;
72         }
73         
74         public void quitDriver() {
75                 if (webdriver != null) {
76                         webdriver.quit();
77                         webdriver = null;
78                 }
79         }
80         
81         
82         public void initDriver(Configuration config){
83                 try {
84                         boolean remoteTesting = config.isRemoteTesting();
85                         if (!remoteTesting) {
86                                 boolean mobProxyStatus = config.isUseBrowserMobProxy();
87                                 if (mobProxyStatus){
88                                         setWebDriverWithMobProxy();
89                                 } else {
90                                         System.out.println("Opening LOCAL browser");
91                                         FirefoxOptions cap = new FirefoxOptions();
92
93                                         cap.setCapability("browserName","firefox");
94                                         cap.setCapability(FirefoxDriver.PROFILE, initFirefoxProfile());
95                                         cap.setCapability(MARIONETTE_CAPABILITY, true);
96                                         
97                                 firefoxProfile.setPreference("network.proxy.type", 2);
98                                         firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://emea.onap.org:8001/");
99                                         firefoxProfile.setPreference("network.proxy.no_proxies_on", "localhost");
100                                         
101                                         webdriver = new FirefoxDriver(cap);
102                                 }                                                               
103                         } else {
104                                 System.out.println("Opening REMOTE browser");
105                                 String remoteEnvIP = config.getRemoteTestingMachineIP();
106                                 int remoteEnvPort = config.getRemoteTestingMachinePort();
107
108                                 FirefoxOptions cap = new FirefoxOptions();
109                                 cap.setCapability("platform",Platform.ANY);
110                                 cap.setCapability("browserName","firefox");
111                                 cap.setCapability(MARIONETTE_CAPABILITY, true);
112                                 
113                                 String remoteNodeUrl = String.format(SELENIUM_NODE_URL, remoteEnvIP, remoteEnvPort);
114                                 RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL(remoteNodeUrl), cap);
115                                 remoteWebDriver.setFileDetector(new LocalFileDetector());
116                                 webdriver = remoteWebDriver;
117                         }
118                         
119
120                 } catch (MalformedURLException e) {
121                         throw new RuntimeException(e);
122                 }
123         }
124
125         private FirefoxProfile initFirefoxProfile() {
126                 firefoxProfile = new FirefoxProfile();
127                 firefoxProfile.setPreference("browser.download.folderList",2);
128                 firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
129                 firefoxProfile.setPreference("browser.download.dir", getDownloadDirectory());
130                 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
131                 return firefoxProfile;
132         }
133
134         private String getDownloadDirectory() {
135                 String downloadDirectory = FileHandling.getBasePath() + File.separator + AUTOMATION_DOWNLOAD_DIR + UUID.randomUUID().toString().split("-")[0] + File.separator;
136                 File dir = new File(downloadDirectory);
137                 if(!dir.exists()) {
138                         dir.mkdirs();
139                 }
140                 return dir.getAbsolutePath();
141         }
142
143         public FirefoxProfile getFirefoxProfile() {
144                 return firefoxProfile;
145         }
146         
147         private void setWebDriverWithMobProxy(){
148                 MobProxy.setProxyServer();
149                 BrowserMobProxyServer proxyServer = MobProxy.getPoxyServer();
150                 
151                 firefoxProfile = new FirefoxProfile();
152                 firefoxProfile.setPreference("browser.download.folderList",2);
153                 firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
154                 firefoxProfile.setPreference("browser.download.dir", getDownloadDirectory());
155                 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
156                 firefoxProfile.setAcceptUntrustedCertificates(true);
157                 firefoxProfile.setAssumeUntrustedCertificateIssuer(true);
158 //              firefoxProfile.setPreference("network.proxy.http", "localhost");
159 //              firefoxProfile.setPreference("network.proxy.http_port", proxyServer.getPort());
160 //              firefoxProfile.setPreference("network.proxy.ssl", "localhost");
161 //              firefoxProfile.setPreference("network.proxy.ssl_port", proxyServer.getPort());
162 //              firefoxProfile.setPreference("network.proxy.type", 1);
163 //              firefoxProfile.setPreference("network.proxy.no_proxies_on", "");
164
165                 FirefoxOptions capabilities = new FirefoxOptions();
166         
167         capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
168         capabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxyServer));
169         capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
170         capabilities.setCapability(MARIONETTE_CAPABILITY, true);
171
172         webdriver = new FirefoxDriver(capabilities);
173                 proxyServer.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT, CaptureType.REQUEST_COOKIES, CaptureType.REQUEST_BINARY_CONTENT,
174                                                           CaptureType.REQUEST_HEADERS, CaptureType.RESPONSE_COOKIES, CaptureType.RESPONSE_HEADERS, CaptureType.RESPONSE_BINARY_CONTENT);
175         }
176
177 }