QA Interview Hub · Knowledge base · Quizzes · Українська

Patterns › Screenplay: an overview

What Screenplay is

Screenplay builds a test around people, not screens: an actor with abilities performs tasks composed of interactions and asks questions about the system. The roles Actor, Ability, Task, Interaction and Question separate what, how and with what. In TypeScript it is implemented by Serenity/JS. It pays off in complex domains with several roles; in a small app it only adds layers.

Patterns · Screenplay

A scenario has a customer, an operator and an admin. Who orchestrates whom if all we have is pages?

The test “an operator approves a large transfer” involves two people and two pages. With Page Objects the test creates two contexts and two pages by hand and arranges who does what in its own variables. The more roles, the more of this orchestration sits in the test body. Screenplay builds the test around people, not screens, so a role becomes a first-class thing, not a variable.

How it works and why. Screenplay grew out of the idea of describing automation in the language of interaction designers: roles, goals, tasks, actions (Kevin Lawrence and Antony Marcano, 2007; later it went into Serenity BDD, and for TypeScript it is implemented by Serenity/JS). The core concepts:

  • Actor: who acts (“Olena, a bank customer”).
  • Ability: what the actor can do. It is access to the outside world: drive a browser, call an API, take notes. The Playwright Test adapter gives every actor the abilities to drive a browser, call an API and take notes without extra code.
  • Task: a business task made of other tasks and interactions (“Transfer money”).
  • Interaction: a low-level action: a click, typing text.
  • Question: what the actor can learn about the system: text, state, an API response. Questions are used in checks.

Why so many layers. Each has one responsibility: a task says what, an interaction says how in this interface, an ability provides with what. The same actor with another ability (an API instead of a browser) performs the same task by another path. So tasks are small and easy to combine, while a Page Object that collects everything from a page grows.

Five concepts

Actorwho acts: Olena, a bank customerAbilitywhat they can do: drive a browser, call an APITaska business task: “transfer money”Interactionlow level: a click, typing textQuestionwhat the actor learns: text, state, a response
Figure 1. A task consists of interactions and the actor performs it with their abilities.

Example: before and after. Before: Page Objects and two contexts that the test holds by hand.

test('operator approves a large transfer', async ({ browser }) => {
  const c = await browser.newContext({ storageState: '.auth/customer.json' });
  const o = await browser.newContext({ storageState: '.auth/operator.json' });
  const customer = new TransferPage(await c.newPage());
  const operator = new ReviewPage(await o.newPage());

  await customer.submit('UA002', 50_001);
  await operator.approveLatest();
  await expect(customer.status).toHaveText('Approved');
});

After, on Serenity/JS. First a task: it is composed of ready interactions. Four places:

import { Task } from '@serenity-js/core';
import { By, Click, Enter, PageElement } from '@serenity-js/web';

const field = (testId: string) =>
  PageElement.located(By.css('[data-testid="' + testId + '"]'));

export const TransferMoney = {
1  of: (amount: number) => ({
    to: (account: string) =>
2      Task.where(
3        '#actor transfers ' + amount + ' to ' + account,
4        Enter.theValue(account).into(field('to')),
        Enter.theValue(String(amount)).into(field('amount')),
        Click.on(field('confirm')),
      ),
  }),
};
  1. The fluent form of(100).to(...) makes the line in the scenario readable.
  2. A task is built with Task.where: a description and a list of actions.
  3. #actor in the description is replaced with the actor's name in the report: “Olena transfers 100 to UA002”.
  4. Enter and Click are ready interactions, not page methods.

Now the test itself. It reads as a behaviour description: an actor performs tasks and asks questions.

import { actorCalled } from '@serenity-js/core';
import { Navigate, Text } from '@serenity-js/web';
import { Ensure, equals } from '@serenity-js/assertions';

await actorCalled('Olena').attemptsTo(
  Navigate.to('/transfers'),
  TransferMoney.of(100).to('UA002'),
  Ensure.that(Text.of(field('balance')), equals('900.00 EUR')),
);

Limits and alternatives. Screenplay pays off when there are many roles and a complex domain. For a small app with one role it adds layers without solving a problem. Also, the team has to learn the model and the library, and locators and actions still have to be written. The comparison with Page Object and where it applies is in the next lesson.

What a class is about

Page Object

Here is a page

A class describes a screen: locators and methods. The test assembles a scenario from pages.

Screenplay

Here is an actor

A class describes behaviour: tasks and questions. The scenario reads as a person's story.

Figure 2. Two equal models: Page Object groups by screen, Screenplay by behaviour.

Common mistakes.

  • A task made of one interaction (“ClickConfirm”): that is a wrapper without business meaning, and a task should be named in the domain language.
  • A task named by the interface, not by intent (“FillFormAndPressButton”): when the UI changes, the name changes too.
  • One actor for the whole suite that changes data: roles and state mix; create an actor per scenario.
  • A check inside a task: it hides the expectation. Check with questions through Ensure.that in the scenario.
  • Taking Screenplay only because it is fashionable, with no complex domain and several roles.

Check yourself

How does a Task differ from an Interaction and why separate them?

Answer

An Interaction is one action in a concrete interface (a click, typing), a Task is a business task composed of other tasks and interactions. The split separates intent from method: when the UI changes the interactions are edited and the scenario with tasks stays.

Why does Screenplay have Questions if the value can be read from an element directly?

Answer

A question is part of the scenario language: “what the actor sees” reads the same whether it is text on a page or an API response. It can be reused in checks and combined with Ensure.that, without knowing about the locator in the test.

In an interview. Junior: Screenplay models a test through actors, their abilities, tasks and questions to the system, not through pages. In TypeScript it is implemented by Serenity/JS. Middle/Senior: It separates intent from method: a task says what, an interaction says how, an ability says with what. It scales well to complex domains with several roles, but has a higher entry cost, so before choosing it I check that there is a real problem it solves.

More articles in this chapter