Consider a bunch of scenarios grouped into one feature file that all require setting up a valid user, however I do not want to generate such a user dynamically, for example:

  Background: Creating a new user and logging in
          Given the following user "<alias">
          When "<alias>" logs in

All scenarios require their own valid user, which based on alias is serialized from a .json file, how can I manage such a background step when I want to choose the alias of my user using a hard set alias? how can each scenario get scope of their own user here, that is not reused throughout all scenarios? each scenario should have its own user created and such alias used is tied to each scenario.

6 Answers 6


If you need different users for different scenarios then you either group them into separate files with proper backgrounds or simply move the steps that define the user from background to the scenarios themselves. Otherwise I do not see what exactly is the problem here.


Add a property to the JSON, or a copy of it or a transformation of it into another form, that has an attribute/field/property/other that indicates the state, e.g. used, unused, part1, etc.

Basically figure out a why to deal with state and how to store it.


Check the regexes that are used to match your step definitions, they should allow for a hardcoded value to match the same as a substituted one. This matching happens after the substitutions.

If you are having trouble with the regexes then you can debug them on Regex 101.


I would try with Cucumber tagged hooks.

Here an example in C# SpecFlow, because I dont have Cucumber installed. This works, so I think it is a good example of the general idea.

Background: test
    Given do something with active user

Scenario: My Scenario  

The taghook implementation stores a user in a global state for other steps to retrieve:

public static void CreateUser()
  ScenarioContext.Current["user"] = new User().GetUser("Username");

Retrieve in step:

[Given(@"do something with active user")]
public void GivenDoSomethingWithAtiveUser()
   var user = (User)ScenarioContext.Current["user"];

Use an expected data manager. This can load the data by any method appropriate with your technology stack, pull the data using an alias/tag that describes its characteristic (e.g. Authorised vs Unauthorised). The expected data manager can have a pool of authorised users if necessary and load them from a platform appropriate test data source. It can create the user with the characteristic, using the data if necessary.

Given an "authorised" user 
When ...

Then when you implement the given step, retrieve


See https://sqa.stackexchange.com/a/39770/17321 for a more elaborate answer on this approach.


We can use any data source like .json or .xslx etc. to store these hard coded set of data. We can read that data from excel or json during runtime.

Sometimes, we can also use these hard code aliases as parameters to our step-definition methods.

E.g. Scenario:

When I login as "DETECTIVE"


@When("^I login as \"(.*?)\"$")

public void user_enters_UserName_and_Password(String userType)




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.