In the section "Configuring Cucumber and Serenity":
Maintaining state between scenarios
You may also wish to maintain state between scenarios. By default, Serenity will use new step libraries and action classes for each scenario. However, if you want to share data between the scenarios of a feature, you can use the following option:
serenity.maintain.session = true
With this value set, you can now use either the @Shared
annotation, or @Steps(shared=true)
.
I haven't done anything to test the @Shared
annotation, but in my testing:
serenity.maintain.session = true
appears to have no effect.
@Steps(shared=true)
does not behave as described.
I will discuss my findings here:
Within a single Definition class-instance, all @Steps
beans are shared across all of the steps within Scenario, but not within a Feature. Adding (shared=true)
has no effect on the creating of new Steps beans between Scenarios within a Feature. When a new Feature is run, every definition used will create new instances of each required Steps bean.
If a Scenario uses multiple Definition classes, then the default behavior is for each Definition class-instance to instantiate new Steps beans on each run.
However, if multiple Definition instances (the behavior is one instance-per-class-per-scenario) use the same Steps bean class with the (shared = true)
property, then the Definition instances will share the same Steps bean. This is a very useful feature for persisting data for the duration of a Scenario, but across multiple Definition classes.