An order-cancellation scenario can become hard to read if every step repeats the customer name and order number. Local Scenario Context lets a later step use information established earlier, so the scenario can say “the placed order” instead of repeating its number. section 13.1
The pattern stores information for one scenario execution and makes it available to later step definitions in the same class or file. Those steps can work together as a business narrative without each step being context-independent. section 13.1 section 13.0
Remember the placed order
After a step records that Rebecca placed order #12342, a cancellation step can refer to “the placed order.” Its step definition retrieves the saved number and cancels that order. The scenario introduces the number once while the automation retains what the later step needs. section 13.1
Listing 13.3 Using the step definition class fields for order number
*** StepDefinitions/OrderingStepDefinitions.cs ***
[Binding]
public class OrderingStepDefinitions
{
private int placedOrderNo;
[Given("the customer {string} has placed the order #{int}")]
public void GivenTheCustomerHasPlacedTheOrder(string customerName,
int orderNo)
{
OrderService.PlaceOrder(customerName, orderNo, "Margherita");
placedOrderNo = orderNo; #A
}
[When("the customer {word} cancels the placed order")]
public void WhenTheCustomerCancelsThePlacedOrder(string customerName)
{
OrderService.CancelOrder(customerName,
placedOrderNo); #B
}
}The step-definition class saves the order number in an instance field when the order is placed, then reads that field when the placed order is cancelled. section 13.1
Keep that memory local to the scenario. Automation tools create fresh glue-code instances for each execution, isolating instance fields from other scenarios. Avoid static fields: their values persist between executions, making results depend on earlier scenarios and preventing parallel execution. When steps live in different classes or files, this local pattern does not share their data; Shareable Scenario Context addresses that broader need. section 13.1 section 13.0
