Anand Chavan Anand Chavan - 6 months ago 98
Ruby Question

How to pass multiple parameters to a Cucumber step?

I want to pass 2 strings from a step as shown in the code below:

testfeature.feature

Then Enter Text 'fName' <fname>
Then Enter Text 'mName' <mname>
Then Enter Text 'lName' <lname>

Examples:

| fname | mname | lname |
| FnameTest | FnameTest | FnameTest |


testfeature.rb

Then /^Enter Text (.*) (.*)$/ do |fieldId|value|
@browser.text_field(:id, fieldId).set(value)
end


Here the fname/lname/mname is being taken from Examples and I am passing the page element through the single quoted string.

Since I'm new with Ruby/Cucumber/Watir, I am not able to understand how this should be handled.

Please Note -

Previously I had written a separate regular expression for all 3 steps, however for optimizing I'm trying this approach.

Looking forward for all happy thoughts :)

Answer

Ruby block parameters are separated with ,, not |. Ruby will parse what you wrote like

Then /^Enter Text (.*) (.*)$/ do |fieldId|
  value | @browser.text_field(:id, fieldId).set(value)
end

To fix it, use a comma:

Then /^Enter Text (.*) (.*)$/ do |fieldId, value|
  @browser.text_field(:id, fieldId).set(value)
end
Comments