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.exception.WebDriverThreadRuntimeException;
28 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
29 import org.openqa.selenium.Platform;
30 import org.openqa.selenium.UnexpectedAlertBehaviour;
31 import org.openqa.selenium.WebDriver;
32 import org.openqa.selenium.firefox.FirefoxDriver;
33 import org.openqa.selenium.firefox.FirefoxProfile;
34 import org.openqa.selenium.remote.CapabilityType;
35 import org.openqa.selenium.remote.DesiredCapabilities;
36 import org.openqa.selenium.remote.LocalFileDetector;
37 import org.openqa.selenium.remote.RemoteWebDriver;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 import java.net.MalformedURLException;
44 import java.util.UUID;
46 public class WebDriverThread {
48 private final static Logger LOGGER = LoggerFactory.getLogger(SetupCDTest.class);
50 static final String AUTOMATION_DOWNLOAD_DIR = "automationDownloadDir";
51 private WebDriver webdriver;
52 private FirefoxProfile firefoxProfile;
53 private static final String SELENIUM_NODE_URL = "http://%s:%s/wd/hub";
55 WebDriverThread(Config config) {
57 webdriver.manage().window().maximize();
60 public WebDriver getDriver() {
65 if (webdriver != null) {
72 private void initDriver(final Config config) {
73 if (config.isRemoteTesting()) {
74 LOGGER.info("Opening REMOTE browser");
75 final String remoteEnvIP = config.getRemoteTestingMachineIP();
76 final String remoteEnvPort = config.getRemoteTestingMachinePort();
78 final DesiredCapabilities cap = DesiredCapabilities.firefox();
79 cap.setPlatform(Platform.ANY);
80 cap.setBrowserName("firefox");
82 final String remoteUrlString = String.format(SELENIUM_NODE_URL, remoteEnvIP, remoteEnvPort);
85 remoteUrl = new URL(remoteUrlString);
86 } catch (MalformedURLException e) {
87 throw new WebDriverThreadRuntimeException(String.format("Malformed URL '%s'", remoteUrlString), e);
89 final RemoteWebDriver remoteWebDriver = new RemoteWebDriver(remoteUrl, cap);
90 remoteWebDriver.setFileDetector(new LocalFileDetector());
91 webdriver = remoteWebDriver;
93 if (config.getUseBrowserMobProxy()) {
94 setWebDriverWithMobProxy();
98 LOGGER.info("Opening LOCAL browser");
100 final DesiredCapabilities cap = DesiredCapabilities.firefox();
101 cap.setBrowserName("firefox");
102 cap.setCapability(FirefoxDriver.PROFILE, initFirefoxProfile());
103 //unexpected model dialog fix.
104 cap.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
106 firefoxProfile.setPreference("network.proxy.type", 2);
107 firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://autoproxy.sbc.com/autoproxy.cgi");
108 firefoxProfile.setPreference("network.proxy.no_proxies_on", "localhost");
110 webdriver = new FirefoxDriver(cap);
114 private FirefoxProfile initFirefoxProfile() {
115 firefoxProfile = new FirefoxProfile();
116 firefoxProfile.setPreference("browser.download.folderList", 2);
117 firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
118 firefoxProfile.setPreference("browser.download.dir", getDownloadDirectory());
119 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
120 return firefoxProfile;
123 private String getDownloadDirectory() {
124 String downloadDirectory = FileHandling.getBasePath() + File.separator + AUTOMATION_DOWNLOAD_DIR + UUID.randomUUID().toString().split("-")[0] + File.separator;
125 File dir = new File(downloadDirectory);
129 return dir.getAbsolutePath();
132 FirefoxProfile getFirefoxProfile() {
133 return firefoxProfile;
136 private void setWebDriverWithMobProxy() {
137 WebDriver driver = null;
138 MobProxy.setProxyServer();
139 BrowserMobProxyServer proxyServer = MobProxy.getPoxyServer();
141 firefoxProfile = new FirefoxProfile();
142 firefoxProfile.setPreference("browser.download.folderList", 2);
143 firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
144 firefoxProfile.setPreference("browser.download.dir", getDownloadDirectory());
145 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
146 firefoxProfile.setAcceptUntrustedCertificates(true);
147 firefoxProfile.setAssumeUntrustedCertificateIssuer(true);
149 DesiredCapabilities capabilities = new DesiredCapabilities();
151 capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
152 capabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxyServer));
153 capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
155 webdriver = new FirefoxDriver(capabilities);
156 proxyServer.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT, CaptureType.REQUEST_COOKIES, CaptureType.REQUEST_BINARY_CONTENT,
157 CaptureType.REQUEST_HEADERS, CaptureType.RESPONSE_COOKIES, CaptureType.RESPONSE_HEADERS, CaptureType.RESPONSE_BINARY_CONTENT);