Last active
July 16, 2022 05:40
-
-
Save EricBallard/41909b42b6913955e4667b27dc30cb53 to your computer and use it in GitHub Desktop.
Detects installed Chrome version, validates supported ChromeDriver version, provides proper ChromeDriver URL if mismatched.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.openqa.selenium.SessionNotCreatedException; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import javax.annotation.Nullable; | |
import java.io.File; | |
public class Browser { | |
public static boolean init() { | |
boolean isWindows = System.getProperty("os.name").startsWith("Windows"); | |
// Get currently installed Chrome version, or null | |
String version = Version.getInstalled(); | |
System.out.println("Chrome version installed: " + version); | |
// Validate and version check | |
if (version == null || !Version.isSupported(version)) { | |
System.out.println("Chrome is not installed or un-supported version."); | |
return false; | |
} | |
// Get ChromeDriver path | |
File chromeDriver = new File("path//to//chromedriver" + (isWindows ? ".exe" : "")); | |
if (!chromeDriver.exists()) { | |
System.out.println("ChromeDriver not found, download and place in folder.\n" | |
+ Version.getDriverURL(version)); | |
return false; | |
} | |
// Set PATH to ChromeDriver | |
System.setProperty("webdriver.chrome.driver", chromeDriver.getPath()); | |
return true; | |
} | |
@Nullable | |
public static WebDriver open() { | |
WebDriver driver; | |
try { | |
driver = new ChromeDriver(); | |
} catch (SessionNotCreatedException ignored) { | |
System.out.println("Detected invalid ChromeDriver version for installed version of Chrome.\n" | |
+ Version.getDriverURL("")); | |
return null; | |
} | |
driver.get("https://google.com"); | |
return driver; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.annotation.Nullable; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
public class Version { | |
/** | |
* @TESTED Windows 10 / Ubuntu 22.04 LTS | |
*/ | |
static final String[] supportedVersions = new String[]{"104.0.5112", "103.0.5060", "102.0.5005"}; | |
static boolean isSupported(String version) { | |
for (String sv : supportedVersions) if (version.startsWith(sv)) return true; | |
return false; | |
} | |
static String getDriverURL(String version) { | |
switch (version) { | |
case "104.0.5112": | |
return "https://chromedriver.storage.googleapis.com/index.html?path=104.0.5112.29/"; | |
case "103.0.5060.53": | |
return "https://chromedriver.storage.googleapis.com/index.html?path=103.0.5060.53/"; | |
case "102.0.5005.61": | |
return "https://chromedriver.storage.googleapis.com/index.html?path=102.0.5005.61/"; | |
default: | |
break; | |
} | |
return "https://sites.google.com/chromium.org/driver/downloads"; | |
} | |
static final String REG_QUERY = "reg query HKEY_CURRENT_USER\\Software\\Google\\Chrome\\BLBeacon /v version"; | |
static final String VER_QUERY = "chromium-browser --version"; | |
static String read(boolean useRegistry) throws Exception { | |
Process process = Runtime.getRuntime().exec(useRegistry ? REG_QUERY : VER_QUERY); | |
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); | |
final String keyWord = (useRegistry ? "version" : "Chromium"); | |
String msg, version = null; | |
while ((msg = stdInput.readLine()) != null) { | |
if (msg.contains(keyWord)) { | |
// Formatted args | |
String[] parts = msg.trim().replaceAll(" +", " ").split(" "); | |
if (useRegistry) { | |
// version REG_SZ 104.0.5112... | |
version = parts[2]; | |
} else { | |
// Chormium 104.0.5060.53 snap | |
version = parts[1]; | |
} | |
break; | |
} | |
} | |
stdInput.close(); | |
if (version != null) return version; | |
else throw new NullPointerException("Failed to read query output!"); | |
} | |
/** | |
* Each version of ChromeDriver supports Chrome with matching major, minor, and build version numbers. | |
* For example, ChromeDriver 103.0.5060.114 supports all Chrome versions that start with 103.0.5060 | |
* | |
* @return current version of installed chrome, or NULL | |
*/ | |
@Nullable | |
public static String getInstalled(boolean isWindows) { | |
String version; | |
try { | |
version = read(isWindows); | |
} catch (Exception ignored) { | |
return null; | |
} | |
return version; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: