WebDriverManager for automatic driver management in Selenium Java - Build an Automation Script
import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import static org.junit.jupiter.api.Assertions.assertEquals; public class WebDriverManagerTest { public static void main(String[] args) { WebDriver driver = null; try { // Setup ChromeDriver automatically WebDriverManager.chromedriver().setup(); // Launch Chrome browser driver = new ChromeDriver(); // Navigate to example.com driver.get("https://example.com"); // Verify page title String title = driver.getTitle(); assertEquals("Example Domain", title); System.out.println("Test Passed: Page title is 'Example Domain'"); } catch (AssertionError e) { System.out.println("Test Failed: " + e.getMessage()); } finally { if (driver != null) { driver.quit(); } } } }
This code uses WebDriverManager to automatically download and setup the correct ChromeDriver binary for the current system. This removes the need to manually download or set system properties for the driver.
We create a ChromeDriver instance to launch the browser, then navigate to 'https://example.com'. We get the page title and assert it equals 'Example Domain' using JUnit's assertEquals method.
The try-finally block ensures the browser closes even if the assertion fails, preventing leftover browser processes.
This simple example shows how WebDriverManager simplifies driver management and how to verify page title with Selenium.
Now add data-driven testing to open multiple URLs and verify their titles
Practice
WebDriverManager in Selenium Java tests?Solution
Step 1: Understand WebDriverManager's role
WebDriverManager automates the setup of browser drivers needed for Selenium tests.Step 2: Compare with other options
Options A, B, and C relate to other Selenium tasks, not driver management.Final Answer:
To automatically download and manage browser drivers -> Option AQuick Check:
WebDriverManager = automatic driver setup [OK]
- Confusing driver management with test assertions
- Thinking it creates test reports
- Assuming it locates web elements
Solution
Step 1: Recall WebDriverManager syntax
The correct method call isWebDriverManager.chromedriver().setup();to setup ChromeDriver.Step 2: Identify syntax errors in other options
Options A, C, and D have incorrect method chaining or casing.Final Answer:
WebDriverManager.chromedriver().setup(); -> Option DQuick Check:
Correct method chaining = WebDriverManager.chromedriver().setup(); [OK]
- Incorrect method chaining order
- Wrong capitalization of 'chromedriver'
- Using parentheses incorrectly
WebDriverManager.firefoxdriver().setup();
WebDriver driver = new FirefoxDriver();
driver.get("https://example.com");
System.out.println(driver.getTitle());Solution
Step 1: Analyze WebDriverManager usage
The code callsWebDriverManager.firefoxdriver().setup();which downloads and sets up FirefoxDriver automatically.Step 2: Check driver creation and navigation
Then it creates a FirefoxDriver instance, opens the URL, and prints the page title.Final Answer:
It opens Firefox, navigates to example.com, and prints the page title -> Option AQuick Check:
Setup + FirefoxDriver + getTitle() = It opens Firefox, navigates to example.com, and prints the page title [OK]
- Assuming driver setup is manual and missing
- Confusing FirefoxDriver with ChromeDriver
- Expecting no browser to open
WebDriverManager.chromedriver.setup(); WebDriver driver = new ChromeDriver();
Solution
Step 1: Check WebDriverManager method call
The methodchromedrivermust be called with parentheses:chromedriver().Step 2: Verify other options
ChromeDriver class is not imported might be true but is not shown in snippet; WebDriverManager cannot setup ChromeDriver is false because WebDriverManager supports ChromeDriver; No error, code is correct is incorrect due to syntax error.Final Answer:
Missing parentheses after chromedriver() method -> Option BQuick Check:
Method calls need parentheses [OK]
- Omitting parentheses on method calls
- Assuming WebDriverManager can't setup ChromeDriver
- Ignoring import statements
Solution
Step 1: Understand the problem
Running tests on multiple browsers requires managing different drivers automatically.Step 2: Identify WebDriverManager feature
WebDriverManager automatically detects browser version and downloads matching drivers, avoiding manual setup.Final Answer:
Automatic detection and download of required driver versions -> Option CQuick Check:
Auto driver management = Automatic detection and download of required driver versions [OK]
- Thinking manual config is needed
- Trying to write custom scripts unnecessarily
- Using outdated driver files
Ready to prove it?
Pass the test and this pattern moves unknown
