Class PlaywrightBrowserManager

java.lang.Object
net.serenitybdd.playwright.PlaywrightBrowserManager

public class PlaywrightBrowserManager extends Object
Manages the Playwright browser lifecycle for use with any test framework.

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 Details

    • managed

      public static PlaywrightBrowserManager managed()
      Creates a new browser manager using configuration from Serenity properties.

      Reads playwright.browsertype and playwright.headless from Serenity properties. This is a factory method — each call creates a new instance. Store it as a field and use current() 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 Playwright OptionsFactory.

      This applies the full set of options — browser type, launch options, context options, testIdAttribute, wsEndpoint, etc. — just as @UsePlaywright does. Serenity environment properties (playwright.headless, playwright.browsertype) are used as fallbacks when the corresponding option is not set in the factory.

    • managed

      public static PlaywrightBrowserManager managed(String browserType)
      Creates a browser manager for a specific browser type. playwright.headless is 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.headless from Serenity properties is applied if not already set.
    • current

      public static PlaywrightBrowserManager 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 with PlaywrightSerenity for screenshot capture and @Steps injection.

      Call this in your @Before / @BeforeEach hook.

      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 / @AfterEach hook.

    • close

      public void close()
      Closes the browser, Playwright instance, and all thread-local resources.

      Call this in your @AfterAll hook or test suite teardown.