0

I am new to cucumber. We have below use case for UI automation through cucumber.

Please consider below example.

in TestNg,

@Test {"formname"}
public void createAndSearchForm(String formName)
{
    //In below  step, it create form by name (formName-timestamp) and return   the formname. e.g. it create form and return "formname-06042016184426"
    // In this method we create form name by appending time stamp to formname passed to method. Since application didn't accept same name we need to append timestamp
    //   to formname.
    String newFormName=createForm(formName);

    // In below method we pass above newFormName and verify whether form is created or not by searching form name.
    asserTrue(searchCreatedForm(newFormName));

}

Now we are moving to cucumber and we need to accomplish above example in cucumber.

Feature: Forms.


Scenario: Login to application
Given Create form with name “formname”
Then Search “formname”

Issue that we are facing -> The formname that is getting returned in step1, we don’t know how to pass it to step2 . Within scenario, We need to pass this form name to different step definition which is implemented in various classes.

I tried to search over the net however didn’t find anything specific to our need.

It will be great help if anyone could give us some pointers/suggestion.

java user
  • 41
  • 5

1 Answers1

1

EDIT When I need to share variables across multiple step files I create a superclass that defines them. Each of my step files extends that superclass giving them all access to that variable (or variables).

As stated in the comments, be careful about making the class variables static as you might leak state. I set the values of any static variables to a value (e.g. null) in the class constructor so that the value is reset for each scenario.

--End edit--

public class YourStepDefinitions {

    private String interStepParameter;

    @Given("^Some first step$")
    public void first_step() {

        interStepParameter = "foo";

    }

    @Then ("^A second step$")
    public void second_step() throws Throwable {
        if (interStepParameter.equals("foo") {
           // Do something
        }
    }
}
MikeJRamsey56
  • 2,779
  • 1
  • 20
  • 34
  • Thanks Mike. We thought of this approach however we need to pass this variable to many step definition that are implemented in different classes. We have many test cases which have same scenario.So we thought it will not be a good idea to implement this way. – java user Apr 07 '16 at 04:30
  • Your driver is shared across classes. Assuming that you are not running parallel, create another static variable who's handle gets passed around, just like driver. – MikeJRamsey56 Apr 07 '16 at 13:28
  • Please note that you might accidentally leak state between scenarios this way (if the variable is set during a test and not reset before the next test). This will cause your tests to be flaky and unreliable. And also this will only work as long as all of the steps that need this variable are in the same file. Consider using Dependency Injection to manage the fields & objects you'd like to share between different steps. the DI container can then manager their creation and (if configured correctly) their destruction. – Marit Mar 03 '18 at 07:27
  • @Marit Oh, you mean if static. Just add a class constructor that sets a static interStepParameter to null. – MikeJRamsey56 Mar 06 '18 at 19:07