20
20
public class SeleniumConfig {
21
21
private static final Logger LOGGER = LoggerFactory .getLogger (SeleniumConfig .class );
22
22
23
- private static String propertiesPath = String .join (File .separator , "src" , "functionaltest" , "resources" );
23
+ private static String propertiesPath = String .join (File .separator , "src" , "functionaltest" ,
24
+ "resources" );
24
25
private static String propertiesFile = "functional-test.properties" ;
25
26
26
- private static String firefoxBZip2FileUrlLinux = "" ;
27
-
28
27
private static File tempDownloadDirectory = Files .createTempDir ();
29
28
private static FirefoxDriver driver ;
30
29
31
- public static FirefoxDriver initFirefoxDriver () throws PropertiesNotLoadedException , OSNotSupportedException {
30
+ public static FirefoxDriver initFirefoxDriver ()
31
+ throws PropertiesNotLoadedException , OSNotSupportedException {
32
32
FirefoxOptions firefoxOptions = new FirefoxOptions ()
33
- .setHeadless (true )
34
- .setLogLevel (FirefoxDriverLogLevel .ERROR );
33
+ .setHeadless (true )
34
+ .setLogLevel (
35
+ FirefoxDriverLogLevel .ERROR );
35
36
36
37
firefoxOptions .addPreference ("browser.download.folderList" , 2 );
37
38
firefoxOptions .addPreference ("browser.download.dir" , tempDownloadDirectory .getPath ());
38
39
firefoxOptions .addPreference ("browser.helperApps.neverAsk.saveToDisk" , "application/json" );
39
40
40
- boolean successfullyLoadedProperties = loadProperties ();
41
- if (!successfullyLoadedProperties ) {
42
- LOGGER .error ("Properties was not properly loaded." );
43
- throw new PropertiesNotLoadedException ();
44
- }
45
-
46
41
if (SystemUtils .IS_OS_LINUX ) {
47
- FirefoxBinary firefoxBinary = installFirefoxBinary ();
42
+ FirefoxBinary firefoxBinary = getFirefoxBinary ();
48
43
firefoxOptions .setBinary (firefoxBinary );
49
44
50
- System .setProperty ("webdriver.gecko.driver" , "src/functionaltest/resources/geckodriver" );
45
+ System .setProperty ("webdriver.gecko.driver" ,
46
+ "src/functionaltest/resources/geckodriver" );
51
47
} else if (SystemUtils .IS_OS_WINDOWS ) {
52
- System .setProperty ("webdriver.gecko.driver" , "src/functionaltest/resources/geckodriver.exe" );
48
+ System .setProperty ("webdriver.gecko.driver" ,
49
+ "src/functionaltest/resources/geckodriver.exe" );
53
50
} else {
54
51
LOGGER .error (SystemUtils .OS_NAME + " currently not supported." );
55
52
throw new OSNotSupportedException ();
56
53
}
57
54
58
55
driver = new FirefoxDriver (firefoxOptions );
59
56
60
- //Make sure all firefox browsers are closed after all tests have finished
57
+ // Make sure all firefox browsers are closed after all tests have finished
61
58
Runtime .getRuntime ().addShutdownHook (new Thread (() -> driver .quit ()));
62
59
return driver ;
63
60
}
@@ -70,32 +67,58 @@ public static String getBaseUrl(int randomServerPort) {
70
67
return "http://localhost:" + randomServerPort ;
71
68
}
72
69
73
- private static FirefoxBinary installFirefoxBinary () {
74
- String firefoxBZip2FileNameLinux = FilenameUtils .getName (firefoxBZip2FileUrlLinux );
75
-
76
- String firefoxBZip2FilePath = String .join (
77
- File .separator , tempDownloadDirectory .getPath (), firefoxBZip2FileNameLinux );
78
- Utils .downloadFileFromUrlToDestination (firefoxBZip2FileUrlLinux , firefoxBZip2FilePath );
79
- Utils .extractBZip2InDir (firefoxBZip2FilePath , tempDownloadDirectory .getPath ());
70
+ private static FirefoxBinary getFirefoxBinary () {
71
+ // Firefox binary will be stored in <repository>/target/firefox/firefox/<binary>
72
+ String firefoxPath = getFirefoxDirPath ().getPath ();
80
73
File firefoxBinaryFilePath = new File (
81
- String .join (File .separator , tempDownloadDirectory .getPath (), "firefox" , "firefox" ));
74
+ String .join (File .separator , firefoxPath , "firefox" , "firefox" ));
75
+
76
+ if (firefoxBinaryFilePath .isFile ()) {
77
+ LOGGER .debug ("Reusing existing firefox binary." );
78
+ return new FirefoxBinary (firefoxBinaryFilePath );
79
+ }
80
+
81
+ LOGGER .debug ("Downloading and extracting new Firefox binary." );
82
+ final String firefoxTarFileUrl = getFirefoxTarFileUrl ();
83
+
84
+ String firefoxBZip2FileNameLinux = FilenameUtils .getName (firefoxTarFileUrl );
85
+ String firefoxTarFilePath = String .join (
86
+ File .separator , firefoxPath , firefoxBZip2FileNameLinux );
87
+
88
+ Utils .downloadFileFromUrlToDestination (firefoxTarFileUrl , firefoxTarFilePath );
89
+ Utils .extractBZip2InDir (firefoxTarFilePath , firefoxPath );
82
90
Utils .makeBinFileExecutable (firefoxBinaryFilePath );
83
- FirefoxBinary firefoxBinary = new FirefoxBinary (firefoxBinaryFilePath );
84
- return firefoxBinary ;
91
+
92
+ return new FirefoxBinary (firefoxBinaryFilePath );
93
+ }
94
+
95
+ private static File getFirefoxDirPath () {
96
+ String relPath = SeleniumConfig .class .getProtectionDomain ()
97
+ .getCodeSource ()
98
+ .getLocation ()
99
+ .getFile ();
100
+ File firefoxDir = new File (relPath + "../firefox" );
101
+ if (!firefoxDir .exists ()) {
102
+ firefoxDir .mkdir ();
103
+ }
104
+ return firefoxDir ;
85
105
}
86
- private static boolean loadProperties () {
87
- final String propertiesFilePath = String .join (File .separator , propertiesPath , propertiesFile );
106
+
107
+ private static String getFirefoxTarFileUrl () {
108
+ final String propertiesFilePath = String .join (
109
+ File .separator , propertiesPath , propertiesFile );
88
110
final Properties properties = Utils .getProperties (propertiesFilePath );
89
111
90
- firefoxBZip2FileUrlLinux = properties .getProperty ("test.selenium.firefox.BZip2File.url.linux" );
112
+ final String firefoxTarFileUrl = properties .getProperty (
113
+ "test.selenium.firefox.TarFile.url.linux" );
91
114
92
- if (StringUtils .isEmpty (firefoxBZip2FileUrlLinux )) {
93
- LOGGER .error ("Failed to load properties, firefoxBZip2FileUrlLinux is not set." );
94
- return false ;
95
- } else {
96
- LOGGER .debug ("Properties have been loaded." );
97
- return true ;
115
+ if (StringUtils .isEmpty (firefoxTarFileUrl )) {
116
+ final String message = "Failed to load firefox binary URL from properties." ;
117
+ LOGGER .error (message );
118
+ throw new PropertiesNotLoadedException (message );
98
119
}
120
+
121
+ return firefoxTarFileUrl ;
99
122
}
100
123
101
124
}
0 commit comments