Skip to content

Commit 9c76ab0

Browse files
authored
refactor IT with webdrivermanager (Azure#18845)
* refactor with WebDriverManager
1 parent 627b9c8 commit 9c76ab0

File tree

8 files changed

+11
-64
lines changed

8 files changed

+11
-64
lines changed

eng/versioning/external_dependencies.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ com.zaxxer:HikariCP;3.4.5
4141
commons-codec:commons-codec;1.14
4242
commons-net:commons-net;3.6
4343
io.fabric8:kubernetes-client;4.10.3
44+
io.github.bonigarcia:webdrivermanager;4.3.1
4445
io.micrometer:micrometer-core;1.5.6
4546
io.micrometer:micrometer-registry-azure-monitor;1.5.6
4647
io.micrometer:micrometer-registry-graphite;1.5.6

sdk/spring/azure-spring-boot-test-selenium-common/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
<version>1.0.0</version> <!-- {x-version-update;com.azure.spring:azure-spring-boot-test-core;current} -->
2929
</dependency>
3030

31+
<dependency>
32+
<groupId>io.github.bonigarcia</groupId>
33+
<artifactId>webdrivermanager</artifactId>
34+
<version>4.3.1</version> <!-- {x-version-update;io.github.bonigarcia:webdrivermanager;external_dependency} -->
35+
<scope>compile</scope>
36+
</dependency>
37+
3138
</dependencies>
3239

3340
</project>

sdk/spring/azure-spring-boot-test-selenium-common/src/main/java/com/azure/test/aad/common/SeleniumITHelper.java

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44
package com.azure.test.aad.common;
55

66
import com.azure.spring.test.AppRunner;
7-
import java.io.File;
8-
import java.io.IOException;
9-
import java.io.InputStream;
107
import java.util.Map;
11-
import java.util.UUID;
12-
import java.util.regex.Pattern;
13-
import org.apache.commons.io.FileUtils;
8+
import io.github.bonigarcia.wdm.WebDriverManager;
149
import org.openqa.selenium.WebDriver;
1510
import org.openqa.selenium.chrome.ChromeDriver;
16-
import org.openqa.selenium.chrome.ChromeDriverService;
1711
import org.openqa.selenium.chrome.ChromeOptions;
1812
import org.openqa.selenium.support.ui.WebDriverWait;
1913

@@ -22,54 +16,17 @@ public class SeleniumITHelper {
2216
protected AppRunner app;
2317
protected WebDriver driver;
2418
protected WebDriverWait wait;
25-
private final static String tempDirPath =
26-
System.getProperty("java.io.tmpdir") + File.separator + UUID.randomUUID();
27-
28-
static {
29-
initChromeDriver();
30-
deleteChromeDriverFile();
31-
}
3219

3320
public SeleniumITHelper(Class<?> appClass, Map<String, String> properties) {
3421
createDriver();
3522
createAppRunner(appClass, properties);
3623
}
3724

38-
private static void initChromeDriver() {
39-
final String chromedriverLinux = "chromedriver_linux64";
40-
final String chromedriverWin32 = "chromedriver_win32.exe";
41-
final String chromedriverMac = "chromedriver_mac64";
42-
String osName = System.getProperty("os.name").toLowerCase();
43-
Process process = null;
44-
File dir;
45-
try {
46-
if (Pattern.matches("linux.*", osName)) {
47-
dir = copyChromeDriverFile(chromedriverLinux);
48-
process = Runtime.getRuntime().exec("chmod +x " + chromedriverLinux, null, dir.getParentFile());
49-
process.waitFor();
50-
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, dir.getPath());
51-
} else if (Pattern.matches("windows.*", osName)) {
52-
dir = copyChromeDriverFile(chromedriverWin32);
53-
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, dir.getPath());
54-
} else if (Pattern.matches("mac.*", osName)) {
55-
dir = copyChromeDriverFile(chromedriverMac);
56-
process = Runtime.getRuntime().exec("chmod +x " + chromedriverMac, null, dir.getParentFile());
57-
process.waitFor();
58-
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, dir.getPath());
59-
} else {
60-
throw new IllegalStateException("Unrecognized osName. osName = " + System.getProperty("os.name"));
61-
}
62-
} catch (InterruptedException | IOException e) {
63-
throw new RuntimeException(e);
64-
} finally {
65-
if (process != null) {
66-
process.destroyForcibly();
67-
}
68-
}
69-
}
7025

7126
protected void createDriver() {
7227
if (driver == null) {
28+
System.setProperty("wdm.cachePath", getClass().getClassLoader().getResource("selenium").getPath());
29+
WebDriverManager.chromedriver().setup();
7330
ChromeOptions options = new ChromeOptions();
7431
options.addArguments("--headless");
7532
options.addArguments("--incognito", "--no-sandbox", "--disable-dev-shm-usage");
@@ -84,24 +41,6 @@ protected void createAppRunner(Class<?> appClass, Map<String, String> properties
8441
app.start();
8542
}
8643

87-
private static File copyChromeDriverFile(String chromeDriverName)
88-
throws IOException {
89-
InputStream resourceAsStream = SeleniumITHelper.class.getClassLoader()
90-
.getResourceAsStream("driver/" + chromeDriverName);
91-
File dest = new File(tempDirPath + File.separator + chromeDriverName);
92-
FileUtils.copyInputStreamToFile(resourceAsStream, dest);
93-
return dest;
94-
}
95-
96-
private static void deleteChromeDriverFile() {
97-
File targetFile = new File(tempDirPath);
98-
try {
99-
FileUtils.forceDeleteOnExit(targetFile);
100-
} catch (IOException e) {
101-
e.printStackTrace();
102-
}
103-
}
104-
10544
/**
10645
* Manually invoke destroy to complete resource release.
10746
*/

sdk/spring/azure-spring-boot-test-selenium-common/src/main/resources/driver/chromedriver_linux64 renamed to sdk/spring/azure-spring-boot-test-selenium-common/src/main/resources/selenium/chromedriver/linux64/86.0.4240.22/chromedriver

File renamed without changes.

sdk/spring/azure-spring-boot-test-selenium-common/src/main/resources/driver/chromedriver_mac64 renamed to sdk/spring/azure-spring-boot-test-selenium-common/src/main/resources/selenium/chromedriver/mac64/88.0.4324.96/chromedriver

15 MB
Binary file not shown.

sdk/spring/azure-spring-boot-test-selenium-common/src/main/resources/driver/chromedriver_win32.exe renamed to sdk/spring/azure-spring-boot-test-selenium-common/src/main/resources/selenium/chromedriver/win32/88.0.4324.96/chromedriver.exe

9.27 MB
Binary file not shown.

0 commit comments

Comments
 (0)