liveBook · Manning

Cast

As explained in BDD in Action, Second Edition

The Cast interface is the Serenity/JS contract responsible for providing actors configured with their abilities and initial state.

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

How BDD in Action, Second Edition covers it

How deep it goes
Explains actor management, ability assignment, and setup, but does not descend into Cast implementation internals.
Examples
Uses OnlineCast, generic casts, Playwright browser setup, named actors, and spotlight actors in step definitions.
What it assumes
Assumes familiarity with Screenplay actors, abilities, Cucumber steps, and a web integration tool such as Playwright or WebDriver.
What you can do after
Configure actors before scenarios and retrieve them reliably while implementing Cucumber step definitions.
How it connects
Connects Actor, abilities, OnStage, Cucumber, the Screenplay Pattern, and Playwright integration.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of BDD in Action, Second Edition. The section links point to the parts of the book it draws on.