Archive

Archive for January, 2016

Cucumber: Passing Test Data as Table from Scenario

January 20, 2016 Leave a comment

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