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.onap.sdc.ci.tests.execute.setup;
25 import java.io.FilenameFilter;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
33 import org.apache.commons.io.FileUtils;
34 import org.onap.sdc.ci.tests.datatypes.Configuration;
35 import org.onap.sdc.ci.tests.datatypes.UserCredentials;
36 import org.onap.sdc.ci.tests.utilities.FileHandling;
37 import org.openqa.selenium.WebDriver;
38 import org.openqa.selenium.firefox.FirefoxProfile;
39 import org.testng.annotations.AfterSuite;
40 import org.testng.annotations.BeforeSuite;
43 public abstract class DriverFactory {
45 private static ThreadLocal<WebDriverThread> driverThread;
46 private static List<WebDriverThread> webDriverThreadPool = Collections.synchronizedList(new ArrayList<WebDriverThread>());
47 private static Configuration configuration;
48 private static final String GECKO_DRIVER_KEY = "webdriver.gecko.driver";
50 protected abstract UserCredentials getUserCredentials();
51 protected abstract Configuration getEnvConfiguration();
53 public DriverFactory() {
56 configuration = getEnvConfiguration();
58 //Set geko driver path for firefox
59 if(configuration.getGeckoDriverPath() != null){
60 System.setProperty(GECKO_DRIVER_KEY, configuration.getGeckoDriverPath());
62 throw new RuntimeException("Gecko driver path is null. Make sure to provide path to the gecko driver executable");
64 } catch (Exception e) {
69 @BeforeSuite(alwaysRun = true)
70 public static void instantiateDriverObject() {
73 File basePath = new File(FileHandling.getBasePath());
74 File[] listFiles = basePath.listFiles(new FilenameFilter() {
77 public boolean accept(File basePath, String name) {
78 return name.startsWith(WebDriverThread.AUTOMATION_DOWNLOAD_DIR);
81 Arrays.asList(listFiles).forEach(e -> FileHandling.deleteDirectory(e.getAbsolutePath()));
85 driverThread = new ThreadLocal<WebDriverThread>() {
87 protected WebDriverThread initialValue() {
88 WebDriverThread webDriverThread = new WebDriverThread(configuration);
89 webDriverThreadPool.add(webDriverThread);
90 return webDriverThread;
95 public static WebDriver getDriver() throws Exception {
96 return driverThread.get().getDriver();
99 public static FirefoxProfile getDriverFirefoxProfile() throws Exception {
100 return driverThread.get().getFirefoxProfile();
103 @AfterSuite(alwaysRun = true)
104 public static void quitDriverAfterSuite() throws Exception {
105 for (WebDriverThread webDriverThread : webDriverThreadPool) {
106 if (webDriverThread.getDriver() != null)
107 webDriverThread.quitDriver();
109 deleteDownloadDirs();
112 private static void deleteDownloadDirs() throws IOException {
114 HashMap<Long,WindowTest> windowMap = WindowTestManager.getWholeMap();
115 for (WindowTest win : windowMap.values()){
116 String downloadDirectory = win.getDownloadDirectory();
117 FileUtils.deleteDirectory(new File(downloadDirectory));
121 public static void quitDriver() throws Exception{
122 driverThread.get().quitDriver();
123 driverThread.remove();
124 WindowTestManager.removeWindowTest();
126 public static Configuration getConfiguration() {
127 return configuration;