Search code examples
cucumberbddgherkincucumber-javaqaf

Reusable/Generic Examples table in Cucumber


Is it possible for multiple scenarios to use the same Examples table?

So instead of having something like the following:

Scenario Outline: First Scenario
    Given I am viewing "<url>"
    Then I assert that the current URL "<url>"
    Examples:
      | url                |
      | https://google.com |
      | https://twitter.com|

  Scenario Outline: Second Scenario
    Given I am viewing "<url>" with route "</contactus>"
    Then I assert that "<url>" contains "contactus"
    Examples:
      | url                |
      | https://google.com |
      | https://twitter.com|

I can do something like

Scenario Outline: Reusable Example
    Examples:
      | url                |
      | https://google.com |
      | https://twitter.com|

  Scenario: First Scenario
    Given I am viewing "<url>"
    Then I assert that the current URL "<url>"

  Scenario: Second Scenario
    Given I am viewing "<url>" with route "</contactus>"
    Then I assert that "<url>" contains "contactus"

I found a similar question on StackOverflow, but merging all my scenarios in just one scenario is not an option for me. Since this question was posted in 2014, maybe there have been some advancements in the framework which I am not aware of :D

Thank you in advance.


Solution

  • 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.