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.FileNotFoundException;
26 import java.io.FilenameFilter;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.List;
34 import org.apache.commons.io.FileUtils;
35 import org.onap.sdc.ci.tests.datatypes.Configuration;
36 import org.onap.sdc.ci.tests.datatypes.UserCredentials;
37 import org.onap.sdc.ci.tests.utilities.FileHandling;
38 import org.openqa.selenium.WebDriver;
39 import org.openqa.selenium.firefox.FirefoxProfile;
40 import org.testng.annotations.AfterSuite;
41 import org.testng.annotations.BeforeSuite;
44 public abstract class DriverFactory {
46 private static ThreadLocal<WebDriverThread> driverThread;
47 private static List<WebDriverThread> webDriverThreadPool = Collections.synchronizedList(new ArrayList<WebDriverThread>());
48 private static Configuration configuration;
49 private static final String GECKO_DRIVER_KEY = "webdriver.gecko.driver";
51 protected abstract UserCredentials getUserCredentials();
52 protected abstract Configuration getEnvConfiguration();
54 public DriverFactory() {
57 configuration = getEnvConfiguration();
59 //Set geko driver path for firefox
60 if(configuration.getGeckoDriverPath() != null){
61 System.setProperty(GECKO_DRIVER_KEY, configuration.getGeckoDriverPath());
63 throw new RuntimeException("Gecko driver path is null. Make sure to provide path to the gecko driver executable");
65 } catch (Exception e) {
70 @BeforeSuite(alwaysRun = true)
71 public static void instantiateDriverObject() {
74 File basePath = new File(FileHandling.getBasePath());
75 File[] listFiles = basePath.listFiles(new FilenameFilter() {
78 public boolean accept(File basePath, String name) {
79 return name.startsWith(WebDriverThread.AUTOMATION_DOWNLOAD_DIR);
82 Arrays.asList(listFiles).forEach(e -> FileHandling.deleteDirectory(e.getAbsolutePath()));
86 driverThread = new ThreadLocal<WebDriverThread>() {
88 protected WebDriverThread initialValue() {
89 WebDriverThread webDriverThread = new WebDriverThread(configuration);
90 webDriverThreadPool.add(webDriverThread);
91 return webDriverThread;
96 public static WebDriver getDriver() throws Exception {
97 return driverThread.get().getDriver();
100 public static FirefoxProfile getDriverFirefoxProfile() throws Exception {
101 return driverThread.get().getFirefoxProfile();
104 @AfterSuite(alwaysRun = true)
105 public static void quitDriverAfterSuite() throws Exception {
106 for (WebDriverThread webDriverThread : webDriverThreadPool) {
107 if (webDriverThread.getDriver() != null)
108 webDriverThread.quitDriver();
110 deleteDownloadDirs();
113 private static void deleteDownloadDirs() throws IOException {
115 HashMap<Long,WindowTest> windowMap = WindowTestManager.getWholeMap();
116 for (WindowTest win : windowMap.values()){
117 String downloadDirectory = win.getDownloadDirectory();
118 FileUtils.deleteDirectory(new File(downloadDirectory));
122 public static void quitDriver() throws Exception{
123 driverThread.get().quitDriver();
124 driverThread.remove();
125 WindowTestManager.removeWindowTest();
127 public static Configuration getConfiguration() {
128 return configuration;