Search code examples
javacucumber

Cucumber the same random class in diffrent scenarios


I have Two Cucumber scenarios. On each sceanrio I want to use the same random_document

in my Steps class I have:

public class PatientCreateSteps {
private final String RANDOM_DOCUMENT='ABC'+RandomStringUtils.randomAlphabetic

....

#in the featurefile:
Scenario: Add patient documentation

@When ("StepsFromScenarioI)
.....
@Then("StepsFromScenarioI)

Scenario: Update patients documentaion

@When("StepsFromScenarioII)

Is it possible to change something to not generating new RANDOM_DOCUMENT in the second class?


Solution

  • It depends on what you are trying to do. If you want the same random string in all scenarios in a single test run you could make it static.

    public class PatientCreateSteps {
    private static final String RANDOM_DOCUMENT='ABC'+RandomStringUtils.randomAlphabetic
    
    ....
    

    But if you are trying to share a specific document between two scenarios you can not do that.

    Consider creating it from scratch and specifying what exactly needs to be the same. For example

    Scenario: A patient with a document
      Given a patient
      And a document for that patient
      ...
    
    
    Scenario: A patient with the same document as another patient
      Given a patient
      And a document for that patient that is the same as another patients document
      ...
    

    Here after creating the patient you'd create a second patient and give him a document, the give the first patient a copy of the second patients document.

    This may sound in efficient. Especially if you're used to manually test things. But computers are pretty fast and/or can do things in parallel. So it's more important to keep each scenario independent.