This class handles the creation and cleanup of Playwright, Browser, BrowserContext,
and Page instances using thread-local storage for parallel test execution.
Pages are automatically registered with PlaywrightSerenity for screenshot
capture, and constructor injection via @Steps works automatically when
serenity-playwright is on the classpath.
The browser type is read from playwright.browsertype and headless mode from
playwright.headless. For additional launch options, use withOptions(BrowserType.LaunchOptions).
For full property-driven configuration, use the Screenplay BrowseTheWebWithPlaywright ability instead.
Usage with Cucumber:
public class PlaywrightHooks {
private static final PlaywrightBrowserManager browser = PlaywrightBrowserManager.managed();
@Before(order = 100)
public void setUp() { browser.openNewPage(); }
@After(order = 100)
public void tearDown() { browser.closeCurrentContext(); }
@AfterAll
public static void shutdown() { browser.close(); }
}
// In other hook classes, use current() to access the active manager:
public class TracingHooks {
@Before(order = 200)
public void startTracing() {
PlaywrightBrowserManager.current().getCurrentContext().tracing().start(...);
}
}
Usage with JUnit 5 (without @UsePlaywright):
@ExtendWith(SerenityJUnit5Extension.class)
class MyTest {
private static final PlaywrightBrowserManager browser = PlaywrightBrowserManager.managed();
@Steps SearchComponent search;
@BeforeEach void setUp() { browser.openNewPage(); }
@AfterEach void tearDown() { browser.closeCurrentContext(); }
@AfterAll static void shutdown() { browser.close(); }
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the browser, Playwright instance, and all thread-local resources.voidCloses the current browser context and unregisters pages.static PlaywrightBrowserManagercurrent()Returns the active browser manager for the current thread.com.microsoft.playwright.BrowserContextReturns the current browser context for this thread, or null if none is open.com.microsoft.playwright.PageReturns the current page for this thread, or null if none is open.static PlaywrightBrowserManagermanaged()Creates a new browser manager using configuration from Serenity properties.static PlaywrightBrowserManagermanaged(com.microsoft.playwright.junit.OptionsFactory optionsFactory) Creates a browser manager using configuration from a PlaywrightOptionsFactory.static PlaywrightBrowserManagerCreates a browser manager for a specific browser type.com.microsoft.playwright.PageOpens a new browser context and page, registering the page withPlaywrightSerenityfor screenshot capture and@Stepsinjection.withContextOptions(com.microsoft.playwright.Browser.NewContextOptions contextOptions) Returns a copy of this manager with the specified context options applied to new browser contexts.static PlaywrightBrowserManagerwithOptions(com.microsoft.playwright.BrowserType.LaunchOptions launchOptions) Creates a browser manager with explicit launch options.
-
Method Details
-
managed
Creates a new browser manager using configuration from Serenity properties.Reads
playwright.browsertypeandplaywright.headlessfrom Serenity properties. This is a factory method — each call creates a new instance. Store it as a field and usecurrent()from other classes that need access. -
managed
public static PlaywrightBrowserManager managed(com.microsoft.playwright.junit.OptionsFactory optionsFactory) Creates a browser manager using configuration from a PlaywrightOptionsFactory.This applies the full set of options — browser type, launch options, context options,
testIdAttribute,wsEndpoint, etc. — just as@UsePlaywrightdoes. Serenity environment properties (playwright.headless,playwright.browsertype) are used as fallbacks when the corresponding option is not set in the factory. -
managed
Creates a browser manager for a specific browser type.playwright.headlessis still read from Serenity properties. -
withOptions
public static PlaywrightBrowserManager withOptions(com.microsoft.playwright.BrowserType.LaunchOptions launchOptions) Creates a browser manager with explicit launch options.playwright.headlessfrom Serenity properties is applied if not already set. -
current
Returns the active browser manager for the current thread.The current manager is set automatically when
openNewPage()is called. Use this in auxiliary hook classes (e.g. tracing fixtures) that need to access the browser context without holding a direct reference to the manager instance.- Returns:
- the active PlaywrightBrowserManager, or null if no page has been opened on this thread
-
withContextOptions
public PlaywrightBrowserManager withContextOptions(com.microsoft.playwright.Browser.NewContextOptions contextOptions) Returns a copy of this manager with the specified context options applied to new browser contexts. -
openNewPage
public com.microsoft.playwright.Page openNewPage()Opens a new browser context and page, registering the page withPlaywrightSerenityfor screenshot capture and@Stepsinjection.Call this in your
@Before/@BeforeEachhook.- Returns:
- the new Page instance
-
getCurrentPage
public com.microsoft.playwright.Page getCurrentPage()Returns the current page for this thread, or null if none is open. -
getCurrentContext
public com.microsoft.playwright.BrowserContext getCurrentContext()Returns the current browser context for this thread, or null if none is open. -
closeCurrentContext
public void closeCurrentContext()Closes the current browser context and unregisters pages.Call this in your
@After/@AfterEachhook. -
close
public void close()Closes the browser, Playwright instance, and all thread-local resources.Call this in your
@AfterAllhook or test suite teardown.
-