Best Selenium code snippet using org.openqa.selenium.chrome.ChromeDriverInfo.isAvailable
Source:NodeOptionsTest.java
...53 @Test54 public void canConfigureNodeWithDriverDetection() {55 assumeFalse("We don't have driver servers in PATH when we run unit tests",56 Boolean.parseBoolean(System.getenv("GITHUB_ACTIONS")));57 assumeTrue("ChromeDriver needs to be available", new ChromeDriverInfo().isAvailable());58 Config config = new MapConfig(singletonMap("node", singletonMap("detect-drivers", "true")));59 List<Capabilities> reported = new ArrayList<>();60 new NodeOptions(config).getSessionFactories(caps -> {61 reported.add(caps);62 return Collections.singleton(HelperFactory.create(config, caps));63 });64 ChromeDriverInfo chromeDriverInfo = new ChromeDriverInfo();65 String expected = chromeDriverInfo.getDisplayName();66 reported.stream()67 .filter(chromeDriverInfo::isSupporting)68 .filter(caps -> expected.equalsIgnoreCase(caps.getBrowserName()))69 .findFirst()70 .orElseThrow(() -> new AssertionError("Unable to find Chrome info"));71 }72 @Test73 public void shouldDetectCorrectDriversOnWindows() {74 assumeTrue(Platform.getCurrent().is(Platform.WINDOWS));75 assumeFalse("We don't have driver servers in PATH when we run unit tests",76 Boolean.parseBoolean(System.getenv("GITHUB_ACTIONS")));77 Config config = new MapConfig(singletonMap("node", singletonMap("detect-drivers", "true")));78 List<Capabilities> reported = new ArrayList<>();79 new NodeOptions(config).getSessionFactories(caps -> {80 reported.add(caps);81 return Collections.singleton(HelperFactory.create(config, caps));82 });83 assertThat(reported).is(supporting("chrome"));84 assertThat(reported).is(supporting("firefox"));85 assertThat(reported).is(supporting("internet explorer"));86 assertThat(reported).is(supporting("MicrosoftEdge"));87 assertThat(reported).isNot(supporting("safari"));88 }89 @Test90 public void shouldDetectCorrectDriversOnMac() {91 assumeTrue(Platform.getCurrent().is(Platform.MAC));92 assumeFalse("We don't have driver servers in PATH when we run unit tests",93 Boolean.parseBoolean(System.getenv("GITHUB_ACTIONS")));94 Config config = new MapConfig(singletonMap("node", singletonMap("detect-drivers", "true")));95 List<Capabilities> reported = new ArrayList<>();96 new NodeOptions(config).getSessionFactories(caps -> {97 reported.add(caps);98 return Collections.singleton(HelperFactory.create(config, caps));99 });100 // There may be more drivers available, but we know that these are meant to be here.101 assertThat(reported).is(supporting("safari"));102 assertThat(reported).isNot(supporting("internet explorer"));103 }104 @Test105 public void canConfigureNodeWithoutDriverDetection() {106 Config config = new MapConfig(singletonMap("node", singletonMap("detect-drivers", "false")));107 List<Capabilities> reported = new ArrayList<>();108 new NodeOptions(config).getSessionFactories(caps -> {109 reported.add(caps);110 return Collections.singleton(HelperFactory.create(config, caps));111 });112 assertThat(reported).isEmpty();113 }114 @Test115 public void shouldThrowConfigExceptionIfDetectDriversIsFalseAndSpecificDriverIsAdded() {116 Config config = new MapConfig(117 singletonMap("node",118 ImmutableMap.of(119 "detect-drivers", "false",120 "driver-implementation", "[chrome]"121 )));122 List<Capabilities> reported = new ArrayList<>();123 try {124 new NodeOptions(config).getSessionFactories(caps -> {125 reported.add(caps);126 return Collections.singleton(HelperFactory.create(config, caps));127 });128 fail("Should have not executed 'getSessionFactories' successfully");129 } catch (ConfigException e) {130 // Fall through131 }132 assertThat(reported).isEmpty();133 }134 @Test135 public void detectDriversByDefault() {136 Config config = new MapConfig(emptyMap());137 List<Capabilities> reported = new ArrayList<>();138 new NodeOptions(config).getSessionFactories(caps -> {139 reported.add(caps);140 return Collections.singleton(HelperFactory.create(config, caps));141 });142 assertThat(reported).isNotEmpty();143 }144 @Test145 public void canBeConfiguredToUseHelperClassesToCreateSessionFactories() {146 Capabilities caps = new ImmutableCapabilities("browserName", "cheese");147 StringBuilder capsString = new StringBuilder();148 new Json().newOutput(capsString).setPrettyPrint(false).write(caps);149 Config config = new TomlConfig(new StringReader(String.format(150 "[node]\n" +151 "detect-drivers = false\n" +152 "driver-factories = [" +153 " \"%s\",\n" +154 " \"%s\"\n" +155 "]",156 HelperFactory.class.getName(),157 capsString.toString().replace("\"", "\\\""))));158 NodeOptions options = new NodeOptions(config);159 Map<Capabilities, Collection<SessionFactory>> factories = options.getSessionFactories(info -> emptySet());160 Collection<SessionFactory> sessionFactories = factories.get(caps);161 assertThat(sessionFactories).size().isEqualTo(1);162 assertThat(sessionFactories.iterator().next()).isInstanceOf(SessionFactory.class);163 }164 @Test165 public void driversCanBeConfigured() {166 String chromeLocation = "/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta";167 String firefoxLocation = "/Applications/Firefox Nightly.app/Contents/MacOS/firefox-bin";168 ChromeOptions chromeOptions = new ChromeOptions();169 chromeOptions.setBinary(chromeLocation);170 FirefoxOptions firefoxOptions = new FirefoxOptions();171 firefoxOptions.setBinary(firefoxLocation);172 StringBuilder chromeCaps = new StringBuilder();173 StringBuilder firefoxCaps = new StringBuilder();174 new Json().newOutput(chromeCaps).setPrettyPrint(false).write(chromeOptions);175 new Json().newOutput(firefoxCaps).setPrettyPrint(false).write(firefoxOptions);176 String[] rawConfig = new String[]{177 "[node]",178 "detect-drivers = false",179 "[[node.driver-configuration]]",180 "name = \"Chrome Beta\"",181 "max-sessions = 1",182 String.format("stereotype = \"%s\"", chromeCaps.toString().replace("\"", "\\\"")),183 "[[node.driver-configuration]]",184 "name = \"Firefox Nightly\"",185 "max-sessions = 2",186 String.format("stereotype = \"%s\"", firefoxCaps.toString().replace("\"", "\\\""))187 };188 Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));189 List<Capabilities> reported = new ArrayList<>();190 new NodeOptions(config).getSessionFactories(capabilities -> {191 reported.add(capabilities);192 return Collections.singleton(HelperFactory.create(config, capabilities));193 });194 assertThat(reported).is(supporting("chrome"));195 assertThat(reported).is(supporting("firefox"));196 //noinspection unchecked197 assertThat(reported)198 .filteredOn(capabilities -> capabilities.asMap().containsKey(ChromeOptions.CAPABILITY))199 .anyMatch(200 capabilities ->201 ((Map<String, String>) capabilities.getCapability(ChromeOptions.CAPABILITY))202 .get("binary").equalsIgnoreCase(chromeLocation));203 assertThat(reported)204 .filteredOn(capabilities -> capabilities.asMap().containsKey(ChromeOptions.CAPABILITY))205 .hasSize(1);206 //noinspection unchecked207 assertThat(reported)208 .filteredOn(capabilities -> capabilities.asMap().containsKey(FirefoxOptions.FIREFOX_OPTIONS))209 .anyMatch(210 capabilities ->211 ((Map<String, String>) capabilities.getCapability(FirefoxOptions.FIREFOX_OPTIONS))212 .get("binary").equalsIgnoreCase(firefoxLocation));213 assertThat(reported)214 .filteredOn(capabilities -> capabilities.asMap().containsKey(FirefoxOptions.FIREFOX_OPTIONS))215 .hasSize(2);216 }217 @Test218 public void driversConfigNeedsStereotypeField() {219 String[] rawConfig = new String[]{220 "[node]",221 "detect-drivers = false",222 "[[node.driver-configuration]]",223 "name = \"Chrome Beta\"",224 "max-sessions = 2",225 "cheese = \"paipa\"",226 "[[node.driver-configuration]]",227 "name = \"Firefox Nightly\"",228 "max-sessions = 2",229 "cheese = \"sabana\"",230 };231 Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));232 List<Capabilities> reported = new ArrayList<>();233 try {234 new NodeOptions(config).getSessionFactories(caps -> {235 reported.add(caps);236 return Collections.singleton(HelperFactory.create(config, caps));237 });238 fail("Should have not executed 'getSessionFactories' successfully because driver config " +239 "needs the stereotype field");240 } catch (ConfigException e) {241 // Fall through242 }243 assertThat(reported).isEmpty();244 }245 @Test246 public void shouldNotOverrideMaxSessionsByDefault() {247 assumeTrue("ChromeDriver needs to be available", new ChromeDriverInfo().isAvailable());248 int maxRecommendedSessions = Runtime.getRuntime().availableProcessors();249 int overriddenMaxSessions = maxRecommendedSessions + 10;250 Config config = new MapConfig(251 singletonMap("node",252 ImmutableMap253 .of("max-sessions", overriddenMaxSessions)));254 List<Capabilities> reported = new ArrayList<>();255 try {256 new NodeOptions(config).getSessionFactories(caps -> {257 reported.add(caps);258 return Collections.singleton(HelperFactory.create(config, caps));259 });260 } catch (ConfigException e) {261 // Fall through262 }263 long chromeSlots = reported.stream()264 .filter(capabilities -> "chrome".equalsIgnoreCase(capabilities.getBrowserName()))265 .count();266 assertThat(chromeSlots).isEqualTo(maxRecommendedSessions);267 }268 @Test269 public void canOverrideMaxSessionsWithFlag() {270 assumeTrue("ChromeDriver needs to be available", new ChromeDriverInfo().isAvailable());271 int maxRecommendedSessions = Runtime.getRuntime().availableProcessors();272 int overriddenMaxSessions = maxRecommendedSessions + 10;273 Config config = new MapConfig(274 singletonMap("node",275 ImmutableMap276 .of("max-sessions", overriddenMaxSessions,277 "override-max-sessions", true)));278 List<Capabilities> reported = new ArrayList<>();279 try {280 new NodeOptions(config).getSessionFactories(caps -> {281 reported.add(caps);282 return Collections.singleton(HelperFactory.create(config, caps));283 });284 } catch (ConfigException e) {...
Source:CoreSelfTest.java
...38 public void detectBrowser() {39 browser = System.getProperty("selenium.browser", "*googlechrome");40 switch (browser) {41 case "*firefox":42 assumeNotNull(new GeckoDriverInfo().isAvailable());43 break;44 case "*googlechrome":45 assumeNotNull(new ChromeDriverInfo().isAvailable());46 break;47 default:48 assumeFalse("No known driver able to be found", false);49 }50 }51 @Before52 public void startTestServer() {53 server = new NettyAppServer();54 server.start();55 }56 @After57 public void stopTestServer() {58 server.stop();59 }...
Source:ChromeDriverInfo.java
...25 capabilities.getCapability("chromeOptions") != null ||26 capabilities.getCapability("goog:chromeOptions") != null;27 }28 @Override29 public boolean isAvailable() {30 try {31 ChromeDriverService.createDefaultService();32 return true;33 } catch (IllegalStateException | WebDriverException e) {34 return false;35 }36 }37 @Override38 public int getMaximumSimultaneousSessions() {39 return Runtime.getRuntime().availableProcessors() + 1;40 }41 @Override42 public Optional<WebDriver> createDriver(Capabilities capabilities)43 throws SessionNotCreatedException {44 if (!isAvailable() || !isSupporting(capabilities)) {45 return Optional.empty();46 }47 WebDriver driver = new ChromeDriver(capabilities);48 return Optional.of(driver);49 }50}...
isAvailable
Using AI Code Generation
1import org.openqa.selenium.chrome.ChromeDriverInfo;2public class ChromeDriverInfoExample {3 public static void main(String[] args) {4 ChromeDriverInfo info = new ChromeDriverInfo();5 System.out.println("isAvailable: " + info.isAvailable());6 }7}
isAvailable
Using AI Code Generation
1package com.automation.selenium.utils;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4import org.openqa.selenium.chrome.ChromeDriverInfo;5public class Example1 {6 public static void main(String[] args) {7 ChromeDriverInfo info = new ChromeDriverInfo();8 if (info.isAvailable()) {9 WebDriver driver = new ChromeDriver();10 driver.quit();11 } else {12 System.out.println("Chrome Driver is not available");13 }14 }15}
isAvailable
Using AI Code Generation
1package com.automation.selenium.driver;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4import org.openqa.selenium.chrome.ChromeDriverInfo;5import org.openqa.selenium.chrome.ChromeOptions;6import org.openqa.selenium.remote.DesiredCapabilities;7public class Example1 {8 public static void main(String[] args) {9 WebDriver driver = null;10 try {11 ChromeOptions options = new ChromeOptions();12 options.addArguments("--start-maximized");13 DesiredCapabilities capabilities = DesiredCapabilities.chrome();14 capabilities.setCapability(ChromeOptions.CAPABILITY, options);15 if (ChromeDriverInfo.isAvailable()) {16 driver = new ChromeDriver(capabilities);17 } else {18 System.out.println("Unable to find ChromeDriver");19 }20 Thread.sleep(2000);21 } catch (Exception exception) {22 System.out.println("Exception:" + exception.getMessage());23 } finally {24 if (driver != null) {25 driver.quit();26 }27 }28 }29}
isAvailable
Using AI Code Generation
1import org.openqa.selenium.chrome.ChromeDriverInfo;2ChromeDriverInfo driverInfo = new ChromeDriverInfo();3if(driverInfo.isAvailable()){4 System.out.println("ChromeDriver is available in the system");5}else{6 System.out.println("ChromeDriver is not available in the system");7}8isAvailable() method of ChromeDriverInfo class
isAvailable
Using AI Code Generation
1import org.openqa.selenium.chrome.ChromeDriverInfo;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4public class SeleniumDemo {5 public static void main(String[] args) {6 System.out.println("Is chrome driver available? " + ChromeDriverInfo.isAvailable());7 WebDriver driver = new ChromeDriver();8 System.out.println("Page title is: " + driver.getTitle());9 driver.quit();10 }11}12Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!