What Is Meant by "Data Persistence"?
In the context of automated testing and Selenium, data persistence refers to the preservation of data as a part of automated test code, so that the information can be referenced outside of the current method/test step at a later point in the test, outside of the current method or test step (i.e., when the desired scope in which you wish to preserve data is larger than that which can be addressed by local variables and parameter passing).
Practical Example of Where One Would Care About Data Persistence
Let's say there is a test case that tests that doing something does
not change a value (whether that is the contents of the page, which page is displayed in the app, or anything else that can be represented as a value in code). So, the Scenario might look like the following:
Scenario: Doing The Thing Doesn't Modify the Value
Given the user is on the page
And the initial value is foo
When the user does the thing on the page
Then the value is still foo
The value is read in the second Given statement, but the verification at the end needs to verify that the current value of from wherever the value was read still has the same value. So, a value is read in one test step, but isn't actually used until two test steps later, in a different method. This is obviously not a job for local variables, and given how Selenium works, you can't just pass the value directly from the Given glue code method to the Then glue code method. Thus, a question of data persistence is raised: how to keep the initially-stored value alive throughout the Scenario?
How to Achieve Data Persistence in Selenium
The answer, at least within a given Scenario, is instance fields. Simply declare an instance variable to hold the data that needs to be persisted between test steps in the Step Definitions class, and then have your earlier method which reads the data store the value to it, and have the later method read it out to use it. A key caveat with this solution is that Selenium creates a new instance of the Step Definition object for each Scenario in a feature file, so if you want to persist values between Scenarios using a variable in the Step Definition file, you must use a
static variable; of course, the use of static variables in code have their own caveats and advantages and disadvantages. Given how Scenarios in theory really should each be standalone and independent, this behavior actually makes sense; if you find that a given Scenario really does care about some data/state from a different, previous, Scenario, that is an excellent hint that perhaps your test cases/Scenarios are not as well structured or written as they should be, and that perhaps they require some revision prior to the automation effort.
--
StanVogel - 29 Apr 2020, updated 15 Jun 2020