27. Reusable Test Fixtures & Base Test Isolation

BaseTest Lifecycle Fixture Pattern

public class BaseTest {
    protected static Playwright playwright;
    protected static Browser browser;
    protected BrowserContext context;
    protected Page page;

public void initSuite() {
playwright = Playwright.create();
browser = playwright.chromium().launch();
}

public void initTest() {
context = browser.newContext();
page = context.newPage();
}

public void cleanupTest() {
if (context != null) context.close();
}

public void cleanupSuite() {
if (browser != null) browser.close();
if (playwright != null) playwright.close();
}
}

Task

  • Implement createContextAndPage:
  • 1. Accept Browser browser.
  • 2. Create new context using browser.newContext().
  • 3. Return context.newPage().
πŸ’‘Common doubts
Did you like the problem?
Lesson 27 of 38
Java
Loading code editor…
Next