4d08e846e893417ed1bbf54179e6ea4ff6b782e1
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / 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.onap.sdc.frontend.ci.tests.execute.setup;
22
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import org.onap.sdc.backend.ci.tests.config.Config;
26 import org.onap.sdc.frontend.ci.tests.exception.WebDriverThreadRuntimeException;
27 import org.openqa.selenium.Dimension;
28 import org.openqa.selenium.WebDriver;
29 import org.openqa.selenium.firefox.FirefoxDriver;
30 import org.openqa.selenium.firefox.FirefoxOptions;
31 import org.openqa.selenium.firefox.FirefoxProfile;
32 import org.openqa.selenium.remote.CapabilityType;
33 import org.openqa.selenium.remote.LocalFileDetector;
34 import org.openqa.selenium.remote.RemoteWebDriver;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class WebDriverThread {
39
40     private final static Logger LOGGER = LoggerFactory.getLogger(SetupCDTest.class);
41
42     private WebDriver webdriver;
43     private FirefoxProfile firefoxProfile;
44     private static final String SELENIUM_NODE_URL = "http://%s:%s/wd/hub";
45
46     WebDriverThread(Config config) {
47         initDriver(config);
48         webdriver.manage().window().maximize();
49     }
50
51     public WebDriver getDriver() {
52         return webdriver;
53     }
54
55     void quitDriver() {
56         if (webdriver != null) {
57             webdriver.quit();
58             webdriver = null;
59         }
60     }
61
62
63     private void initDriver(final Config config) {
64         if (config.isRemoteTesting()) {
65             LOGGER.info("Opening REMOTE browser");
66             final String remoteEnvIP = config.getRemoteTestingMachineIP();
67             final String remoteEnvPort = config.getRemoteTestingMachinePort();
68             FirefoxOptions firefoxOptions = new FirefoxOptions();
69             firefoxOptions.setProfile(initFirefoxProfile(config));
70             firefoxOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
71             final String remoteUrlString = String.format(SELENIUM_NODE_URL, remoteEnvIP, remoteEnvPort);
72             final URL remoteUrl;
73             try {
74                 remoteUrl = new URL(remoteUrlString);
75             } catch (MalformedURLException e) {
76                 throw new WebDriverThreadRuntimeException(String.format("Malformed URL '%s'", remoteUrlString), e);
77             }
78             final RemoteWebDriver remoteWebDriver = new RemoteWebDriver(remoteUrl, firefoxOptions);
79             remoteWebDriver.setFileDetector(new LocalFileDetector());
80             remoteWebDriver.manage().window().setSize(new Dimension(1920,1440));
81             webdriver = remoteWebDriver;
82
83         } else {
84             LOGGER.info("Opening LOCAL browser");
85             System.setProperty("webdriver.gecko.driver", "target/gecko/geckodriver");
86             FirefoxOptions firefoxOptions = new FirefoxOptions();
87             final FirefoxProfile firefoxProfile = initFirefoxProfile(config);
88             firefoxProfile.setPreference("browser.download.dir", config.getDownloadAutomationFolder());
89             firefoxProfile.setPreference("browser.download.folderList", 2);
90             firefoxOptions.setProfile(firefoxProfile);
91             firefoxOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
92             firefoxOptions.setHeadless(false);
93             webdriver = new FirefoxDriver(firefoxOptions);
94             webdriver.manage().window().maximize();
95         }
96     }
97
98     private FirefoxProfile initFirefoxProfile(Config config) {
99         firefoxProfile = new FirefoxProfile();
100         firefoxProfile.setPreference("browser.download.folderList", 0);
101         //firefoxProfile.setPreference("browser.alwaysOpenInSystemViewerContextMenuItem", false);
102         //firefoxProfile.setPreference("browser.download.useDownloadDir", false);
103         //firefoxProfile.setPreference("browser.download.downloadDir", config.getContainerDownloadAutomationFolder());
104         //firefoxProfile.setPreference("browser.download.dir", config.getContainerDownloadAutomationFolder());
105         //firefoxProfile.setPreference("app.update.notifyDuringDownload", false);
106         //firefoxProfile.setPreference("browser.download.lastDir", config.getContainerDownloadAutomationFolder());
107         firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/xml, text/plain, text/xml, image/jpeg");
108         firefoxProfile.setPreference("network.proxy.type", 4);
109         firefoxProfile.setAcceptUntrustedCertificates(true);
110         firefoxProfile.setAssumeUntrustedCertificateIssuer(true);
111
112         return firefoxProfile;
113     }
114
115     FirefoxProfile getFirefoxProfile() {
116         return firefoxProfile;
117     }
118 }