0
0
Selenium Javatesting~15 mins

Java environment setup (JDK, IDE) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify Java environment setup by running a simple Selenium test
Preconditions (3)
Step 1: Open the IDE and create a new Java project
Step 2: Add Selenium WebDriver dependency to the project (e.g., via Maven or manually)
Step 3: Create a new Java class named 'EnvironmentSetupTest'
Step 4: Write a simple Selenium test that opens a browser and navigates to 'https://example.com'
Step 5: Run the test from the IDE
✅ Expected Result: The browser opens and navigates to 'https://example.com' without errors, and the test completes successfully
Automation Requirements - Selenium WebDriver with JUnit 5
Assertions Needed:
Verify the page title is 'Example Domain' after navigation
Best Practices:
Use explicit waits if needed
Use WebDriverManager to manage browser drivers automatically
Use JUnit 5 annotations for setup and teardown
Keep code clean and readable
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.assertEquals;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class EnvironmentSetupTest {
    private WebDriver driver;

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

    @Test
    public void testOpenExampleDotCom() {
        driver.get("https://example.com");
        String title = driver.getTitle();
        assertEquals("Example Domain", title, "Page title should be 'Example Domain'");
    }

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

This test class EnvironmentSetupTest uses JUnit 5 and Selenium WebDriver to verify the Java environment setup.

setUp() method runs before each test. It uses WebDriverManager to automatically download and configure the ChromeDriver, then creates a new Chrome browser instance.

testOpenExampleDotCom() opens the URL 'https://example.com' and asserts the page title is exactly 'Example Domain'. This confirms Selenium and Java are working correctly.

tearDown() closes the browser after the test to clean up resources.

This structure follows best practices: using WebDriverManager avoids manual driver setup, JUnit 5 annotations organize setup and cleanup, and the assertion verifies the test outcome clearly.

Common Mistakes - 4 Pitfalls
Not setting up the browser driver correctly
Not closing the browser after test
Using outdated JUnit 4 annotations with JUnit 5 dependencies
Hardcoding driver executable paths
Bonus Challenge

Now add data-driven testing with 3 different URLs and verify their page titles

Show Hint