Anand Chavan Anand Chavan - 2 years ago 413
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:


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


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


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

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 Source

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)

To fix it, use a comma:

Then /^Enter Text (.*) (.*)$/ do |fieldId, value|
  @browser.text_field(:id, fieldId).set(value)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download