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;
23 import net.lightbody.bmp.BrowserMobProxyServer;
24 import net.lightbody.bmp.client.ClientUtil;
25 import net.lightbody.bmp.proxy.CaptureType;
26 import org.openecomp.sdc.ci.tests.config.Config;
27 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
28 import org.openqa.selenium.Platform;
29 import org.openqa.selenium.UnexpectedAlertBehaviour;
30 import org.openqa.selenium.WebDriver;
31 import org.openqa.selenium.firefox.FirefoxDriver;
32 import org.openqa.selenium.firefox.FirefoxProfile;
33 import org.openqa.selenium.remote.CapabilityType;
34 import org.openqa.selenium.remote.DesiredCapabilities;
35 import org.openqa.selenium.remote.LocalFileDetector;
36 import org.openqa.selenium.remote.RemoteWebDriver;
39 import java.net.MalformedURLException;
41 import java.util.UUID;
43 //import org.openqa.selenium.firefox.FirefoxOptions; // Selenium 3.4.0 change
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();
78 // Selenium 3.4.0 change, add firefox options : set, add firefox 5.X location and enable
79 // FirefoxOptions options = new FirefoxOptions();
80 // options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox5\\firefox.exe"); //Location where Firefox is installed
81 // options.addPreference("dom.file.createInChild", true); // Enable file upload with sendKeys
82 // End of Selenium 3.4.0 change
84 System.out.println("Opening LOCAL browser");
85 DesiredCapabilities cap = new DesiredCapabilities();
87 cap = DesiredCapabilities.firefox();
88 cap.setBrowserName("firefox");
89 cap.setCapability(FirefoxDriver.PROFILE, initFirefoxProfile());
90 //unexpected model dialog fix.
91 cap.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
92 // cap.setCapability("moz:firefoxOptions", options); //Add options to Capabilities, Selenium 3.4.0 change
94 firefoxProfile.setPreference("network.proxy.type", 2);
95 // firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://emea-auto.proxy.att.com:8001/");
96 firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://autoproxy.sbc.com/autoproxy.cgi");
97 firefoxProfile.setPreference("network.proxy.no_proxies_on", "localhost");
99 webdriver = new FirefoxDriver(cap);
102 System.out.println("Opening REMOTE browser");
103 String remoteEnvIP = config.getRemoteTestingMachineIP();
104 String remoteEnvPort = config.getRemoteTestingMachinePort();
106 DesiredCapabilities cap = new DesiredCapabilities();
107 cap = DesiredCapabilities.firefox();
108 cap.setPlatform(Platform.ANY);
109 cap.setBrowserName("firefox");
111 String remoteNodeUrl = String.format(SELENIUM_NODE_URL, remoteEnvIP, remoteEnvPort);
112 RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL(remoteNodeUrl), cap);
113 remoteWebDriver.setFileDetector(new LocalFileDetector());
114 webdriver = remoteWebDriver;
118 } catch (MalformedURLException e) {
119 throw new RuntimeException(e);
123 private FirefoxProfile initFirefoxProfile() {
124 firefoxProfile = new FirefoxProfile();
125 firefoxProfile.setPreference("browser.download.folderList",2);
126 firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
127 firefoxProfile.setPreference("browser.download.dir", getDownloadDirectory());
128 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
129 return firefoxProfile;
132 private String getDownloadDirectory() {
133 String downloadDirectory = FileHandling.getBasePath() + File.separator + AUTOMATION_DOWNLOAD_DIR + UUID.randomUUID().toString().split("-")[0] + File.separator;
134 File dir = new File(downloadDirectory);
138 return dir.getAbsolutePath();
141 public FirefoxProfile getFirefoxProfile() {
142 return firefoxProfile;
145 private void setWebDriverWithMobProxy(){
146 WebDriver driver = null;
147 MobProxy.setProxyServer();
148 BrowserMobProxyServer proxyServer = MobProxy.getPoxyServer();
150 firefoxProfile = new FirefoxProfile();
151 firefoxProfile.setPreference("browser.download.folderList",2);
152 firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
153 firefoxProfile.setPreference("browser.download.dir", getDownloadDirectory());
154 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
155 firefoxProfile.setAcceptUntrustedCertificates(true);
156 firefoxProfile.setAssumeUntrustedCertificateIssuer(true);
157 // firefoxProfile.setPreference("network.proxy.http", "localhost");
158 // firefoxProfile.setPreference("network.proxy.http_port", proxyServer.getPort());
159 // firefoxProfile.setPreference("network.proxy.ssl", "localhost");
160 // firefoxProfile.setPreference("network.proxy.ssl_port", proxyServer.getPort());
161 // firefoxProfile.setPreference("network.proxy.type", 1);
162 // firefoxProfile.setPreference("network.proxy.no_proxies_on", "");
164 DesiredCapabilities capabilities = new DesiredCapabilities();
166 capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
167 capabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxyServer));
168 capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
170 webdriver = new FirefoxDriver(capabilities);
171 proxyServer.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT, CaptureType.REQUEST_COOKIES, CaptureType.REQUEST_BINARY_CONTENT,
172 CaptureType.REQUEST_HEADERS, CaptureType.RESPONSE_COOKIES, CaptureType.RESPONSE_HEADERS, CaptureType.RESPONSE_BINARY_CONTENT);