0

When I first execute a feature file , am updating the fields of it during run time. The same field's value is required to pass to other feature file. Is it possible to pass the data of one feature file to another feature file using java?

feature file 1:
scenario outline: test xxx functionality
Examples :
|user|password|
|abc|pass|

feature file2:
Scenario Outline : test yyy functionality
Examples:
|user|password|
|    |        |

Here, I want the data that is there in the feature file1 to be passed to feature file 2 i.e, abc and pass should be copied to Examples of feature file 2. Please do suggest.Thanks in advance!!

Rahul
  • 759
  • 3
  • 21
  • 43

2 Answers2

1

Passing values from one feature file to another is not supported. It would force you to execute your scenarios in a specific order. That is a well known anti pattern. Your scenarios should be possible to execute in any order and that is not possible if you expect one scenario to be executed before another.

So how should you be able to reuse your setup from the first feature file in the second? The short answer is don’t. Instead implement a helper that the features that need the user to be setup can use to prepare the system under test. Call this helper from each scenario that need this setup before it’s execution. This may sound as a lot of unnecessary work, but it will save you from a lot of problems with scenarios that depends on each other and leaves your system in unexpected states between executions.

Thomas Sundberg
  • 4,098
  • 3
  • 18
  • 25
0

Strictly speaking, you should not be passing data from one BDD test to another. This would defeat the purpose of having an independent unit BDD test. In general, software unit tests are supposed to be independent of one another.

That being said, you can certainly persist some state from one step definition and then reuse it another one:

@When("^I login with username \"(.*)\"$") 
public void enterUsername(String username) { 
    // do something with username

    // make a database call and insert the username in a temporary table
}

Then, in the second feature file you can query the same table and retrieve the username.

This may not be exactly what you have in mind, but the general idea is just to persist some state during the first test, which can then be used in a second test.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • This is a bit misleading for the OP as it doesn't explain that the state in the variables will be lost between one scenario and the next, and the database will be reset as well. Using `test` in your final sentence confuses things are you talking about a scenario here or a step definition. Step definitions are not tests. – diabolist Oct 31 '17 at 06:47
  • @diabolist `the database will be reset` ... no it won't, and we use this method to get around the problem of steps supposing to be stateless. – Tim Biegeleisen Oct 31 '17 at 06:50
  • Cucumber by default wants to reset the database between scenarios (note not Steps). You answer is confusing because it is not clear when you are talking about steps and when you are talking about scenarios. You can only share data between step definitions when they are use in the same scenario. – diabolist Oct 31 '17 at 06:54