2

Variable set with some value in the one cucumber step implementation is losing its value in the next step

@Test
Feature: Test

@test
  Scenario: Test

   When user sets value of varibale x
    Then user retrives value of x

Step Implementation

import cucumber.api.java.en.When;

public class TestStepToBeRemoved {
    String x;

    @When("^user sets value of varibale x$")
    public void setValueOfx() {
        x = "Random Text";
    }

    @When("^user retrives value of x$")
    public void retriveValueOfX() {
        System.out.println("Value of X is : " + x);
    }
}

This was working fine before we merged another framework in our project which uses Guice libraries and injectors. But Now output

Value of X is : null

So, is there any in cucumber that we can set the cucumber to clear all the object when step execution is completed?

Please dont ask for making x static, it will resolve this issue but we need any other solution except making x static

Akash Chavan
  • 297
  • 5
  • 20

1 Answers1

1

when you user cucumber-guice lib, cucumber somehow creates different object references for different step definitions, Hence the instance variable initialized in one step is not able to keep the same value in the next step. To avoid this situation you can use @ScenarioScoped at the top in Step definition file and it will resolve the issue

import cucumber.api.java.en.When;

@ScenarioScoped
public class TestStepToBeRemoved {
    String x;

    @When("^user sets value of varibale x$")
    public void setValueOfx() {
        x = "Random Text";
    }

    @When("^user retrives value of x$")
    public void retriveValueOfX() {
        System.out.println("Value of X is : " + x);
    }
}
Akash Chavan
  • 297
  • 5
  • 20