Overview

13 Spotlight patterns for automating narrative flow

```html

BDD scenarios are presented as cohesive business narratives, not as isolated technical steps, so the chapter focuses on automation patterns that preserve readability while allowing steps to share knowledge. The main idea is to keep the important entities, actions, and outcomes “in the spotlight” so the scenario stays concise, natural, and focused on the behavior being demonstrated. Rather than forcing every step to be self-contained, the automation should reflect the flow of the story and support reuse of step definitions as building blocks.

To achieve this, the chapter first introduces scenario context as a temporary memory that exists only for the lifetime of one scenario execution. A simple form of this is storing data in instance fields of a step definition class, which works when related steps live in the same class, while avoiding static fields that would break isolation and cause flaky tests. When multiple step definition classes need access to the same information, a shared context object can be injected through dependency injection, allowing data such as the logged-in customer to be passed safely between classes without compromising scenario independence.

The chapter then expands the idea of narrative flow with explicit language for introducing and referring to entities. It recommends using different phrases for newly introduced entities versus already known ones, and shows how custom parameter types can support flexible references like “the order,” “the placed order,” or even “they” when the meaning is unambiguous. For more complex cases, where a scenario must choose one item from a collection, the Entity Selector pattern lets business language trigger domain logic such as selecting the earliest order, making scenarios more readable and step definitions more reusable while keeping the automation aligned with the story the scenario tells.

```
Class diagram showing constructor injection of shared context class

Summary

  • Step definitions are expected to be reusable in many scenarios.
  • Step definitions are not expected to be context independent. They should be designed to rely on knowledge already present in the scenario’s execution context.
  • BDD execution frameworks provide mechanisms for ensuring that the scenario’s execution context is started afresh at the beginning of each scenario execution. Without this guarantee of isolation the outcome of a scenario might depend on the order that the scenarios are run in.
  • The Shareable scenario context and Local scenario context patterns help automate context-aware steps by providing standardized ways to share and interact with the scenario execution context.
  • Scenarios specify the expected behavior by describing how actors perform their tasks by interacting with various elements of the domain. We use the generic term, entity, to refer to any actor or domain element in a scenario.
  • The Introduce and refer pattern helps keep scenarios readable by suggesting phrases that distinguish between a new entity being introduced and an existing entity being referred to.
  • The Entity selector pattern is used to extend custom parameter type definitions to support relevant domain phrases (e.g., “latest order”). These phrases are resolved by querying the scenario context or other data sources.

FAQ

What is the main goal of the Spotlight pattern language in BDD scenario automation?It helps keep the scenario’s focus on the most important business details, reducing repetition and making the narrative read more like natural language while still supporting automation.
What is scenario context in a BDD execution framework?Scenario context is the transient memory available during a scenario run. It stores data needed by later steps and is discarded when the scenario finishes, keeping executions isolated.
What problem does the Local Scenario Context pattern solve?It lets step definitions in the same class remember values from earlier steps, so scenarios can avoid repeating data like names or IDs within the same execution flow.
Why should static fields be avoided in BDD step definitions?Static fields persist across scenario executions, which can cause flaky tests, cross-scenario interference, and prevent safe parallel execution.
When should you use the Shareable Scenario Context pattern?Use it when multiple step definition classes need access to the same scenario data, such as a logged-in customer name used in both authentication and ordering steps.
What is the role of dependency injection in shareable scenario context?Dependency injection provides scenario-scoped context objects to step definitions, ensuring shared data is accessible across classes while still remaining isolated per scenario.
What does the Introduce and Refer pattern do?It separates the language used to introduce a new entity from the language used to refer to that same entity later, making scenarios clearer and more idiomatic.
Why is the distinction between “a/an” and “the” useful in scenario text?It often signals whether an entity is being introduced or referred to, helping readers understand when something new enters the narrative and when an existing item is being discussed.
What is the Entity Selector pattern?It selects an entity using business logic expressed in the scenario, such as “the earliest order received,” allowing scenarios to refer to collections in a natural, readable way.
How do custom parameter types help with narrative flow in BDD scenarios?They let multiple phrases map to the same automation logic, such as “the order,” “the placed order,” or “the earliest order received,” improving readability and step reuse.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Effective Behavior-Driven Development ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Effective Behavior-Driven Development ebook for free