A base test class helps you write tests faster by putting common setup and cleanup steps in one place. It keeps your test code clean and easy to manage.
0
0
Base test class pattern in Selenium Java
Introduction
When you have many tests that need the same browser setup.
When you want to open and close the browser before and after each test automatically.
When you want to share common helper methods across multiple test classes.
When you want to avoid repeating code in every test class.
When you want to keep your test code organized and easy to update.
Syntax
Selenium Java
public class BaseTest { protected WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://example.com"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
The base class uses @BeforeEach to run setup before every test.
The driver is protected so child test classes can use it.
Examples
This example uses FirefoxDriver instead of ChromeDriver.
Selenium Java
public class BaseTest { protected WebDriver driver; @BeforeEach public void setUp() { driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://example.com"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This example adds an implicit wait to handle slow page loads.
Selenium Java
public class BaseTest { protected WebDriver driver; @BeforeEach public void setUp() { driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); driver.get("https://example.com"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
Sample Program
This test class HomePageTest extends the base class. It opens the browser, goes to example.com, and checks the page title. The browser closes automatically after the test.
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(); driver.get("https://example.com"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } } public class HomePageTest extends BaseTest { @Test public void testHomePageTitle() { String title = driver.getTitle(); assertEquals("Example Domain", title); } }
OutputSuccess
Important Notes
Always quit the driver in @AfterEach to close the browser and free resources.
Use protected access for the WebDriver so child classes can use it but it stays hidden from outside.
Keep your base test class simple and focused on setup and teardown only.
Summary
A base test class saves time by sharing setup and cleanup code.
Child test classes extend the base class to reuse the WebDriver and setup steps.
This pattern keeps tests clean, organized, and easy to maintain.