[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / setup / DriverFactory.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.openecomp.sdc.ci.tests.execute.setup;
22
23
24 import java.io.File;
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;
33
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;
42
43
44 public class DriverFactory {
45
46         private static ThreadLocal<WebDriverThread> driverThread;
47         private static List<WebDriverThread> webDriverThreadPool = Collections.synchronizedList(new ArrayList<WebDriverThread>());
48         private static Config config;
49         
50         public DriverFactory() {
51                 try {
52                         config = Utils.getConfig();
53                 } catch (FileNotFoundException e) {
54                         e.printStackTrace();
55                 }
56         }
57         
58         @BeforeSuite(alwaysRun = true)
59         public static void instantiateDriverObject() {
60                 
61                 
62                 File basePath = new File(FileHandling.getBasePath());
63                 File[] listFiles = basePath.listFiles(new FilenameFilter() {
64                         
65                         @Override
66                         public boolean accept(File basePath, String name) {
67                                 return name.startsWith(WebDriverThread.AUTOMATION_DOWNLOAD_DIR);
68                         }
69                 });
70                 Arrays.asList(listFiles).forEach(e -> FileHandling.deleteDirectory(e.getAbsolutePath()));
71                 
72                 
73                 
74                 driverThread = new ThreadLocal<WebDriverThread>() {
75                         @Override
76                         protected WebDriverThread initialValue() {
77                                 WebDriverThread webDriverThread = new WebDriverThread(config);
78                                 webDriverThreadPool.add(webDriverThread);
79                                 return webDriverThread;
80                         }
81                 };
82         }
83         
84         public static WebDriver getDriver() throws Exception {
85                 return driverThread.get().getDriver();
86         }
87         
88         public static FirefoxProfile getDriverFirefoxProfile() throws Exception {
89                 return driverThread.get().getFirefoxProfile();
90         }
91         
92         @AfterSuite(alwaysRun = true)
93         public static void quitDriverAfterSuite() throws Exception {
94                 for (WebDriverThread webDriverThread : webDriverThreadPool) {
95                         if (webDriverThread.getDriver() != null)
96                                 webDriverThread.quitDriver();
97                 }
98                 deleteDownloadDirs();
99         }
100
101         private static void deleteDownloadDirs() throws IOException {
102 //              System.gc();
103                 HashMap<Long,WindowTest> windowMap = WindowTestManager.getWholeMap();
104                 for (WindowTest win : windowMap.values()){
105                         String downloadDirectory = win.getDownloadDirectory();
106                         FileUtils.deleteDirectory(new File(downloadDirectory));
107                 }
108         }
109         
110         public static void quitDriver() throws Exception{
111                 driverThread.get().quitDriver();
112                 driverThread.remove();
113                 WindowTestManager.removeWindowTest();
114         }
115
116         public static Config getConfig() {
117                 return config;
118         }
119
120         public static void setConfig(Config config) {
121                 DriverFactory.config = config;
122         }
123         
124
125 }