java.lang.Object
net.serenitybdd.screenplay.actors.Cast
net.serenitybdd.screenplay.playwright.actors.PlaywrightCast

public class PlaywrightCast extends net.serenitybdd.screenplay.actors.Cast
A Cast implementation that automatically provides actors with the ability to browse the web using Playwright.

This class simplifies Screenplay tests by managing actor lifecycle and Playwright browser instances. Actors created through this Cast will automatically have the BrowseTheWebWithPlaywright ability.

Usage with JUnit 5:


 @BeforeEach
 void setupStage() {
     OnStage.setTheStage(new PlaywrightCast());
 }

 @AfterEach
 void teardownStage() {
     OnStage.drawTheCurtain();
 }

 @Test
 void shouldSearchForProducts() {
     Actor customer = OnStage.theActorCalled("Customer");
     customer.attemptsTo(
         Open.url("https://example.com"),
         SearchFor.product("Widget")
     );
 }
 

Usage with Cucumber:

Create a hooks class in your step definitions package:


 public class PlaywrightHooks {

     @Before(order = 0)
     public void setTheStage(Scenario scenario) {
         OnStage.setTheStage(new PlaywrightCast());
     }

     @ParameterType(".*")
     public Actor actor(String actorName) {
         return OnStage.theActorCalled(actorName);
     }
 }
 

The cleanup is handled automatically by Serenity's StageDirector when you include net.serenitybdd.cucumber.actors in your Cucumber glue path:


 @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "your.steps,net.serenitybdd.cucumber.actors")
 

The OnStage.drawTheCurtain() call will automatically clean up all browser resources.

  • Constructor Details

    • PlaywrightCast

      public PlaywrightCast()
    • PlaywrightCast

      public PlaywrightCast(net.serenitybdd.screenplay.Ability[] abilities)
    • PlaywrightCast

      @SafeVarargs public PlaywrightCast(Consumer<net.serenitybdd.screenplay.Actor>... providers)
  • Method Details

    • whereEveryoneCan

      public static PlaywrightCast whereEveryoneCan(net.serenitybdd.screenplay.Ability... abilities)
      Create a PlaywrightCast where all actors have the specified abilities in addition to BrowseTheWebWithPlaywright.
    • whereEveryoneCan

      @SafeVarargs public static PlaywrightCast whereEveryoneCan(Consumer<net.serenitybdd.screenplay.Actor>... abilityProviders)
      Create a PlaywrightCast where each actor is configured using the provided function.
    • withLaunchOptions

      public PlaywrightCast withLaunchOptions(com.microsoft.playwright.BrowserType.LaunchOptions options)
      Configure launch options for the Playwright browser.
    • withContextOptions

      public PlaywrightCast withContextOptions(com.microsoft.playwright.Browser.NewContextOptions options)
      Configure context options for browser contexts.
    • withBrowserType

      public PlaywrightCast withBrowserType(String browserType)
      Set the browser type (chromium, firefox, or webkit).
    • withOptions

      public PlaywrightCast withOptions(com.microsoft.playwright.junit.OptionsFactory optionsFactory)
      Configure the cast using a Playwright OptionsFactory.

      This allows reusing the same OptionsFactory that you use with @UsePlaywright when setting up a Screenplay stage.

    • withHeadlessMode

      public PlaywrightCast withHeadlessMode(boolean headless)
      Set headless mode for the browser.
    • actorNamed

      public net.serenitybdd.screenplay.Actor actorNamed(String actorName, net.serenitybdd.screenplay.Ability... abilities)
      Overrides:
      actorNamed in class net.serenitybdd.screenplay.actors.Cast