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

> The Screenplay Pattern: Better Interactions for Better Automation - Andrew Knight. Canonical page: <https://zapple.tech/blog/aqa-process/andrew-knight-the-screenplay-pattern-better-interactions-for-better-automation/>

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.

*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](/wp-content/uploads/2022/05/Raw-WebDriver-calls-e1652298834474-300x166.png.webp)

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](/wp-content/uploads/2022/05/Reconsidering-interactions-e1652299013835-300x168.png.webp)

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.

## 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

## FAQ

### 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.
