When Cucumber steps run across multiple methods, recreating actors loses their abilities and state. A Cast keeps scenario actors available by name and configures them consistently. section 12.10.1
A Cast is the Serenity/JS contract used to provide actors with their abilities and initial state. Configure it before scenarios run, then retrieve actors through the framework rather than tracking Actor objects manually. section 12.10.1 section 14.3
A typical Cast setup follows this flow:
- Choose the actors or personas that scenarios will engage. section 14.2.2
- Assign abilities such as browser interaction, including a shared generic ability when actor personas do not need to differ. section 14.2.2
- Register the Cast before scenarios begin, using OnStage or Serenity/JS configuration. section 12.10.1 section 14.2.2
- Retrieve the named actor, or the actor in the spotlight, from step definitions. section 12.10.5 section 14.3
A generic browser cast
With Cucumber.js and Playwright, configure `Cast.whereEveryoneCan(BrowseTheWebWithPlaywright.using(browser))` in `BeforeAll`, then close the browser in `AfterAll`. Every actor receives the same browser ability while the Screenplay interface remains consistent. section 14.2.2
Listing 14.1 Configuring a generic cast of actors to use Playwright
import { BeforeAll, AfterAll } from '@cucumber/cucumber'
import { configure, Cast } from '@serenity-js/core'
import { BrowseTheWebWithPlaywright } from '@serenity-js/playwright'
import * as playwright from 'playwright'
let browser: playwright.Browser
BeforeAll(async () => { #1
browser = await playwright.chromium.launch({ #2
headless: true,
})
configure({ #3
actors: Cast.whereEveryoneCan(
BrowseTheWebWithPlaywright.using(browser), #4
)
})
})
AfterAll(async () => { #5
await browser.close()
})
Inspect the complete lifecycle: launch Playwright, configure the generic Cast, provide the browser ability, and close the browser after all scenarios. section 14.2.2
