Skip to main content

Cucumber: Share random data between Steps using static variable

There are several ways to share data between steps. Best one is to use Dependency Injection. In this video we will discuss, sharing data using static variable. Which is not the efficient approach but I have seen several QA using the same.

Step 1: Create static variable in you base file

public static String myRandomData;

Step 2: Now generate random data in your first step definition file

@When("I create a random record")
public void i_create_a_new_user() 
    DateFormat df = new SimpleDateFormat("yyMMddHHmmssZ");
    Date dateObj = new Date();
    String dateToString = df.format(dateObj);
    myRandomData = "goswami.tarun77"+dateToString+"";

Step 3: Use same myRandomData in another step definition, this step definition can exist in any file:

@Then("I use random record")
public void i_use_random_record() 
    System.out.println("Random call: "+myRandomData);

Although this is not the recommended and efficient approach, we will discuss more approach in upcoming lectures.

Submitted by tgoswami on November 2, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.


At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.