Skip to content

Commit a5ad7f0

Browse files
author
Anders Breid
authored
Speed up tests (#267)
- Fix unecessary loading of firefox driver on tests that does not need it. - Fix firefox tar was downloaded and extracted for each test.
1 parent 04958a3 commit a5ad7f0

File tree

5 files changed

+61
-40
lines changed

5 files changed

+61
-40
lines changed

src/functionaltest/java/com/ericsson/ei/config/SeleniumConfig.java

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,41 @@
2020
public class SeleniumConfig {
2121
private static final Logger LOGGER = LoggerFactory.getLogger(SeleniumConfig.class);
2222

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");
2425
private static String propertiesFile = "functional-test.properties";
2526

26-
private static String firefoxBZip2FileUrlLinux = "";
27-
2827
private static File tempDownloadDirectory = Files.createTempDir();
2928
private static FirefoxDriver driver;
3029

31-
public static FirefoxDriver initFirefoxDriver() throws PropertiesNotLoadedException, OSNotSupportedException {
30+
public static FirefoxDriver initFirefoxDriver()
31+
throws PropertiesNotLoadedException, OSNotSupportedException {
3232
FirefoxOptions firefoxOptions = new FirefoxOptions()
33-
.setHeadless(true)
34-
.setLogLevel(FirefoxDriverLogLevel.ERROR);
33+
.setHeadless(true)
34+
.setLogLevel(
35+
FirefoxDriverLogLevel.ERROR);
3536

3637
firefoxOptions.addPreference("browser.download.folderList", 2);
3738
firefoxOptions.addPreference("browser.download.dir", tempDownloadDirectory.getPath());
3839
firefoxOptions.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/json");
3940

40-
boolean successfullyLoadedProperties = loadProperties();
41-
if (!successfullyLoadedProperties) {
42-
LOGGER.error("Properties was not properly loaded.");
43-
throw new PropertiesNotLoadedException();
44-
}
45-
4641
if (SystemUtils.IS_OS_LINUX) {
47-
FirefoxBinary firefoxBinary = installFirefoxBinary();
42+
FirefoxBinary firefoxBinary = getFirefoxBinary();
4843
firefoxOptions.setBinary(firefoxBinary);
4944

50-
System.setProperty("webdriver.gecko.driver", "src/functionaltest/resources/geckodriver");
45+
System.setProperty("webdriver.gecko.driver",
46+
"src/functionaltest/resources/geckodriver");
5147
} 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");
5350
} else {
5451
LOGGER.error(SystemUtils.OS_NAME + " currently not supported.");
5552
throw new OSNotSupportedException();
5653
}
5754

5855
driver = new FirefoxDriver(firefoxOptions);
5956

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
6158
Runtime.getRuntime().addShutdownHook(new Thread(() -> driver.quit()));
6259
return driver;
6360
}
@@ -70,32 +67,58 @@ public static String getBaseUrl(int randomServerPort) {
7067
return "http://localhost:" + randomServerPort;
7168
}
7269

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();
8073
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);
8290
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;
85105
}
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);
88110
final Properties properties = Utils.getProperties(propertiesFilePath);
89111

90-
firefoxBZip2FileUrlLinux = properties.getProperty("test.selenium.firefox.BZip2File.url.linux");
112+
final String firefoxTarFileUrl = properties.getProperty(
113+
"test.selenium.firefox.TarFile.url.linux");
91114

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);
98119
}
120+
121+
return firefoxTarFileUrl;
99122
}
100123

101124
}

src/functionaltest/java/com/ericsson/ei/frontend/TestBridgeCurlFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.mockserver.client.MockServerClient;
2323
import org.mockserver.integration.ClientAndServer;
2424

25-
public class TestBridgeCurlFunctions extends SeleniumBaseClass {
25+
public class TestBridgeCurlFunctions extends TestBaseClass {
2626
private static MockServerClient mockClient1;
2727
private static MockServerClient mockClient2;
2828
private static ClientAndServer mockServer1;

src/functionaltest/java/com/ericsson/ei/frontend/exception/PropertiesNotLoadedException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.ericsson.ei.frontend.exception;
22

3-
public class PropertiesNotLoadedException extends Exception {
3+
public class PropertiesNotLoadedException extends RuntimeException {
44

55
private static final long serialVersionUID = 1L;
66

src/functionaltest/resources/functional-test.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
test.selenium.firefox.BZip2File.url.linux = https://ftp.mozilla.org/pub/firefox/releases/61.0.1/linux-x86_64/en-GB/firefox-61.0.1.tar.bz2
1+
test.selenium.firefox.TarFile.url.linux = https://ftp.mozilla.org/pub/firefox/releases/61.0.1/linux-x86_64/en-GB/firefox-61.0.1.tar.bz2
22
ei.backend.instances.list.json.content=[{"name":"EI-Backend-1","host":"localhost","port":8070,"contextPath":"","https":false,"defaultBackend":true}, \
33
{"name":"EI-Backend-2","host":"localhost","port":8077,"contextPath":"","https":false,"defaultBackend":false},\
44
{"name":"EI-Backend-3","host":"localhost","port":8078,"contextPath":"","https":false,"defaultBackend":true}]

src/test/java/com/ericsson/ei/frontend/BackendInformationControllerTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,11 @@ public class BackendInformationControllerTest {
5757
@MockBean
5858
private BackendInstancesHandler utils;
5959

60-
private JsonObject instance;
6160
private JsonArray instances;
6261
private List<BackendInstance> information;
6362

6463
@Before
6564
public void before() throws Exception {
66-
instance = new JsonParser().parse(new FileReader(BACKEND_INSTANCE_FILE_PATH)).getAsJsonObject();
6765
instances = new JsonParser().parse(new FileReader(BACKEND_INSTANCES_FILE_PATH)).getAsJsonArray();
6866
information = new ArrayList<>();
6967
for(JsonElement element : instances) {

0 commit comments

Comments
 (0)