3

Something I've been running into several times now.

Example:

  Scenario: When I flick the switch, different colors appear
    Given that the electricity bill is paid
    When switch flicked is true
      | blue  |
      | green |
      | red   |
    Then the lightbulb should show red at the end

And the following step definition (Given and When are not relevant for the question):

@When("switchFlicked is {}")
public void switchFlickedIs(boolean isSwitchFlicked, DataTable dataTable) {
    DiscoLight.setColours(dataTable.asList());
    DiscoLight.setSwitch(isSwitchFlicked);
}

Can both parameters and data tables be used at the same time? If so: how? Because the above will not work.

Fahim Uz Zaman
  • 444
  • 3
  • 6
Robert van der Spek
  • 1,017
  • 3
  • 16
  • 37
  • Firstly, your syntax is not correct, see [here](https://stackoverflow.com/a/64424395/3355860) for an example. Then, maybe put true/false in the table. – ou_ryperd Oct 28 '20 at 13:42
  • Not much wrong with the syntax, you can put a table there. Putting the boolean in the middle doesn't change. (Example works when I only use the boolean, or only use the datatable, but not both at the same time.) – Robert van der Spek Oct 28 '20 at 14:29
  • 1
    U can try by specifying the parameter type. Try this in the feature file - `When switch flicked is 'true'`. And the step def method - `@Given("When switch flicked is {string}") - public void switchFlickedIs(String value, DataTable dataTable)` – Grasshopper Oct 28 '20 at 15:02
  • 1
    Try using Scenario Outline and example table. You can pass the value in the example table and use it Feature as "" where color is one of the columns in your example table having different colors in row –  Oct 28 '20 at 15:59

2 Answers2

1

Can both parameters and data tables be used at the same time?

Yes.

If so: how? Because the above will not work.

Note that your step is: switch flicked is true while your step definition is: switchFlicked is {}. This is effectively the same as writing the regex ^switchFlicked is (.*)$. Because the space and capitalization matter this regex will never match your step.

Instead try using:

@When("switch flicked is {}")
public void switchFlickedIs(boolean isSwitchFlicked, DataTable dataTable) {

}
M.P. Korstanje
  • 10,426
  • 3
  • 36
  • 58
0

Scenario Outline: When I flick the switch, different colors appear Given that the electricity bill is paid When switch flicked is "" | blue | | green | | red | Then the lightbulb should show "" at the end

Example |TC|SwitchState|Color| |1|True|Red|

StepDeinition


@When("switch flicked is {string}")
public String switchFlickedIs(String SwitchState,List<String>colors){
   String Set Color= "";
   If SwitchState.equals(true){
    for(int i=0; i<colors.size();i++){
      SetColor= colors.get(i)
     }
    retutn SetColor; 
   }else 
    return SetColor;
}

@Then("Then the lightbulb should show {string} at the end")
public void VerifyLightbulb color(String FinalColor){
  Assert.equals(FinalColor,SetColor, "Final Color is not red");
}
`````
  • 1
    This did not work for me. The Step is expecting only one argument -> string, but the definition has two arguments -> String and List<>. Got the below exception: io.cucumber.core.exception.CucumberException: Step [switch flicked is {string}] is defined with 2 parameters at 'stepdefinitions.Test_Steps.switchFlickedIs(java.lang.String,io.cucumber.datatable.DataTable)'. However, the gherkin step has 1 arguments – user1710861 Jun 18 '21 at 08:40