0
0
Selenium Javatesting~10 mins

Base test class pattern in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses a base test class to set up and tear down the Selenium WebDriver. It verifies that the homepage title is correct after navigating to the website.

Test Code - JUnit 5 with Selenium WebDriver
Selenium Java
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class BaseTest {
    protected WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

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

public class HomePageTest extends BaseTest {

    @Test
    public void testHomePageTitle() {
        driver.get("https://example.com");
        String title = driver.getTitle();
        assertEquals("Example Domain", title);
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts and BaseTest setUp() method runsChrome browser opens and window is maximized-PASS
2HomePageTest navigates to https://example.comBrowser loads the Example Domain homepage-PASS
3HomePageTest retrieves the page titlePage title is 'Example Domain'Verify page title equals 'Example Domain'PASS
4HomePageTest assertion checks title equalityTitle matches expected valueassertEquals("Example Domain", title)PASS
5BaseTest tearDown() method runsBrowser closes-PASS
Failure Scenario
Failing Condition: Page title does not match 'Example Domain' or element not found
Execution Trace Quiz - 3 Questions
Test your understanding
What is the purpose of the BaseTest class in this test?
ATo set up and tear down the WebDriver for all tests
BTo store test data for the homepage
CTo define the test assertions
DTo navigate to the website
Key Result
Using a base test class helps avoid repeating setup and cleanup code, making tests cleaner and easier to maintain.