1

I'm still pretty new to Cucumber automation testing and I haven't found anything about this in documentation, so I'm guessing either it's not possible or it's done in a very different way.

The thing is, in Robot automation framework I'm used to working with variables like this: ${userName} and once set these can be shared by different tests. Is that possible in cucumber?

The specific use case is: I'm using Java version of Cucumber, and I want to create a new user account that I'll use throughout all scenarios in the whole feature file:

Scenario: Create user account
    Given user navigates to the home page
    And user enters username and password  *(this is where I want to provide random data and have it stored for future use)*
    And user clicks on the register button
    Then user should see the default landing page

I can always create a random user name and password in my test implementation using java and store them in class-scoped variables for future use in all steps that require them. But I'm not sure this is a good practice. Is there a better way to do so?

Floella
  • 1,121
  • 14
  • 37
  • Possible duplicate of: https://stackoverflow.com/questions/26422470/good-practice-to-pass-variables-between-cucumber-jvm-steps – Marit Jan 11 '18 at 15:36

3 Answers3

2

To generate a user and password, you'll have to implement this yourself, as stated by @Eugene S.

To share variables between steps, it is best to use Dependency Injection. Cucumber-jvm supports multiple DI frameworks, as listed here: https://cucumber.io/docs/reference/java-di Unfortunately there isn't a lot of "official" (Cucumber) documentation on how to do this.

Thomas Sundberg has written several blog posts on how to share variables between steps using different DI frameworks, including:

Marit
  • 2,174
  • 13
  • 24
1

Random user and password is not really necessary. Read please Equivalence Class Testing vs. Boundary Value Testing

You can create your wildcards for variables:

`And user enters username and password "VAR#:username" "VAR#:password"`

`@Given("^user enters username and password$")
public void getCredentials(String userName, String password) {
    userName = DataStore.getValue(userName);
    password = DataStore.getValue(password);
}`

Inside dataStore you can get values from property file or anything else.

property file can look like:

VAR#:username=Username VAR#:password=1234

nick318
  • 569
  • 4
  • 15
0

No, Cucumber does not allow anything like that. The only thing Cucumber does, is parse your steps and run the matching methods.

To achieve what you describe you can just create a method that will somehow encapsulate data and variable that you require. Then you can run this step in order to obtain it.

For example take the step that you described:

And user enters username and password

Then the method that will be executed will look something like:

@Given("^user enters username and password$")
public void getCredentials() {

    //for example
    userName = SomeStaticClaass.getUsername();
    password = SomeStaticClaass.getPassword();

}
Eugene S
  • 6,119
  • 7
  • 52
  • 82