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
Class diagram showing constructor injection of shared context class
Lifecycle of a shared context instance
Logical flow when resolving custom parameter types
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.
Effective Behavior-Driven Development ebook for free