Can DataTable and Parameter be used at the same time in Cucumber?

Viewed 3739

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.

2 Answers

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) {

}

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");
}
`````
Related