[Easiest Way] Share the data within different Scenarios in Cucumber BDD + Java!

Here in this article, we're going to see how we can store the value from Cucumber examples as input to use in other template statements & scenarios.

Cucumber has the concept called Scenario Context by using which we can share the data within different scenarios.

But, we'll use simple Java Data structure & OOPS concepts like Encapsulation to achieve our goal.

This guide covers all data-sharing issues in Cucumber like Sharing data between different scenarios, Steps, or Different feature files.

{tocify} $title={Table of Contents}

    How to use inputs taken from One scenario in another one in Cucumber BDD(Java)?

    We need to create a Class to store the values and set Getter() & Setter() methods to play with the data.

    We'll use Java Data Structure to store the values in a Class.

    We'll use Java HashSet to store the inputs taken from Cucumber Scenario tables. You can use HashMap too.

    Step 1: Create a new class with HashSets & Getter/Setter methods

    First, we'll initiate the new class with a cluster name in the same Step definition file. In our case, it's PriceStorage. Because we're storing the book prices in our example.

    1. Open the Cucumber Step definition file (Java)

    2. Then, we've to create and use the 

    static Set<String> bookList = new HashSet<String>();

    2. Now, we'll need to create some methods to add/get/clear the value in the HashSet

    3. We'll use add() to push the value in a Set, & clear() to reset the entire Pricelist

    4. A new class that holds all the add, get & clear value operations will look like this

    class PriceStorage {

           static Set<String> bookList = new HashSet<String>();

       static Set<Float> priceList = new HashSet<Float>();

    static Set<Float> newPriceList = new HashSet<Float>();

    public static void resetPriceList() {

        priceList.clear();    }

    public static void addPrice(float price) {



    public static Set<Float> getPriceList() {

        return priceList;


    public static void addNewPrice(float newPrice) {



    public static Set<Float> getnewPriceList() {

         return newPriceList;



    You can change and adjust the code according to your conditions. Now, the next step is to implement these methods.

    Step 2: Import the created Mehods in a Scenarios

    Here in this step, we'll edit and import the methods from the class and implement those methods according to testing scenarios.

    1. Write your Cucumber sentences, our cucumber sentences look like this


    Scenario Outline: Adding items to cart functionality Bookmart

    Given Logged in to Bookmart website

    When Search items with "<BookName>" & "<Price>" and add it to cart twice

    Then Books are added to cart and Total amount calculated correctly


    | BookName | Price |

    | Harry Potter and the Chamber of Secrets | 235.00 |

    | Harry Potter and the Goblet of Fire | 321.00 |

    | Harry Potter and the Prisoner of Azkaban | 213.00 |

    | Harry Potter and the Deathly Hallows | 325.00 |

    | Harry Potter and the Half-Blood Prince | 433.00 |

    2. We're looking to store the values of <Price> variable

    3. Step definition code for Line 4 is as follows

    @When("Search items with {string} & {string} and add it to cart twice")

    public void search_items_with_and_add_it_to_cart_twice(String bookname1

    , String price1) {


    price2 = Float.parseFloat(price1);



    (By.cssSelector("input[placeholder='Search books or authors']")).sendKeys(bookname1);




    (By.xpath("//span[contains(text(),'Add to Cart')]")).click();


    (By.xpath("//span[contains(text(),'Add to Cart')]")).click();



    4. We're passing the Book price through parameters & converted it to float

    5. Now accessing the method addPrice() from the PriceStorage class

    6. It will store the value in HashSet

    7. When we need the value in another or in the same scenario, we'll access the method to return the values using the getPriceList() method

    8. Or else you can directly access the value from the Data Structure by looping through the HashSet

    @Then("Books are added to cart and Total amount calculated correctly")

    public void books_are_added_to_cart_and_total_amount_calculated_correctly() {

    int priceListItemCount = PriceStorage.bookList.size();

    float priceOne = 0;

    if( priceListItemCount == 5 ) {

    for(Float i : PriceStorage.priceList) {

    priceOne = i * 2;






    In the above example, we're just getting the price of the book and storing it in HashSet. Then, we're multiplying it by 2 and storing it in another HashSet for future use.

    It was just a basic example of how we can store the data from one scenario and share it with others using basic Java concepts & Data structure.

    Hope you like this tutorial, if you find it helpful please leave a comment down below.

    If you have any queries related to Cucumber please reach out to us through Comments. Happy coding!!!


    How to run the same scenario with different test data in Cucumber?

    There is a concept of Scenario Outline in Cucumber by which you can pass multiple values in order to test the same scenario with different test data.

    Scenario Outline takes the Data from tables in the cucumber feature file. In the result console, the tested scenario name will be the same for every passed data.

    How to pass value from one feature file to another in Cucumber?

    The answer is simple. Just create a new class to store the data in a different Java file within the same package.

    Whenever you need the class to perform store operations or retrieval operations, just import the package and access the Class through it.

    How do you link two scenarios in Cucumber?

    Just use the same tag for both scenarios. Add the tag in the Java runner file(JUnit or TestNG).

    Sanket bhosale

    Myself Sanket Bhosale. True Techy and founder of The Tech Review, OS Tech Review & Home Automation BOT. I am a Software Engg. by profession and a Blogger by passion. Working as Software Engineer for US Banking clients. I've plenty of knowledge about US and general Finance and banking. My interest is in IT & its applications, Finance, Android, Gaming, Home decor/automation, SEO, and Digital marketing. I Have 3+ years of experience in the Blogging industry.

    Visit my blogs:
    The Tech Review
    OS Tech Review
    Nerdy VR
    Home Automation Bot

    Post a Comment

    Previous Post Next Post