Catalog alignment
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / setup / WebDriverThread.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.execute.setup;
22
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;
40
41 import java.io.File;
42 import java.net.MalformedURLException;
43 import java.net.URL;
44 import java.util.UUID;
45
46 public class WebDriverThread {
47
48     private final static Logger LOGGER = LoggerFactory.getLogger(SetupCDTest.class);
49
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";
54
55     WebDriverThread(Config config) {
56         initDriver(config);
57         webdriver.manage().window().maximize();
58     }
59
60     public WebDriver getDriver() {
61         return webdriver;
62     }
63
64     void quitDriver() {
65         if (webdriver != null) {
66             webdriver.quit();
67             webdriver = null;
68         }
69     }
70
71
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();
77
78             final DesiredCapabilities cap = DesiredCapabilities.firefox();
79             cap.setPlatform(Platform.ANY);
80             cap.setBrowserName("firefox");
81
82             final String remoteUrlString = String.format(SELENIUM_NODE_URL, remoteEnvIP, remoteEnvPort);
83             final URL remoteUrl;
84             try {
85                 remoteUrl = new URL(remoteUrlString);
86             } catch (MalformedURLException e) {
87                 throw new WebDriverThreadRuntimeException(String.format("Malformed URL '%s'", remoteUrlString), e);
88             }
89             final RemoteWebDriver remoteWebDriver = new RemoteWebDriver(remoteUrl, cap);
90             remoteWebDriver.setFileDetector(new LocalFileDetector());
91             webdriver = remoteWebDriver;
92         } else {
93             if (config.getUseBrowserMobProxy()) {
94                 setWebDriverWithMobProxy();
95                 return;
96             }
97
98             LOGGER.info("Opening LOCAL browser");
99
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);
105
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");
109
110             webdriver = new FirefoxDriver(cap);
111         }
112     }
113
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;
121     }
122
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);
126         if (!dir.exists()) {
127             dir.mkdirs();
128         }
129         return dir.getAbsolutePath();
130     }
131
132     FirefoxProfile getFirefoxProfile() {
133         return firefoxProfile;
134     }
135
136     private void setWebDriverWithMobProxy() {
137         WebDriver driver = null;
138         MobProxy.setProxyServer();
139         BrowserMobProxyServer proxyServer = MobProxy.getPoxyServer();
140
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);
148
149         DesiredCapabilities capabilities = new DesiredCapabilities();
150
151         capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
152         capabilities.setCapability(CapabilityType.PROXY, ClientUtil.createSeleniumProxy(proxyServer));
153         capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
154
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);
158     }
159
160 }