How to pass variables between cucumber-jvm steps

JavaCucumberCucumber Jvm

Java Problem Overview


To pass variables between steps I have the step methods belong to the same class, and use fields of the class for the passed information.

Here is an example as follows:

Feature: Demo

  Scenario: Create user
    Given User creation form management
    When Create user with name "TEST"
    Then User is created successfully

Java class with steps definitions:

public class CreateUserSteps {

   private String userName;

   @Given("^User creation form management$")
   public void User_creation_form_management() throws Throwable {
      // ...
   }

   @When("^Create user with name \"([^\"]*)\"$")
   public void Create_user_with_name(String userName) throws Throwable {
      //...
      this.userName = userName;
   }

   @Then("^User is created successfully$")
   public void User_is_created_successfully() throws Throwable {
      // Assert if exists an user with name equals to this.userName
   }

My question is if it is a good practice to share information between steps? Or would be better to define the feature as:

Then User with name "TEST" is created successfully

Java Solutions


Solution 1 - Java

In order to share commonalities between steps you need to use a World. In Java it is not as clear as in Ruby.

Quoting the creator of Cucumber.

> The purpose of a "World" is twofold: > > 1) Isolate state between scenarios. > > 2) Share data between step definitions and hooks within a scenario. > > How this is implemented is language specific. For example, in ruby, > the implicit self variable inside a step definition points to the > current scenario's World object. This is by default an instance of > Object, but it can be anything you want if you use the World hook. > > In Java, you have many (possibly connected) World objects. > > The equivalent of the World in Cucumber-Java is all of the objects > with hook or stepdef annotations. In other words, any class with > methods annotated with @Before, @After, @Given and so on will be > instantiated exactly once for each scenario. > > This achieves the first goal. To achieve the second goal you have two > approaches: > > a) Use a single class for all of your step definitions and hooks > > b) Use several classes divided by responsibility [1] and use dependency > injection [2] to connect them to each other. > > Option a) quickly breaks down because your step definition code > becomes a mess. That's why people tend to use b). > > [1] https://cucumber.io/docs/gherkin/step-organization/ > > [2] PicoContainer, Spring, Guice, Weld, OpenEJB, Needle

The available Dependency Injection modules are:

  • cucumber-picocontainer
  • cucumber-guice
  • cucumber-openejb
  • cucumber-spring
  • cucumber-weld
  • cucumber-needle

Original post here https://groups.google.com/forum/#!topic/cukes/8ugcVreXP0Y.

Hope this helps.

Solution 2 - Java

It's fine to share data between steps defined within a class using an instance variable. If you need to share data between steps in different classes you should look at the DI integrations (PicoContainer is the simplest).

In the example you show, I'd ask whether showing "TEST" in the scenario is necessary at all. The fact that the user is called TEST is an incidental detail and makes the scenario less readable. Why not generate a random name (or hard code something) in Create_user_with_name()?

Solution 3 - Java

In Pure java, I just use a Singleton object that gets created once and cleared after tests.

public class TestData_Singleton {
    private static TestData_Singleton myself = new TestData_Singleton();
    
    private TestData_Singleton(){ }

    public static TestData_Singleton getInstance(){
        if(myself == null){
            myself = new TestData_Singleton();
        }

        return myself;
    }

    public void ClearTestData(){
        myself = new TestData_Singleton();
    }

Solution 4 - Java

I would say that there are reasons to share information between steps, but I don't think that's the case in this scenario. If you propagate the user name via the test steps then it's not really clear from the feature what's going on. I think it's better to specifically say in the scenario what is expected. I would probably do something like this:

Feature: Demo

  Scenario: Create user
    Given User creation form management
    When Create user with name "TEST"
    Then A user named "TEST" has been created

Then, your actual test steps might look something like:

@When("^Create user with name \"([^\"]*)\"$")
public void Create_user_with_name(String userName) throws Throwable {
   userService.createUser(userName);
}

@Then("^A user named \"([^\"]*)\" has been created$")
public void User_is_created_successfully(String userName) throws Throwable {
   assertNotNull(userService.getUser(userName));
}

Solution 5 - Java

Here my way: I define a custom Scenario-Scope with spring every new scenario there will be a fresh context

Feature      @Dummy
  Scenario: zweites Scenario
   When Eins
   Then Zwei

1: Use spring

<properties>
<cucumber.version>1.2.5</cucumber.version>
<junit.version>4.12</junit.version>
</properties>

<!-- cucumber section -->


<dependency>
  <groupId>info.cukes</groupId>
  <artifactId>cucumber-java</artifactId>
  <version>${cucumber.version}</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>info.cukes</groupId>
  <artifactId>cucumber-junit</artifactId>
  <version>${cucumber.version}</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>${junit.version}</version>
  <scope>test</scope>
</dependency>

 <dependency> 
   <groupId>info.cukes</groupId> 
   <artifactId>cucumber-spring</artifactId> 
   <version>${cucumber.version}</version> 
   <scope>test</scope> 
 </dependency> 


<!-- end cucumber section -->

<!-- spring-stuff -->
<dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-test</artifactId> 
              <version>4.3.4.RELEASE</version> 
       <scope>test</scope> 
 </dependency> 

   <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-context</artifactId> 
              <version>4.3.4.RELEASE</version> 
       <scope>test</scope>
   </dependency> 
   <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-tx</artifactId> 
       <version>4.3.4.RELEASE</version> 
       <scope>test</scope>
   </dependency> 
   <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-core</artifactId> 
       <version>4.3.4.RELEASE</version> 
       <scope>test</scope>
       <exclusions> 
           <exclusion> 
               <groupId>commons-logging</groupId> 
               <artifactId>commons-logging</artifactId> 
           </exclusion> 
       </exclusions> 
   </dependency> 
   <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-beans</artifactId> 
              <version>4.3.4.RELEASE</version> 
       <scope>test</scope>
   </dependency> 

   <dependency> 
       <groupId>org.springframework.ws</groupId> 
       <artifactId>spring-ws-core</artifactId> 
       <version>2.4.0.RELEASE</version> 
       <scope>test</scope>
   </dependency> 

2: build custom scope class

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(scopeName="scenario")
public class ScenarioContext {
	
	public Scenario getScenario() {
		return scenario;
	}

	public void setScenario(Scenario scenario) {
		this.scenario = scenario;
	}

	public String shareMe;
}

3: usage in stepdef

@ContextConfiguration(classes = { CucumberConfiguration.class })
public class StepdefsAuskunft {

private static Logger logger = Logger.getLogger(StepdefsAuskunft.class.getName());

@Autowired
private ApplicationContext applicationContext;

// Inject service here : The impl-class need @Primary @Service
// @Autowired
// IAuskunftservice auskunftservice;


public ScenarioContext getScenarioContext() {
	return (ScenarioContext) applicationContext.getBean(ScenarioContext.class);
}


@Before
public void before(Scenario scenario) {

	ConfigurableListableBeanFactory beanFactory = ((GenericApplicationContext) applicationContext).getBeanFactory();
	beanFactory.registerScope("scenario", new ScenarioScope());

	ScenarioContext context = applicationContext.getBean(ScenarioContext.class);
	context.setScenario(scenario);

	logger.fine("Context für Scenario " + scenario.getName() + " erzeugt");

}

@After
public void after(Scenario scenario) {

	ScenarioContext context = applicationContext.getBean(ScenarioContext.class);
	logger.fine("Context für Scenario " + scenario.getName() + " gelöscht");

}



@When("^Eins$")
public void eins() throws Throwable {
	System.out.println(getScenarioContext().getScenario().getName());
	getScenarioContext().shareMe = "demo"
	// you can save servicecall here
}

@Then("^Zwei$")
public void zwei() throws Throwable {
	System.out.println(getScenarioContext().getScenario().getName());
	System.out.println(getScenarioContext().shareMe);
	// you can use last service call here
}


@Configuration
    @ComponentScan(basePackages = "i.am.the.greatest.company.cucumber")
    public class CucumberConfiguration {
    }

the scope class

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;


public class ScenarioScope implements Scope {
	

  private Map<String, Object> objectMap = Collections.synchronizedMap(new HashMap<String, Object>());
  
    /** (non-Javadoc)
     * @see org.springframework.beans.factory.config.Scope#get(java.lang.String, org.springframework.beans.factory.ObjectFactory)
     */
    public Object get(String name, ObjectFactory<?> objectFactory) {
        if (!objectMap.containsKey(name)) {
            objectMap.put(name, objectFactory.getObject());
        }
        return objectMap.get(name);
         
    }
 
    /** (non-Javadoc)
     * @see org.springframework.beans.factory.config.Scope#remove(java.lang.String)
     */
    public Object remove(String name) {
        return objectMap.remove(name);
    }
 
    /** (non-Javadoc)
     * @see org.springframework.beans.factory.config.Scope#registerDestructionCallback(java.lang.String, java.lang.Runnable)
     */
    public void registerDestructionCallback(String name, Runnable callback) {
        // do nothing
    }
 
    /** (non-Javadoc)
     * @see org.springframework.beans.factory.config.Scope#resolveContextualObject(java.lang.String)
     */
    public Object resolveContextualObject(String key) {
        return null;
    }
 
    /** (non-Javadoc)
     * @see org.springframework.beans.factory.config.Scope#getConversationId()
     */
    public String getConversationId() {
        return "VolatileScope";
    }
 
    /**
     * vaporize the beans
     */
    public void vaporize() {
        objectMap.clear();
    }
    

}

Solution 6 - Java

Other option is to use ThreadLocal storage. Create a context map and add them to the map. Cucumber JVM runs all the steps in the same thread and you have access to that across all the steps. To make it easier, you can instantiate the storage in before hook and clear in after hook.

Solution 7 - Java

If you are using Serenity framework with cucumber you can use current session.

Serenity.getCurrentSession()

more about this feature in http://thucydides-webtests.com/2012/02/22/managing-state-between-steps/. (Serenity was called Thucydides before)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontroigView Question on Stackoverflow
Solution 1 - JavaPedro LopezView Answer on Stackoverflow
Solution 2 - JavaSeb RoseView Answer on Stackoverflow
Solution 3 - JavaJason SmileyView Answer on Stackoverflow
Solution 4 - JavaBarrySW19View Answer on Stackoverflow
Solution 5 - Javauser528322View Answer on Stackoverflow
Solution 6 - JavasamfromcoView Answer on Stackoverflow
Solution 7 - JavabsmkView Answer on Stackoverflow