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 webdriver = new FirefoxDriver(cap);
100 System.out.println("Opening REMOTE browser");
101 String remoteEnvIP = config.getRemoteTestingMachineIP();
102 int remoteEnvPort = config.getRemoteTestingMachinePort();
104 FirefoxOptions cap = new FirefoxOptions();
105 cap.setCapability("platform",Platform.ANY);
106 cap.setCapability("browserName","firefox");
107 cap.setCapability(MARIONETTE_CAPABILITY, true);
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;
116 } catch (MalformedURLException e) {
117 throw new RuntimeException(e);
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;
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);
136 return dir.getAbsolutePath();
139 public FirefoxProfile getFirefoxProfile() {
140 return firefoxProfile;
143 private void setWebDriverWithMobProxy(){
144 MobProxy.setProxyServer();
145 BrowserMobProxyServer proxyServer = MobProxy.getPoxyServer();
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", "");
161 FirefoxOptions capabilities = new FirefoxOptions();
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);
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);