Re: cucumber placeholder names

241 views
Skip to first unread message

aslak hellesoy

unread,
Nov 14, 2009, 3:52:32 PM11/14/09
to cu...@googlegroups.com
> Aslak, in a step definition, is it possible for me to access the placeholder
> name?
>
> For example, in pseudo-code:
>
>> Given /^I type in "([^"]*)",$/ do |value|
>>     fill_in <placeholder_name>, :with => value
>> end
>
> Here, the placeholder_name is also the name of the field I want to fill in.
>

I don't think I understand the question. You want Cucumber to guess
what text field you're typing into?

Aslak

> Thank you,
>

P.S. No need to email me directly or address questions to me
personally - I'm subscribed to the cukes list along with 500 other
people, and many of them can answer questions too ;-)

> Larry Siden
> http://umich.edu/~lsiden
> 734-926-9614
> skype: lsiden
>

Larry Siden

unread,
Nov 14, 2009, 3:44:03 PM11/14/09
to aslak.h...@gmail.com, cu...@googlegroups.com
Aslak, in a step definition, is it possible for me to access the placeholder name?

For example, in pseudo-code:

Given /^I type in "([^"]*)",$/ do |value|
    fill_in <placeholder_name>, :with => value
end

Here, the placeholder_name is also the name of the field I want to fill in.

Thank you,

lsiden

unread,
Nov 14, 2009, 4:38:30 PM11/14/09
to Cukes
The placeholder names are the same as the field names, so there is no
need to guess if I can find a way to access the placeholder name.
Otherwise, I will have to type 20 nearly identical steps definitions.
Guess I'll get started now, and use your answer, if I can, another
time.

Mike Sassak

unread,
Nov 14, 2009, 5:02:34 PM11/14/09
to cu...@googlegroups.com
On Sat, Nov 14, 2009 at 4:38 PM, lsiden <lsi...@gmail.com> wrote:
> The placeholder names are the same as the field names, so there is no
> need to guess if I can find a way to access the placeholder name.
> Otherwise, I will have to type 20 nearly identical steps definitions.
> Guess I'll get started now, and use your answer, if I can, another
> time.
>

Hi Larry,

Do you mean the placeholder in a Scenario Outline step? If that's the
case, then you can write a step in your scenario outline like:

Given I fill in "<placeholder>" with "<value>"

which should use the values from the examples table for placeholder
and value, and match the Webrat step definition.

Mike

> On Nov 14, 3:52 pm, aslak hellesoy <aslak.helle...@gmail.com> wrote:
>> > Aslak, in a step definition, is it possible for me to access the placeholder
>> > name?
>>
>> > For example, in pseudo-code:
>>
>> >> Given /^I type in "([^"]*)",$/ do |value|
>> >>     fill_in <placeholder_name>, :with => value
>> >> end
>>
>> > Here, the placeholder_name is also the name of the field I want to fill in.
>>
>> I don't think I understand the question. You want Cucumber to guess
>> what text field you're typing into?
>>
>> Aslak
>>
>> > Thank you,
>>
>> P.S. No need to email me directly or address questions to me
>> personally  - I'm subscribed to the cukes list along with 500 other
>> people, and many of them can answer questions too ;-)
>>
>> > Larry Siden
>> >http://umich.edu/~lsiden
>> > 734-926-9614
>> > skype: lsiden
>
> --
>
> You received this message because you are subscribed to the Google Groups "Cukes" group.
> To post to this group, send email to cu...@googlegroups.com.
> To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cukes?hl=.
>
>
>

lsiden

unread,
Nov 15, 2009, 11:01:50 AM11/15/09
to Cukes
I'm not sure I made my goal clear. Let's say I have
Examples:
| first_name | last_name | ...
| Ichabod | Crane | ...
....

Let's also assume I have been careful to use the names of my form
inputs as the names of my placeholders. Then I would like to be able
to write something like this in my feature:

"And I type "<first_name>,...",

and then in my step def:

Given /^I type "([^\"]*)" ' do |value|
fill placeholder :with => value # expression 1
end

where the variable 'placeholder' would be assigned the value
'first_name' in the closure, or context in which expression (1) is
being evaluated. Instead I have to write:

'And I type "<first_name>" into field "first_name"'...

Then in the step def:

'Given /^I type "([^\"]*)" into field "([^\"]*)"/ do |value,
name| ...'

which works fine, but requires more typing, and thus has more
opportunity for error.

Just a suggestion for a future release.

aslak hellesoy

unread,
Nov 15, 2009, 11:16:21 AM11/15/09
to cu...@googlegroups.com
On Sun, Nov 15, 2009 at 5:01 PM, lsiden <lsi...@gmail.com> wrote:
> I'm not sure I made my goal clear.  Let's say I have
> Examples:
>    | first_name | last_name | ...
>    | Ichabod     | Crane        | ...
>    ....
>

I didn't understand from your first couple of emails that you were
using Scenario Outline and Examples tables. Now your question makes
more sense to me.
The term "placeholder name" isn't a term that conveys much contextual
meaning, although I know it's mentioned in the Scenario Outline wiki
page.

I suggest we use a different name for this. How about "Scenario
Outline parameter"?

> Let's also assume I have been careful to use the names of my form
> inputs as the names of my placeholders.  Then I would like to be able
> to write something like this in my feature:
>
> "And I type "<first_name>,...",
>
> and then in my step def:
>
> Given /^I type "([^\"]*)" ' do |value|
>    fill placeholder :with => value     # expression 1
> end
>

What if I have something like this:

Scenario Outline: Eat veggies
Given I have <n> green <name> in my belly

Examples:
|n|name|
|7|potatoes|
|4|cukes|

And a step definition:

Given /I have (.*) green (.*) in my belly/ do |n, name|
# What would you expect the placeholder method to return in this case?
end

Aslak

lsiden

unread,
Nov 15, 2009, 11:52:07 AM11/15/09
to Cukes
Got me stumped!

lsiden

unread,
Nov 15, 2009, 12:21:43 PM11/15/09
to Cukes
> # What would you expect the placeholder method to return in this
case?

Aslak, FWIW, what if "placeholder" could be created as an array? Then
placeholder[0] == 'n', placeholder[1] == 'name'. Of course, I
understand that this would be only a convenience, although I'm sure
someone might find a more creative way to use this then to save me a
few keystrokes of typing. ;)

aslak hellesoy

unread,
Nov 15, 2009, 12:48:00 PM11/15/09
to cu...@googlegroups.com
On Sun, Nov 15, 2009 at 6:21 PM, lsiden <lsi...@gmail.com> wrote:
>  >   # What would you expect the placeholder method to return in this
> case?
>
> Aslak, FWIW, what if "placeholder" could be created as an array?  Then
> placeholder[0] == 'n', placeholder[1] == 'name'.  Of course, I
> understand that this would be only a convenience, although I'm sure
> someone might find a more creative way to use this then to save me a
> few keystrokes of typing. ;)
>

I suggest:

placeholders # => return them all
placeholder # => alias for placeholders[0]

Could you please create a ticket for this in Lighthouse?

Aslak

Larry Siden

unread,
Nov 15, 2009, 2:12:23 PM11/15/09
to cu...@googlegroups.com
Sure!  I'll be glad to. 

I am newbie at Ruby, and have been looking for something to crack my teeth on besides little step definitions, so maybe I'll take a shot at it myself while I tried to find work to keep the wolf away from the door.  It can't hurt - I can only learn something.  If I come up with something that works, I'll submit a patch.  Thanks much for your support.


Larry Siden
http://umich.edu/~lsiden
734-926-9614
skype: lsiden


lsiden

unread,
Nov 15, 2009, 2:33:50 PM11/15/09
to Cukes
Here's the ticket:
https://rspec.lighthouseapp.com/projects/16211-cucumber/tickets/524-access-names-of-matched-placeholders-with-array

On Nov 15, 2:12 pm, Larry Siden <lsi...@gmail.com> wrote:
> Sure!  I'll be glad to.
>
> I am newbie at Ruby, and have been looking for something to crack my teeth
> on besides little step definitions, so maybe I'll take a shot at it myself
> while I tried to find work to keep the wolf away from the door.  It can't
> hurt - I can only learn something.  If I come up with something that works,
> I'll submit a patch.  Thanks much for your support.
>
> Larry Sidenhttp://umich.edu/~lsiden
> 734-926-9614
> skype: lsiden
>
> On Sun, Nov 15, 2009 at 12:48 PM, aslak hellesoy
> > >> > >> >> >http://umich.edu/~lsiden<http://umich.edu/%7Elsiden>
> > >> > >> >> > 734-926-9614
> > >> > >> >> > skype: lsiden
>
> > >> > >> > --
>
> > >> > >> > You received this message because you are subscribed to the
> > Google Groups "Cukes" group.
> > >> > >> > To post to this group, send email to cu...@googlegroups.com.
> > >> > >> > To unsubscribe from this group, send email to
> > cukes+un...@googlegroups.com <cukes%2Bunsu...@googlegroups.com>.
> > >> > >> > For more options, visit this group athttp://
> > groups.google.com/group/cukes?hl=.
>
> > >> > > --
>
> > >> > > You received this message because you are subscribed to the Google
> > Groups "Cukes" group.
> > >> > > To post to this group, send email to cu...@googlegroups.com.
> > >> > > To unsubscribe from this group, send email to
> > cukes+un...@googlegroups.com <cukes%2Bunsu...@googlegroups.com>.
> > >> > > For more options, visit this group athttp://
> > groups.google.com/group/cukes?hl=.
>
> > > --
>
> > > You received this message because you are subscribed to the Google Groups
> > "Cukes" group.
> > > To post to this group, send email to cu...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > cukes+un...@googlegroups.com <cukes%2Bunsu...@googlegroups.com>.
> > > For more options, visit this group at
> >http://groups.google.com/group/cukes?hl=.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "Cukes" group.
> > To post to this group, send email to cu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > cukes+un...@googlegroups.com <cukes%2Bunsu...@googlegroups.com>.

Matt Wynne

unread,
Nov 15, 2009, 2:46:48 PM11/15/09
to cu...@googlegroups.com
You need to bear in mind that there isn't a 1:1 mapping between
placeholders (or scenario outline parameters) used in in Scenario
Outlines and the regexp capture arguments that are passed to a step
definition.

For example, if there was very different behaviour for potatoes and
cukes in Aslak's example, you could equally implement those steps like
this:

Given /I have (.*) (.*) potatoes in my belly/ do |n, colour|
placeholder[1].should == "name" # surprise! you thought it would
return "colour", right?
end

Given /I have (.*) (.*) cukes in my belly/ do |n, colour|
end

I have a feeling that what you're asking for is built on the
assumption that placeholders:step args are always 1:1 - am I correct?
I think this feature could just exacerbate that confusion.
cheers,
Matt

http://mattwynne.net
+447974 430184

lsiden

unread,
Nov 15, 2009, 5:03:54 PM11/15/09
to Cukes
I think I understand your concern, but what I proposed to Aslak is
that the assignment would depend only on the appearance of
placeholders in the scenario outline step, such that "Given I have <n>
green <name> in my belly" would yield placeholders == ['n', 'name'].
It would have nothing to do with the regular expression or what it
captures.

On Nov 15, 2:46 pm, Matt Wynne <m...@mattwynne.net> wrote:
> You need to bear in mind that there isn't a 1:1 mapping between  
> placeholders (or scenario outline parameters) used in in Scenario  
> Outlines and the regexp capture arguments that are passed to a step  
> definition.
>
> For example, if there was very different behaviour for potatoes and  
> cukes in Aslak's example, you could equally implement those steps like  
> this:
>
>    Given /I have (.*) (.*) potatoes in my belly/ do |n, colour|
>      placeholder[1].should == "name" # surprise! you thought it would  
> return "colour", right?
>    end
>
>    Given /I have (.*) (.*) cukes in my belly/ do |n, colour|
>    end
>
> I have a feeling that what you're asking for is built on the  
> assumption that placeholders:step args are always 1:1 - am I correct?  
> I think this feature could just exacerbate that confusion.
>
> On 15 Nov 2009, at 19:33, lsiden wrote:
>
>
>
> > Here's the ticket:
> >https://rspec.lighthouseapp.com/projects/16211-cucumber/tickets/524-a...
> > For more options, visit this group athttp://groups.google.com/group/cukes?hl=

aslak hellesoy

unread,
Nov 15, 2009, 5:07:39 PM11/15/09
to cu...@googlegroups.com
On Sun, Nov 15, 2009 at 11:03 PM, lsiden <lsi...@gmail.com> wrote:
> I think I understand your concern, but what I proposed to Aslak is
> that the assignment would depend only on the appearance of
> placeholders in the scenario outline step, such that "Given I have <n>
> green <name> in my belly" would yield placeholders == ['n', 'name'].
> It would have nothing to do with the regular expression or what it
> captures.
>

I was thinking the same thing Larry. Agree Matt?

Aslak
> To unsubscribe from this group, send email to cukes+un...@googlegroups.com.

Matt Wynne

unread,
Nov 15, 2009, 7:20:28 PM11/15/09
to cu...@googlegroups.com

On 15 Nov 2009, at 22:07, aslak hellesoy wrote:

> On Sun, Nov 15, 2009 at 11:03 PM, lsiden <lsi...@gmail.com> wrote:
>> I think I understand your concern, but what I proposed to Aslak is
>> that the assignment would depend only on the appearance of
>> placeholders in the scenario outline step, such that "Given I have
>> <n>
>> green <name> in my belly" would yield placeholders == ['n', 'name'].
>> It would have nothing to do with the regular expression or what it
>> captures.
>>
>
> I was thinking the same thing Larry. Agree Matt?

Okay cool, I'm glad we all get that :)

Don't you think it would be quite weird that you'd end up building a
step definition that could only be used in a scenario outline though?
Seems to me that could make for brittle features.

I guess there's not really any harm in it but I do worry it has the
potential to confuse more people than it helps. Sorry if I sound like
a curmudgeon ;)
>> For more options, visit this group at http://groups.google.com/group/cukes?hl=
>> .
>>
>>
>>
>
> --
>
> You received this message because you are subscribed to the Google
> Groups "Cukes" group.
> To post to this group, send email to cu...@googlegroups.com.
> To unsubscribe from this group, send email to cukes+un...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/cukes?hl=
> .
>
>

cheers,
Matt

+447974 430184
ma...@mattwynne.net
http://mattwynne.net

aslak hellesoy

unread,
Nov 15, 2009, 7:29:15 PM11/15/09
to cu...@googlegroups.com
On Mon, Nov 16, 2009 at 1:20 AM, Matt Wynne <ma...@mattwynne.net> wrote:
>
> On 15 Nov 2009, at 22:07, aslak hellesoy wrote:
>
>> On Sun, Nov 15, 2009 at 11:03 PM, lsiden <lsi...@gmail.com> wrote:
>>> I think I understand your concern, but what I proposed to Aslak is
>>> that the assignment would depend only on the appearance of
>>> placeholders in the scenario outline step, such that "Given I have
>>> <n>
>>> green <name> in my belly" would yield placeholders == ['n', 'name'].
>>> It would have nothing to do with the regular expression or what it
>>> captures.
>>>
>>
>> I was thinking the same thing Larry. Agree Matt?
>
> Okay cool, I'm glad we all get that :)
>
> Don't you think it would be quite weird that you'd end up building a
> step definition that could only be used in a scenario outline though?
> Seems to me that could make for brittle features.
>

Good point Matt

> I guess there's not really any harm in it but I do worry it has the
> potential to confuse more people than it helps. Sorry if I sound like
> a curmudgeon ;)
>

Those are valid points. I think adding this feature would be
marginally useful and potentially confusing, so I'm not very keen on
it.

Cheers,
Aslak
> To unsubscribe from this group, send email to cukes+un...@googlegroups.com.

Gregory Hnatiuk

unread,
Nov 15, 2009, 9:24:32 PM11/15/09
to cu...@googlegroups.com
Larry, would something like the following work?

Scenario Outline:  Filling out a very picky registration form
   ...
   When I fill in the following fields with:
       | First Name    | Last Name    | Address     | Country       |
       | <first_name> | <last_name>  | <address> | <country>     |  
   And I click 'submit'
   Then the result should be <result> 
   ....

  Examples
      | first_name | last_name | address | country   |  result |
      | Greg         | Hnatiuk     | 23 st.    | US         |  good  | 
      | Someone  | Else          | 0          | nowhere |  bad    |

Given /^I fill out the following fields with:$/ do |table|
    table.hashes.first.each do |k,v|
       Given %Q{I fill in "#{k}" with "#{v}"}
    end
end
        
Greg       

aslak hellesoy

unread,
Nov 15, 2009, 9:51:15 PM11/15/09
to cu...@googlegroups.com
On Mon, Nov 16, 2009 at 3:24 AM, Gregory Hnatiuk <ghna...@gmail.com> wrote:
> Larry, would something like the following work?
> Scenario Outline:  Filling out a very picky registration form
>    ...
>    When I fill in the following fields with:
>        | First Name    | Last Name    | Address     | Country       |
>        | <first_name> | <last_name>  | <address> | <country>     |
>    And I click 'submit'
>    Then the result should be <result>
>    ....
>   Examples
>       | first_name | last_name | address | country   |  result |
>       | Greg         | Hnatiuk     | 23 st.    | US         |  good  |
>       | Someone  | Else          | 0          | nowhere |  bad    |
> Given /^I fill out the following fields with:$/ do |table|
>     table.hashes.first.each do |k,v|
>        Given %Q{I fill in "#{k}" with "#{v}"}
>     end
> end
>

Or even:

When I fill in the following fields with:
| First Name | <first_name> |
| Last Name | <last_name> |
| Address | <address> |
| Country |<country> |

and in the stepdef: table.rows_hash.each do |k,v|

Aslak

Larry Siden

unread,
Nov 15, 2009, 10:02:03 PM11/15/09
to cu...@googlegroups.com
Matt Wynn wrote: "Don't you think it would be quite weird that you'd end up building a

step definition that could only be used in a scenario outline though?
Seems to me that could make for brittle features."

I didn't think of that, Matt.  I'm a newbie so it didn't occur to me that step definitions should not be so tightly coupled to the scenarios.

As for Gregory and Aslaks alternate solutions, I need to do a bit more reading so that I can understand them fully.  I haven't used those constructs yet.  Maybe if I had known about them I wouldn't have asked my question in the first place.

I have to get back to some paying work now (what I originally wrote this to test!), so I'm gonna have to let it go for now and move on.  I appreciate your responses.

Larry Siden
Reply all
Reply to author
Forward
0 new messages