Merge from ecomp 718fd196 - Integration Tests
[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.FilenameFilter;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
32
33 import org.apache.commons.io.FileUtils;
34 import org.onap.sdc.ci.tests.datatypes.Configuration;
35 import org.onap.sdc.ci.tests.datatypes.UserCredentials;
36 import org.onap.sdc.ci.tests.utilities.FileHandling;
37 import org.openqa.selenium.WebDriver;
38 import org.openqa.selenium.firefox.FirefoxProfile;
39 import org.testng.annotations.AfterSuite;
40 import org.testng.annotations.BeforeSuite;
41
42
43 public abstract class DriverFactory {
44
45         private static ThreadLocal<WebDriverThread> driverThread;
46         private static List<WebDriverThread> webDriverThreadPool = Collections.synchronizedList(new ArrayList<WebDriverThread>());
47         private static Configuration configuration;
48         private static final String GECKO_DRIVER_KEY = "webdriver.gecko.driver";
49         
50         protected abstract UserCredentials getUserCredentials();
51         protected abstract Configuration getEnvConfiguration();
52         
53         public DriverFactory() {
54
55                 try {
56                         configuration = getEnvConfiguration();
57
58                         //Set geko driver path for firefox
59                         if(configuration.getGeckoDriverPath() != null){
60                                 System.setProperty(GECKO_DRIVER_KEY, configuration.getGeckoDriverPath());
61                         } else {
62                                 throw new RuntimeException("Gecko driver path is null. Make sure to provide path to the gecko driver executable");
63                         }
64                 } catch (Exception e) {
65                         e.printStackTrace();
66                 }
67         }
68         
69         @BeforeSuite(alwaysRun = true)
70         public static void instantiateDriverObject() {
71                 
72                 
73                 File basePath = new File(FileHandling.getBasePath());
74                 File[] listFiles = basePath.listFiles(new FilenameFilter() {
75                         
76                         @Override
77                         public boolean accept(File basePath, String name) {
78                                 return name.startsWith(WebDriverThread.AUTOMATION_DOWNLOAD_DIR);
79                         }
80                 });
81                 Arrays.asList(listFiles).forEach(e -> FileHandling.deleteDirectory(e.getAbsolutePath()));
82                 
83                 
84                 
85                 driverThread = new ThreadLocal<WebDriverThread>() {
86                         @Override
87                         protected WebDriverThread initialValue() {
88                                 WebDriverThread webDriverThread = new WebDriverThread(configuration);
89                                 webDriverThreadPool.add(webDriverThread);
90                                 return webDriverThread;
91                         }
92                 };
93         }
94         
95         public static WebDriver getDriver() throws Exception {
96                 return driverThread.get().getDriver();
97         }
98         
99         public static FirefoxProfile getDriverFirefoxProfile() throws Exception {
100                 return driverThread.get().getFirefoxProfile();
101         }
102         
103         @AfterSuite(alwaysRun = true)
104         public static void quitDriverAfterSuite() throws Exception {
105                 for (WebDriverThread webDriverThread : webDriverThreadPool) {
106                         if (webDriverThread.getDriver() != null)
107                                 webDriverThread.quitDriver();
108                 }
109                 deleteDownloadDirs();
110         }
111
112         private static void deleteDownloadDirs() throws IOException {
113 //              System.gc();
114                 HashMap<Long,WindowTest> windowMap = WindowTestManager.getWholeMap();
115                 for (WindowTest win : windowMap.values()){
116                         String downloadDirectory = win.getDownloadDirectory();
117                         FileUtils.deleteDirectory(new File(downloadDirectory));
118                 }
119         }
120         
121         public static void quitDriver() throws Exception{
122                 driverThread.get().quitDriver();
123                 driverThread.remove();
124                 WindowTestManager.removeWindowTest();
125         }
126         public static Configuration getConfiguration() {
127                 return configuration;
128         }
129         
130
131 }