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 org.apache.commons.io.FileUtils;
25 import org.openecomp.sdc.ci.tests.config.Config;
26 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
27 import org.openecomp.sdc.ci.tests.utils.Utils;
28 import org.openqa.selenium.WebDriver;
29 import org.openqa.selenium.firefox.FirefoxProfile;
30 import org.testng.annotations.AfterSuite;
31 import org.testng.annotations.BeforeSuite;
34 import java.io.FileNotFoundException;
35 import java.io.FilenameFilter;
36 import java.io.IOException;
40 public class DriverFactory {
42 private static ThreadLocal<WebDriverThread> driverThread;
43 private static List<WebDriverThread> webDriverThreadPool = Collections.synchronizedList(new ArrayList<WebDriverThread>());
44 private static Config config;
46 public DriverFactory() {
48 config = Utils.getConfig();
49 } catch (FileNotFoundException e) {
54 @BeforeSuite(alwaysRun = true)
55 public static void instantiateDriverObject() {
57 // Selenium 3.4.0 change, location of gecko driver, set system property
58 // System.setProperty("webdriver.gecko.driver","C:\\Gekko18\\geckodriver-v0.18.0-win64\\geckodriver.exe"); //change for 3.4.0, gecko driver location
59 // End of Selenium 3.4.0 change
61 File basePath = new File(FileHandling.getBasePath());
62 File[] listFiles = basePath.listFiles(new FilenameFilter() {
65 public boolean accept(File basePath, String name) {
66 return name.startsWith(WebDriverThread.AUTOMATION_DOWNLOAD_DIR);
69 Arrays.asList(listFiles).forEach(e -> FileHandling.deleteDirectory(e.getAbsolutePath()));
73 driverThread = new ThreadLocal<WebDriverThread>() {
75 protected WebDriverThread initialValue() {
76 WebDriverThread webDriverThread = new WebDriverThread(config);
77 webDriverThreadPool.add(webDriverThread);
78 return webDriverThread;
83 public static WebDriver getDriver() throws Exception {
84 return driverThread.get().getDriver();
87 public static FirefoxProfile getDriverFirefoxProfile() throws Exception {
88 return driverThread.get().getFirefoxProfile();
91 @AfterSuite(alwaysRun = true)
92 public static void quitDriverAfterSuite() throws Exception {
93 for (WebDriverThread webDriverThread : webDriverThreadPool) {
94 if (webDriverThread.getDriver() != null)
95 webDriverThread.quitDriver();
98 MobProxy.removeAllProxyServers();
100 deleteDownloadDirs();
103 private static void deleteDownloadDirs() throws IOException {
105 HashMap<Long,WindowTest> windowMap = WindowTestManager.getWholeMap();
106 for (WindowTest win : windowMap.values()){
107 String downloadDirectory = win.getDownloadDirectory();
108 FileUtils.deleteDirectory(new File(downloadDirectory));
112 public static void quitDriver() throws Exception{
113 driverThread.get().quitDriver();
114 driverThread.remove();
115 WindowTestManager.removeWindowTest();
116 MobProxy.removePoxyServer();
119 public static Config getConfig() {
123 public static void setConfig(Config config) {
124 DriverFactory.config = config;