The Base Test Class

Learn how to set up the base test class to make the test fixtures, the driver object, and the method for opening the site's Home Page reusable.

The code of the base test class is shown below:

Press + to interact
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 ...

Why is the base class needed?