2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdc.ci.tests.execute.setup;
24 import java.net.MalformedURLException;
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;
43 public class WebDriverThread {
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";
51 public WebDriverThread(Configuration config) {
54 webdriver.manage().window().setSize(new Dimension(1920, 1080));
56 webdriver.manage().window().maximize();
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;
70 public WebDriver getDriver() throws Exception {
74 public void quitDriver() {
75 if (webdriver != null) {
82 public void initDriver(Configuration config){
84 boolean remoteTesting = config.isRemoteTesting();
86 boolean mobProxyStatus = config.isUseBrowserMobProxy();
88 setWebDriverWithMobProxy();
90 System.out.println("Opening LOCAL browser");
91 FirefoxOptions cap = new FirefoxOptions();
93 cap.setCapability("browserName","firefox");
94 cap.setCapability(FirefoxDriver.PROFILE, initFirefoxProfile());
95 cap.setCapability(MARIONETTE_CAPABILITY, true);
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");
101 webdriver = new FirefoxDriver(cap);
104 System.out.println("Opening REMOTE browser");
105 String remoteEnvIP = config.getRemoteTestingMachineIP();
106 int remoteEnvPort = config.getRemoteTestingMachinePort();
108 FirefoxOptions cap = new FirefoxOptions();
109 cap.setCapability("platform",Platform.ANY);
110 cap.setCapability("browserName","firefox");
111 cap.setCapability(MARIONETTE_CAPABILITY, true);
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;
120 } catch (MalformedURLException e) {
121 throw new RuntimeException(e);
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;
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);
140 return dir.getAbsolutePath();
143 public FirefoxProfile getFirefoxProfile() {
144 return firefoxProfile;
147 private void setWebDriverWithMobProxy(){
148 MobProxy.setProxyServer();
149 BrowserMobProxyServer proxyServer = MobProxy.getPoxyServer();
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", "");
165 FirefoxOptions capabilities = new FirefoxOptions();
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);
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);