Merge "Merge automation from ECOMP's repository"
[vid.git] / vid-automation / src / main / java / org / onap / 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.onap.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.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;
42
43
44 public abstract class DriverFactory {
45
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";
50         
51         protected abstract UserCredentials getUserCredentials();
52         protected abstract Configuration getEnvConfiguration();
53         
54         public DriverFactory() {
55
56                 try {
57                         configuration = getEnvConfiguration();
58
59                         //Set geko driver path for firefox
60                         if(configuration.getGeckoDriverPath() != null){
61                                 System.setProperty(GECKO_DRIVER_KEY, configuration.getGeckoDriverPath());
62                         } else {
63                                 throw new RuntimeException("Gecko driver path is null. Make sure to provide path to the gecko driver executable");
64                         }
65                 } catch (Exception e) {
66                         e.printStackTrace();
67                 }
68         }
69         
70         @BeforeSuite(alwaysRun = true)
71         public static void instantiateDriverObject() {
72                 
73                 
74                 File basePath = new File(FileHandling.getBasePath());
75                 File[] listFiles = basePath.listFiles(new FilenameFilter() {
76                         
77                         @Override
78                         public boolean accept(File basePath, String name) {
79                                 return name.startsWith(WebDriverThread.AUTOMATION_DOWNLOAD_DIR);
80                         }
81                 });
82                 Arrays.asList(listFiles).forEach(e -> FileHandling.deleteDirectory(e.getAbsolutePath()));
83                 
84                 
85                 
86                 driverThread = new ThreadLocal<WebDriverThread>() {
87                         @Override
88                         protected WebDriverThread initialValue() {
89                                 WebDriverThread webDriverThread = new WebDriverThread(configuration);
90                                 webDriverThreadPool.add(webDriverThread);
91                                 return webDriverThread;
92                         }
93                 };
94         }
95         
96         public static WebDriver getDriver() throws Exception {
97                 return driverThread.get().getDriver();
98         }
99         
100         public static FirefoxProfile getDriverFirefoxProfile() throws Exception {
101                 return driverThread.get().getFirefoxProfile();
102         }
103         
104         @AfterSuite(alwaysRun = true)
105         public static void quitDriverAfterSuite() throws Exception {
106                 for (WebDriverThread webDriverThread : webDriverThreadPool) {
107                         if (webDriverThread.getDriver() != null)
108                                 webDriverThread.quitDriver();
109                 }
110                 deleteDownloadDirs();
111         }
112
113         private static void deleteDownloadDirs() throws IOException {
114 //              System.gc();
115                 HashMap<Long,WindowTest> windowMap = WindowTestManager.getWholeMap();
116                 for (WindowTest win : windowMap.values()){
117                         String downloadDirectory = win.getDownloadDirectory();
118                         FileUtils.deleteDirectory(new File(downloadDirectory));
119                 }
120         }
121         
122         public static void quitDriver() throws Exception{
123                 driverThread.get().quitDriver();
124                 driverThread.remove();
125                 WindowTestManager.removeWindowTest();
126         }
127         public static Configuration getConfiguration() {
128                 return configuration;
129         }
130         
131
132 }