Can we use analytics cookies? They count visits and form use. We run no ads.

AQA ProcessAQA Virtual Summit/Meetups

Andrew Knight – The Screenplay Pattern: Better Interactions for Better Automation

Published 11 May 2022. Updated with a short answer, key takeaways and a FAQ; the talk text is unchanged. Published by Zapple.Tech, founded by the founders of CodeceptJS and Codeception, open-source testing frameworks.

Watch on YouTube
Short answer

In this talk, Andrew Knight explains the Screenplay pattern, which models test interactions as actors using abilities to perform actions. He covers why raw Selenium WebDriver calls duplicate code and race with page loading, the benefits of Screenplay, and how the open-source C# library Boa Constrictor applies it.

Key takeaways
  • Interactions plus verification as a definition of testing, shown on a DuckDuckGo search test
  • Why raw WebDriver code duplicates logic and races with the page
  • The Actor, ability and interaction roles that make up Screenplay
  • Five benefits: reusable, composable, easy waiting, readable, and not limited to web UI
  • Using Screenplay through the Boa Constrictor C# library

Andrew “Pandy” Knight, Lead Software Engineer in Test PrecisionLender, a Q2 Company, and Lead Developer for Boa Constrictor, talked about the challenges of traditional interactions, the benefits of Screenplay, and using Screenplay with Boa Constrictor.

If you’re new to visual testing and have no idea what applitools are, there’s a quick way to figure things out.In these two almost identical images, you can see some differences. To be precise, there are 10 of them. This is the power of the visual AI developed by Applitools it can detect any differences between pictures in a split second.

Screenplay pattern – a new way to automate your interactions

Screenplay pattern is not entirely new, it has been around for several years. But still not familiar to many testers. First, we need to understand what interactions are. Interactions – how users operate software: loads, clicks, scrapes, etc. And the easiest way to define testing is interactions plus verification, i.e., you do something and then make sure that it works.As an example of a search test, consider the DuckDuckGo search engine. It’s simple here:

  • Opening a search engine requires navigation.
  • Searching for a phrase requires pressing the enter key and the search button.
  • Checking the results requires clearing the page title and links to the results.

While most teams use Selenium WebDriver, they all do it differently. There is a problem of code duplication and unreliability, so it’s time to consider how teams develop their interactions based on WebDriver.When teams start writing test automation code with Selenium WebDriver, they often write raw calls. Let’s look at some of them:

  • The WebDriver object is initialized with the Chrome driver (if we’re testing Chrome).
  • The first step is to open the search engine by calling driver.navigate.GoToURL (“DuckDuckGo website address”).
  • The second step searches by retrieving the web elements using driver.findelement (it has multiple locators), and calls methods like send keys and click.
  • The third step uses the headers to check the contents of the page header and the existence of links to the results.
  • WebDriver closes the web browser for proper cleaning at the end of the test.

Raw WebDriver calls

The problem is that there are at least 3 race conditions in this code, in which the automation does not wait for the page to be ready for interactions. WebDriver does not automatically wait for elements to load or headers to appear, you must do this manually. And this is one of the reasons why we call such tests unreliable.But there must be a better way to handle interactions, right? Fortunately, it exists. Let’s take a closer look at how the interactions happen.

Reconsidering interactions

  1. There is someone who initiates the interaction – the user. Let’s call them Actor.
  2. There is a product under test – in our case, this is a web app.
  3. There are interactions themselves – for a web app, these can be simple keystrokes or more complex actions (login to the app or search for a page).
  4. Finally, there are the objects themselves, which allow users to make interactions. Let’s call them “ability.”

All this can be summarized: Users use abilities to perform interactions. This is the essence of the Screenplay pattern.

Benefits of Screenplay

  1. Rich, reusable, reliable interactions.
  2. Interactions are composable.
  3. Easy waiting.
  4. Readable, understandable calls.
  5. Covers all interactions – not just Web UI.

Boa Constrictor

Boa Constrictor — open-source C# implementation of the Screenplay Pattern, developed by PrecisionLender, a Q2 company. It can be used with any .NET test framework, like SpecFlow or NUnit. It helps you make better interactions for better automation.

Questions about this talk

What is the Screenplay pattern in test automation?

The Screenplay pattern models test interactions as users (actors) who use abilities to perform interactions. In the talk, Andrew Knight describes it as a way to build rich, reusable, composable interactions that are easy to read, instead of writing raw WebDriver calls directly inside each test.

Why are raw Selenium WebDriver calls unreliable?

Andrew Knight points out that WebDriver does not wait automatically for elements to load or headers to appear, so you must add waits by hand. In his DuckDuckGo search example the raw code contains at least three race conditions, which is one reason such tests turn out unreliable.

What is Boa Constrictor?

Boa Constrictor is an open-source C# implementation of the Screenplay pattern, developed by PrecisionLender, a Q2 company. It works with any .NET test framework, such as SpecFlow or NUnit. Andrew Knight, its lead developer, shows how to use Screenplay with it to get better interactions.

Request a QA audit quote

Tell us what you ship and what worries you.

What happens after you send this

  1. You send the form: your situation and what you need tested, then your name and work email.
  2. Within one business day, we reply with a suggested first step, its scope, timeline and a quote.
  • Fixed scope
  • You decide after each step
  • The tests live in your repository
  • An NDA signed before access

“They helped us to close the positions and consult our team how to proceed further. High-quality quick feedbacks with the deep checking of the required tasks, professional approach, great understanding of the company's technologies and product, detailed reports.”

Kristina Lavrentieva, Manager at keepgo.com, Israel
  • Rated 4.9 out of 5 on Clutch, from 7 client reviews
  • 42 completed test automation and infrastructure projects
  • Founded by the founders of CodeceptJS and Codeception, open-source testing frameworks

Request a QA audit quote

    What is closest to your situation?
    Optional. The product, the platforms, how often you release, what broke last.

    You decide after our reply whether to go ahead.

    Prefer to talk first? Book a call

    One-week QA audit · written report

    Request a QA audit quote