1111import java .io .*;
1212import java .net .HttpURLConnection ;
1313import java .net .URL ;
14+ import java .util .Arrays ;
1415import java .util .zip .ZipEntry ;
1516import java .util .zip .ZipInputStream ;
1617
@@ -28,6 +29,12 @@ public class ChromeDownloader {
2829 public static String downloadChromeForTesting () {
2930 try {
3031 String platform = detectPlatform ();
32+ String localBinary = findExistingChromeBinary (platform );
33+ if (localBinary != null ) {
34+ logger .log (Level .INFO , "Using existing Chrome for Testing: " + localBinary );
35+ return localBinary ;
36+ }
37+
3138 String latestVersion = fetchLatestVersion ();
3239 String versionDir = DOWNLOAD_DIR + File .separator + latestVersion ;
3340 String extractPath = versionDir + File .separator + "chrome-" + platform ;
@@ -92,6 +99,29 @@ public static String downloadChromeForTesting() {
9299 }
93100 }
94101
102+ private static String findExistingChromeBinary (String platform ) {
103+ File downloadDirectory = new File (DOWNLOAD_DIR );
104+ if (!downloadDirectory .isDirectory ()) {
105+ return null ;
106+ }
107+
108+ String chromeExecutable = platform .contains ("win" ) ? "chrome.exe" : "chrome" ;
109+ File [] versionDirectories = downloadDirectory .listFiles (File ::isDirectory );
110+ if (versionDirectories == null || versionDirectories .length == 0 ) {
111+ return null ;
112+ }
113+
114+ Arrays .sort (versionDirectories , (left , right ) -> right .getName ().compareTo (left .getName ()));
115+ for (File versionDirectory : versionDirectories ) {
116+ File candidate = new File (versionDirectory , "chrome-" + platform + File .separator + chromeExecutable );
117+ if (candidate .isFile ()) {
118+ return candidate .getAbsolutePath ();
119+ }
120+ }
121+
122+ return null ;
123+ }
124+
95125 private static String fetchLatestVersion () throws IOException {
96126 URL url = new URL (CHROME_DOWNLOAD_URL );
97127 HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
0 commit comments