I have written a number of Rest Assured Cucumber tests to test my API. I have written cucumber feature/step definition files do the following:-
Add a new category Update the category Delete the Category.
So I have a single step definition class with three scenarios.
The first scenario creates a new category and I store the category ID in a global scope variable (this variable was declared at the top of the class file). I wrote the variable to the console to make sure it has a value (it did) after the new category is deleted. After the scenario has finished it moves onto scenario 2 where it tries to update the category. The problem is that value of the global variable has now become empty and the test fails.
Does anyone know if its possible to share the content of global variables between different scenarios in the same step definition class file?
It is intentionally difficult to share information between scenarios. Each scenario is it's own little experiment and should not be influenced by others.
Now if you're used to writing manual test scripts you may think it's efficient to reuse things. And it is. But a computer is much faster so you don't have to be efficient. Rather because it's cheap to do everything from scratch, a little inefficiency is worth reducing the chance tests can interact by accident.
So rather consider writing three different scenarios:
Scenario: Create
Given the things needed to make a category
When a new category is created
Then it has all the things used to make it
Scenario: Update
Given a category with some property
When the property of the category is changed
Then fetching the category again shows the updated
property
Scenario: Delete
Given a category
When the category is deleted
Then the category is not found when fetched
The second and third create the category in the given step.
Now if this is a system that isn't shutdown after a test run you may also want to use after hooks to try and delete any categories created when the test is done.