0
0
Selenium Javatesting~15 mins

WebDriverManager for automatic driver management in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Automate browser launch using WebDriverManager
Preconditions (2)
Step 1: Use WebDriverManager to setup ChromeDriver automatically
Step 2: Launch a new Chrome browser instance
Step 3: Navigate to 'https://example.com'
Step 4: Verify the page title is 'Example Domain'
Step 5: Close the browser
✅ Expected Result: Chrome browser opens, navigates to 'https://example.com', page title matches 'Example Domain', and browser closes without errors
Automation Requirements - Selenium WebDriver with WebDriverManager
Assertions Needed:
Assert that the page title equals 'Example Domain'
Best Practices:
Use WebDriverManager to manage driver binaries automatically
Use explicit waits if needed (not required here as page loads quickly)
Close the browser in a finally block or use try-with-resources
Use By locators only when interacting with page elements (not needed here)
Automated Solution
Selenium Java
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.

Common Mistakes - 4 Pitfalls
Manually downloading and setting driver path instead of using WebDriverManager
Not closing the browser after test execution
Using Thread.sleep() to wait for page load
Catching generic Exception instead of AssertionError for test assertions
Bonus Challenge

Now add data-driven testing to open multiple URLs and verify their titles

Show Hint