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.openecomp.sdc.ci.tests.execute.setup;
24 import java.io.FileNotFoundException;
25 import java.net.MalformedURLException;
27 import java.util.UUID;
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;
41 import net.lightbody.bmp.BrowserMobProxyServer;
42 import net.lightbody.bmp.client.ClientUtil;
43 import net.lightbody.bmp.proxy.CaptureType;
45 public class WebDriverThread {
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";
52 public WebDriverThread(Config config) {
54 webdriver.manage().window().maximize();
57 public WebDriver getDriver() throws Exception {
61 public void quitDriver() {
62 if (webdriver != null) {
69 public void initDriver(Config config){
71 boolean remoteTesting = config.isRemoteTesting();
73 boolean mobProxyStatus = config.getUseBrowserMobProxy();
75 setWebDriverWithMobProxy();
77 System.out.println("Opening LOCAL browser");
78 DesiredCapabilities cap = new DesiredCapabilities();
80 cap = DesiredCapabilities.firefox();
81 cap.setBrowserName("firefox");
82 cap.setCapability(FirefoxDriver.PROFILE, initFirefoxProfile());
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");
88 webdriver = new FirefoxDriver(cap);
91 System.out.println("Opening REMOTE browser");
92 String remoteEnvIP = config.getRemoteTestingMachineIP();
93 String remoteEnvPort = config.getRemoteTestingMachinePort();
95 DesiredCapabilities cap = new DesiredCapabilities();
96 cap = DesiredCapabilities.firefox();
97 cap.setPlatform(Platform.ANY);
98 cap.setBrowserName("firefox");
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;
107 } catch (MalformedURLException e) {
108 throw new RuntimeException(e);
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;
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);
127 return dir.getAbsolutePath();
130 public FirefoxProfile getFirefoxProfile() {
131 return firefoxProfile;
134 private void setWebDriverWithMobProxy(){
135 WebDriver driver = null;
136 MobProxy.setProxyServer();
137 BrowserMobProxyServer proxyServer = MobProxy.getPoxyServer();
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", "");
153 DesiredCapabilities capabilities = new DesiredCapabilities();
155 capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
156 capabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxyServer));
157 capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
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);