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