Cucumber: Share random data between Steps using static variable

Profile picture for user devraj

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+"@gmail.com";
}

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.