Reusable/Generic Examples table in Cucumber

血红的双手。 提交于 2019-12-06 15:22:46

You can use qaf-gherkin where you can move examples in external file and use it with one or more scenario. With qaf your feature file may look like below:

Scenario Outline: First Scenario
   Given I am viewing "<url>"
   Then I assert that the current URL "<url>"
   Examples:{'datafile':'resources/testdata.txt'}

Scenario Outline: Second Scenario
Given I am viewing "<url>" with route "</contactus>"
Then I assert that "<url>" contains "contactus"
Examples:{'datafile':'resources/testdata.txt'}

And your datafile will look like:

url
https://google.com
https://twitter.com

Here is the reference.

You might use a Background to specify steps which are equal for all scenarios. (Have a look on the link for constraints)

A feature file might look like

Feature: use of reusable Given

  Background: Reusable Example
    Given I am viewing url
      | https://google.com |
    And a search phrase is entered in the search field

  Scenario: First Scenario
    And step for first scenario

  Scenario: Second Scenario
    And step for second scenario

implementing the glue code for the Given

@Given("^I am viewing url$")
public void iAmViewing(List<String> url) throws Throwable {
    System.out.println("url = " + url);
}

edit After the question has been updated a Scenario Outline could work for both examples.

Feature: use of example

  Scenario Outline: First Scenario
    Given I am viewing "<host>" with path "<path>"
    Then I assert that the current URL is "<host><path>"

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