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;
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.openecomp.sdc.ci.tests.config.Config;
36 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
37 import org.openecomp.sdc.ci.tests.utils.Utils;
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 class DriverFactory {
46 private static ThreadLocal<WebDriverThread> driverThread;
47 private static List<WebDriverThread> webDriverThreadPool = Collections.synchronizedList(new ArrayList<WebDriverThread>());
48 private static Config config;
50 public DriverFactory() {
52 config = Utils.getConfig();
53 } catch (FileNotFoundException e) {
58 @BeforeSuite(alwaysRun = true)
59 public static void instantiateDriverObject() {
62 File basePath = new File(FileHandling.getBasePath());
63 File[] listFiles = basePath.listFiles(new FilenameFilter() {
66 public boolean accept(File basePath, String name) {
67 return name.startsWith(WebDriverThread.AUTOMATION_DOWNLOAD_DIR);
70 Arrays.asList(listFiles).forEach(e -> FileHandling.deleteDirectory(e.getAbsolutePath()));
74 driverThread = new ThreadLocal<WebDriverThread>() {
76 protected WebDriverThread initialValue() {
77 WebDriverThread webDriverThread = new WebDriverThread(config);
78 webDriverThreadPool.add(webDriverThread);
79 return webDriverThread;
84 public static WebDriver getDriver() throws Exception {
85 return driverThread.get().getDriver();
88 public static FirefoxProfile getDriverFirefoxProfile() throws Exception {
89 return driverThread.get().getFirefoxProfile();
92 @AfterSuite(alwaysRun = true)
93 public static void quitDriverAfterSuite() throws Exception {
94 for (WebDriverThread webDriverThread : webDriverThreadPool) {
95 if (webDriverThread.getDriver() != null)
96 webDriverThread.quitDriver();
99 MobProxy.removeAllProxyServers();
101 deleteDownloadDirs();
104 private static void deleteDownloadDirs() throws IOException {
106 HashMap<Long,WindowTest> windowMap = WindowTestManager.getWholeMap();
107 for (WindowTest win : windowMap.values()){
108 String downloadDirectory = win.getDownloadDirectory();
109 FileUtils.deleteDirectory(new File(downloadDirectory));
113 public static void quitDriver() throws Exception{
114 driverThread.get().quitDriver();
115 driverThread.remove();
116 WindowTestManager.removeWindowTest();
117 MobProxy.removePoxyServer();
120 public static Config getConfig() {
124 public static void setConfig(Config config) {
125 DriverFactory.config = config;