Search⌘ K
AI Features

The Base Test Class

Explore the structure and purpose of the base test class in Selenium Java automation. Understand how it centralizes WebDriver initialization and cleanup, enabling test classes to share common setup while maintaining test independence. Learn to implement test fixtures for efficient browser management and simplify your automated test suite.

The code of the base test class is shown below:

Java
package Tests;
import java.net.MalformedURLException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import PageObjects.HomePage;
import framework.DriverFactory;
import framework.LogWriter;
public class BaseTest {
public WebDriver driver;
private static final String URL = "http://www.vpl.ca";
private LogWriter traceLogWriter = new LogWriter("./target/logs", "trace.log");
@BeforeMethod
public void setUp() throws MalformedURLException
{
String browserName = System.getenv("BROWSER");
this.driver = DriverFactory.getDriver(browserName);
}
@AfterMethod
public void tearDown()
{
driver.quit();
}
public HomePage openHomePage()
{
driver.navigate().to(URL);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(d -> d.getCurrentUrl().contains("vpl.ca"));
traceLogWriter.writeToLog("Open Home Page");
return new HomePage(driver);
}
}

Why is the base test class needed?

The base test class is used as a parent for the test classes to avoid each test class having its own driver object and its own test fixtures. It also allows removing the code duplication for the test fixtures and the driver object.

The base test class should be used by the test classes that only need the driver object to be instantiated before ...

Why is the base class needed?