Merge from ecomp 718fd196 - Integration Tests
[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                                         webdriver = new FirefoxDriver(cap);
98                                 }                                                               
99                         } else {
100                                 System.out.println("Opening REMOTE browser");
101                                 String remoteEnvIP = config.getRemoteTestingMachineIP();
102                                 int remoteEnvPort = config.getRemoteTestingMachinePort();
103
104                                 FirefoxOptions cap = new FirefoxOptions();
105                                 cap.setCapability("platform",Platform.ANY);
106                                 cap.setCapability("browserName","firefox");
107                                 cap.setCapability(MARIONETTE_CAPABILITY, true);
108                                 
109                                 String remoteNodeUrl = String.format(SELENIUM_NODE_URL, remoteEnvIP, remoteEnvPort);
110                                 RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL(remoteNodeUrl), cap);
111                                 remoteWebDriver.setFileDetector(new LocalFileDetector());
112                                 webdriver = remoteWebDriver;
113                         }
114                         
115
116                 } catch (MalformedURLException e) {
117                         throw new RuntimeException(e);
118                 }
119         }
120
121         private FirefoxProfile initFirefoxProfile() {
122                 firefoxProfile = new FirefoxProfile();
123                 firefoxProfile.setPreference("browser.download.folderList",2);
124                 firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
125                 firefoxProfile.setPreference("browser.download.dir", getDownloadDirectory());
126                 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
127                 return firefoxProfile;
128         }
129
130         private String getDownloadDirectory() {
131                 String downloadDirectory = FileHandling.getBasePath() + File.separator + AUTOMATION_DOWNLOAD_DIR + UUID.randomUUID().toString().split("-")[0] + File.separator;
132                 File dir = new File(downloadDirectory);
133                 if(!dir.exists()) {
134                         dir.mkdirs();
135                 }
136                 return dir.getAbsolutePath();
137         }
138
139         public FirefoxProfile getFirefoxProfile() {
140                 return firefoxProfile;
141         }
142         
143         private void setWebDriverWithMobProxy(){
144                 MobProxy.setProxyServer();
145                 BrowserMobProxyServer proxyServer = MobProxy.getPoxyServer();
146                 
147                 firefoxProfile = new FirefoxProfile();
148                 firefoxProfile.setPreference("browser.download.folderList",2);
149                 firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
150                 firefoxProfile.setPreference("browser.download.dir", getDownloadDirectory());
151                 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
152                 firefoxProfile.setAcceptUntrustedCertificates(true);
153                 firefoxProfile.setAssumeUntrustedCertificateIssuer(true);
154 //              firefoxProfile.setPreference("network.proxy.http", "localhost");
155 //              firefoxProfile.setPreference("network.proxy.http_port", proxyServer.getPort());
156 //              firefoxProfile.setPreference("network.proxy.ssl", "localhost");
157 //              firefoxProfile.setPreference("network.proxy.ssl_port", proxyServer.getPort());
158 //              firefoxProfile.setPreference("network.proxy.type", 1);
159 //              firefoxProfile.setPreference("network.proxy.no_proxies_on", "");
160
161                 FirefoxOptions capabilities = new FirefoxOptions();
162         
163         capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
164         capabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxyServer));
165         capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
166         capabilities.setCapability(MARIONETTE_CAPABILITY, true);
167
168         webdriver = new FirefoxDriver(capabilities);
169                 proxyServer.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT, CaptureType.REQUEST_COOKIES, CaptureType.REQUEST_BINARY_CONTENT,
170                                                           CaptureType.REQUEST_HEADERS, CaptureType.RESPONSE_COOKIES, CaptureType.RESPONSE_HEADERS, CaptureType.RESPONSE_BINARY_CONTENT);
171         }
172
173 }