0
0
Selenium Javatesting~15 mins

Selenium dependency configuration in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Configure Selenium WebDriver dependencies in a Java Maven project
Preconditions (3)
Step 1: Create a new Maven project in your IDE
Step 2: Open the pom.xml file
Step 3: Add the Selenium Java dependency with the latest stable version inside the <dependencies> tag
Step 4: Add the WebDriverManager dependency to manage browser drivers automatically
Step 5: Save the pom.xml file and let Maven download the dependencies
Step 6: Create a simple Java class to verify Selenium WebDriver can be instantiated
Step 7: Write code to open a browser and navigate to https://example.com
Step 8: Run the Java class and observe the browser opening
✅ Expected Result: Maven downloads Selenium and WebDriverManager dependencies successfully. The Java program launches a browser window and navigates to https://example.com without errors.
Automation Requirements - JUnit 5 with Selenium WebDriver
Assertions Needed:
Verify that the WebDriver instance is not null
Verify the current URL is https://example.com after navigation
Best Practices:
Use WebDriverManager to handle browser driver binaries automatically
Use explicit waits if needed (not required here as navigation is simple)
Close the browser after test execution
Use JUnit 5 annotations for setup and teardown
Automated Solution
Selenium Java
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class SeleniumDependencyConfigTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
    }

    @Test
    public void testOpenExampleDotCom() {
        assertNotNull(driver, "WebDriver should be initialized");
        driver.get("https://example.com");
        String currentUrl = driver.getCurrentUrl();
        assertEquals("https://example.com/", currentUrl, "URL should be https://example.com/");
    }

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

The @BeforeEach method sets up the WebDriver using WebDriverManager, which downloads and configures the ChromeDriver automatically. This avoids manual driver setup.

The test method testOpenExampleDotCom verifies the WebDriver instance is created and navigates to https://example.com. It asserts the current URL matches the expected URL with a trailing slash, which browsers add automatically.

The @AfterEach method closes the browser to clean up resources after the test.

This setup ensures dependencies are correctly configured and Selenium WebDriver works as expected in a Maven Java project.

Common Mistakes - 4 Pitfalls
Manually downloading and setting the ChromeDriver executable path
Not adding Selenium dependencies in pom.xml, causing compilation errors
Not closing the browser after test execution
Using outdated Selenium or WebDriverManager versions
Bonus Challenge

Now add data-driven testing to open three different URLs and verify each loads correctly

Show Hint