44package com .azure .test .aad .common ;
55
66import com .azure .spring .test .AppRunner ;
7- import java .io .File ;
8- import java .io .IOException ;
9- import java .io .InputStream ;
107import 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 ;
149import org .openqa .selenium .WebDriver ;
1510import org .openqa .selenium .chrome .ChromeDriver ;
16- import org .openqa .selenium .chrome .ChromeDriverService ;
1711import org .openqa .selenium .chrome .ChromeOptions ;
1812import 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 */
0 commit comments