Reusing cucumber data tables

2019-09-18 22:21发布

问题:

How to reuse cucumber test data across several scenarios or feature files? I would like to bypass table data code duplication.

My current feature file:

Scenario: At begining unable to click on first
    When On Sidebar page I click element basic table pagination
    And On PaginationTable page I click element first
    Then On PaginationTable page label pagination info value is Showing 1 to 13 of 1497 rows
    And I check table result pagination for PaginationTable page
        |noId|first_name|last_name|email|
        |1|Abbba|Baaba|big0@data.com|
        |2|Left|Right|big1@data.com|
    And I close and log out

Scenario: At begining unable to click on left
    When On Sidebar page I click element basic table pagination
    And On PaginationTable page I click element left
    Then On PaginationTable page label pagination info value is Showing 1 to 13 of 1497 rows
    And I check table result pagination for PaginationTable page
        |noId|first_name|last_name|email|
        |1|Abbba|Baaba|big0@data.com|
        |2|Left|Right|big1@data.com|
    And I close and log out

回答1:

I'll answer your question below, but I think this is probably a case for a scenario outline instead, which would allow you to reuse your table just in a different fashion. Your scenarios are identical save for which element you click. This screams scenario outline.

Just looking at your gherkin, it feels like you have too much specificity and are building a brittle test, but you know more what data you have in there and how it is loaded/managed during the test run. Just something to think about.


Answer to your question

This type of feature is not directly supported by Cucumber, however if you use a background step for setup, you can build your table there and reuse it, but only within the same feature file. It would not work across feature files.

In the background given, just store the table as a variable and reference that in your steps.

Background:
  Given I expect table result pagination to be:
        |noId|first_name|last_name|email        |
        |1   |Abbba     |Baaba    |big0@data.com|
        |2   |Left      |Right    |big1@data.com|

Scenario: At beginning unable to click on first
    When On Sidebar page I click element basic table pagination
    And On PaginationTable page I click element first
    Then On PaginationTable page label pagination info value is Showing 1 to 13 of 1497 rows
    And I check table result pagination for PaginationTable page
    And I close and log out

Scenario: At beginning unable to click on left
    When On Sidebar page I click element basic table pagination
    And On PaginationTable page I click element left
    Then On PaginationTable page label pagination info value is Showing 1 to 13 of 1497 rows
    And I check table result pagination for PaginationTable page
    And I close and log out

Alternative implementation using scenario outline

Scenario Outline: At beginning unable to click on element
    When On Sidebar page I click element basic table pagination
    And On PaginationTable page I click <element>
    Then On PaginationTable page label pagination info value is Showing 1 to 13 of 1497 rows
    And I check table result pagination for PaginationTable page
        |noId|first_name|last_name|email|
        |1|Abbba|Baaba|big0@data.com|
        |2|Left|Right|big1@data.com|
    And I close and log out

  Examples:
     | element |
     | first   |
     | left    |


标签: cucumber