Overview

13 Spotlight patterns for automating narrative flow

This chapter shows how automated BDD scenarios can preserve the feel of a business narrative even though they are executed step by step by automation tools. The central idea is that a scenario is not just a list of isolated actions: it is a cohesive flow in which earlier steps establish context that later steps can reuse. By treating scenario state as a temporary, isolated memory, the team can reduce repetition, improve readability, and keep the important business details in focus.

The first major idea is that scenario context can be used locally within a single step-definition class or shared across multiple classes through an injected context object. Local Scenario Context uses instance fields in glue code to remember values like an order number between steps, while Shareable Scenario Context extends that idea by introducing a dedicated context class that can be injected wherever needed. This avoids static state, preserves scenario isolation, supports parallel execution, and lets different parts of the automation solution exchange information without forcing the scenario text to become verbose or repetitive.

The chapter then connects that technical memory model to natural language through the Introduce and Refer pattern, which distinguishes between first mentioning an entity and referring to it later. Scenario text can use phrases such as “an order” when something is introduced, and “the order,” “the placed order,” or even “they” when referring back to it, provided the wording remains unambiguous. Finally, the Entity Selector pattern goes a step further by allowing the scenario to name an entity by a business-friendly description that is resolved programmatically, such as “the earliest order received.” This keeps scenarios readable while letting automation code choose the right entity based on domain logic, though the chapter stresses that such selection logic should be tested separately from the scenario itself.

Lifecycle of glue code instances
Lifecycle of glue code instances
Class diagram showing constructor injection of shared context class
Class diagram showing constructor injection of shared context class
Lifecycle of a shared context instance
Lifecycle of a shared context instance
Logical flow when resolving custom parameter types
Logical flow when resolving custom parameter types
Using logic to select the expected result of a behavior
Using logic to select the expected result of a behavior

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.
  • Scenario automation tools 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., “earliest 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 scenarios?The Spotlight pattern language helps keep a scenario focused on the most important business details, so the narrative reads naturally and the reader is not distracted by unnecessary repetition or technical noise.
What is scenario context in automated BDD execution?Scenario context is the transient data store created for a single scenario execution. It lets step definitions remember and share information during that scenario, and it is discarded when the scenario finishes.
Why should step definitions not be treated as isolated atomic actions?Because a scenario is a cohesive flow of related events, not just separate actions. Step definitions should work together as reusable building blocks that automate the business narrative described by the scenario.
What problem does the Local Scenario Context pattern solve?It solves the problem of repeating data within a scenario by storing values in step definition instance fields, allowing later steps in the same class to reuse information introduced earlier.
Why are static fields discouraged in step definition classes?Static fields preserve values across scenario executions, which breaks isolation, can cause flaky tests, and prevents safe parallel execution.
When should the Shareable Scenario Context pattern be used?Use it when step definitions in different classes need access to the same scenario data, such as sharing authenticated customer details between authentication and ordering steps.
What is the difference between introducing an entity and referring to it later?Introducing an entity means presenting something new in the scenario and storing it in context. Referring to it later means using a phrase that clearly points to that already-known entity, such as “the order” or “the authenticated customer.”
Why is “an order” often better than “the order” when first mentioning an entity?“An order” signals that the entity is being introduced for the first time, while “the order” suggests it was already known. This makes the scenario easier to understand and less ambiguous.
What does the Entity Selector pattern do?It uses business-readable phrases to select an entity by logic, such as choosing “the earliest order received” from a collection of orders, rather than referring only to a fixed stored entity.
Why should the logic behind an Entity Selector be covered by unit tests?Because the pattern is meant to improve scenario readability, not to validate the correctness of the selection logic itself. Unit tests should verify that the underlying logic works correctly.

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


Close
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 Effective Behavior-Driven Development ebook for free
Close
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 Effective Behavior-Driven Development ebook for free