Anand Chavan Anand Chavan - 10 months ago 198
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 :)


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)