0
0
Selenium Javatesting~15 mins

WebDriver setup (ChromeDriver) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Setup ChromeDriver and open Google homepage
Preconditions (3)
Step 1: Set system property for ChromeDriver with the correct path
Step 2: Create a new instance of ChromeDriver
Step 3: Navigate to 'https://www.google.com'
Step 4: Verify the page title is 'Google'
Step 5: Close the browser
✅ Expected Result: Chrome browser opens, navigates to Google homepage, page title is 'Google', and browser closes without errors
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify page title equals 'Google'
Best Practices:
Use WebDriverManager or set system property for ChromeDriver path
Use try-finally or @After method to close browser
Use explicit waits if needed (not required here as page load is simple)
Use By locators only when interacting with page elements (not needed here)
Automated Solution
Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ChromeDriverSetupTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() {
        // Set the path to chromedriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    }

    @Test
    public void openGoogleHomePage() {
        driver.get("https://www.google.com");
        String title = driver.getTitle();
        Assertions.assertEquals("Google", title, "Page title should be 'Google'");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

The setUp method sets the system property to tell Selenium where the ChromeDriver executable is located, then creates a new ChromeDriver instance.

The test method openGoogleHomePage navigates to Google and asserts the page title is exactly 'Google'.

The tearDown method ensures the browser closes after the test to free resources.

This structure uses JUnit 5 annotations for setup and cleanup, which is a best practice for test automation.

Common Mistakes - 3 Pitfalls
{'mistake': 'Not setting the system property for ChromeDriver path', 'why_bad': 'Without setting the path, Selenium cannot find the ChromeDriver executable and will throw an error.', 'correct_approach': "Always set the system property 'webdriver.chrome.driver' with the correct path before creating ChromeDriver instance."}
Not closing the browser after test
Hardcoding incorrect ChromeDriver path
Bonus Challenge

Modify the test to use WebDriverManager to setup ChromeDriver automatically instead of setting system property manually.

Show Hint