Home > cucumber, ruby, selenium > Cucumber: Passing Test Data as Table from Scenario

Cucumber: Passing Test Data as Table from Scenario

While we can pass test data as table from the feature file through Scenario Outline:

Scenario Outline: User updates user information
 Given I login to application
 And I go to user profile page
 And I enter "<gender>" into field of "gender_element";
 And I enter "<age>" into field of "age_element";
 And I enter "<location>" into field of "location_element";
 Then I verify user updates are saved
Examples:
 | gender | age | location |
 | Female | 20 | Singapore |

Here’s another option to pass your data in table format through your step definition.

Scenario: User updates user information
 Given I login to application
 And I go to user profile page
 When I update user information with options:
  | Gender | Female |
  | Age | 20 |
  | Location | Singapore |
 Then I verify user updates are saved

#Step Definitions File

#step definition to populate textfields
Then /^I enter "(.*)" into field of "(.*)"$/ do |value, field|
 textfield(I18n.translate!(field)).click
 textfield(I18n.translate!(field)).send_keys value
end

#step definition that accepts test data from the feature file as table
When /^I update user information(?: with options:)?$/ do |table|

 options = table.rows_hash
 step %Q[I enter "#{options['Gender']}" into field of "gender_element"]
 step %Q[I enter "#{options['Age']}" into field of "age_element"]
 step %Q[I enter "#{options['Location']}" into field of "location_element"]
 step %Q[I press button labelled "bt_address_suggestion_use_this"] 

#step definition that accepts test data from the feature file as table with default value
When /^I update user information(?: with options:)?$/ do |table|
 default_value = {
 'Age' = "18",
 'Location' = "United States"
 }
 options = default_value.merge(table.rows_hash)
 step %Q[I enter "#{options['Gender']}" into field of "gender_element"]
 step %Q[I enter "#{options['Age']}" into field of "age_element"]
 step %Q[I enter "#{options['Location']}" into field of "location_element"]
 step %Q[I press button labelled "bt_address_suggestion_use_this"]
Advertisements
Categories: cucumber, ruby, selenium
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: