Handling cucumber scenario examples as one scenario

大憨熊 提交于 2020-01-07 02:42:13

问题


I'm having a problem with Cucumber - right now I'm doing a mobile automation and I have features that use scenario outlines - I have a few variables in the scenario :

Scenario Outline: Menu items

Given the user is on the hamburger menu
And the language is <language>
Then menu item is <menu item>

Examples:
  | menu item           | language |
  | Search              | EN       |
  | Zoeken              | NL       |
  | Recherche           | FR       |
  | Saved properties    | EN       |
  | Bewaarde zoekertjes | NL       |
  | Biens sauvés        | FR       |
  | Saved searches      | EN       |
  | Bewaarde zoekacties | NL       |
  | Recherches sauvées   | FR       |
  | Settings            | EN       |
  | Instellingen        | NL       |
  | Paramètres          | FR       |

And when I run this scenario it restarts the application for every row (in some cases that might be good, but not this) which is very time consuming. Is there a way to point out when the application should restart the application and when it should just continue along the examples ?

I tried handling the examples as a List but that did not help.

@Then("^menu item is (.*)$")
public void menuItem(List<String> menuItems){
    for(String menuItem : menuItems)
        Assert.assertEquals( menuItem, Common.getElementAttributeByName(menuItem,"text"));
}

回答1:


As Thomas Sundberg mentioned you have many options. However for your particular case as you might need to keep all the specs as is and

Then he should be able to do it on all languages

is not enough specific. You might rewrite the scenario like this:

Scenario: Menu items
    Given the user is on the hamburger menu
    Then the possible menu items are
      | Search              | EN       |
      | Zoeken              | NL       |
      | Recherche           | FR       |
      | Saved properties    | EN       |
      | Bewaarde zoekertjes | NL       |
      | Biens sauvés        | FR       |
      | Saved searches      | EN       |
      | Bewaarde zoekacties | NL       |
      | Recherches sauvées   | FR       |
      | Settings            | EN       |
      | Instellingen        | NL       |
      | Paramètres          | FR       |

Then you could handle those like that:

    @Then("^the possible menu items are$")
    public void menuItems(Map<String, String> menuItems){
        for(Map.Entry<String, String> menuItem:menuItems.entrySet()) {
            switchToLanguage(menuItem.getValue());
Assert.assertEquals( menuItem.getKey(), Common.getElementAttributeByName(menuItem.getKey(),"text"));
}



回答2:


It sounds like you restart your application before each scenario and for a scenario outline you would like the application to be started only once.

A scenario outline is a shorthand way to write many scenarios. Each example will be unfolded and turned into a scenario and then executed.

Your options are to

  • Not restart your application before each scenario
  • Accept that the application is restarted
  • Rewrite your scenario so you don't have an example for each language

The last alternative would mean that you would move the language list down the stack. Probably into the helper class the steps delegates to.

This means a scenario like this:

Given the user is on the hamburger menu
When he order a hamburger
Then he should be able to do it on all languages  

I'm not happy with the wording here. But the idea is that you hide the list of languages deep somewhere. This will allow you to iterate over them rather than having the list of languages in a scenario outline.

This will solve the restart issues you are facing, given that you restart before each scenario.

It will also raise the abstraction level in your scenario. Currently, your scenario looks more like a script to me than a specification of a desired behaviour.



来源:https://stackoverflow.com/questions/35871105/handling-cucumber-scenario-examples-as-one-scenario

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!