[webrat] webrat+cucumber - Specifying which link to click when many have same name on page

246 views
Skip to first unread message

arifba

unread,
May 14, 2010, 2:14:22 PM5/14/10
to webrat
Hi all,

I have a page containing a table with multiple links ('View') with the
same name. The only thing that differs b/w them is the text they are
associated with. For example:
Acme X Corp <a href="clients/1">View</a>
Acme Y Corp <a href="clients/2">View</a>
etc.

In Cucumber I want to specify which View link to follow (ie. Given I
follow "View" for "Acme X Corp"). I know that in webrat's included
steps I can specify an id to click, or do a 'within' statement however
giving each link a unique id does not seem like the way to go.

What's best practice here? Should I give each anchor tag a name
attribute matching the company name and match on that (don't love that
as the only reason I'd be doing so would be for cucumber and not for
any other reason)? Or how do most handle this sort of simple
situation?

Thanks,
Arif

--
You received this message because you are subscribed to the Google Groups "webrat" group.
To post to this group, send email to web...@googlegroups.com.
To unsubscribe from this group, send email to webrat+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webrat?hl=en.

Matt Wynne

unread,
May 14, 2010, 3:18:45 PM5/14/10
to web...@googlegroups.com

On 14 May 2010, at 19:14, arifba wrote:

> Hi all,
>
> I have a page containing a table with multiple links ('View') with the
> same name. The only thing that differs b/w them is the text they are
> associated with. For example:
> Acme X Corp <a href="clients/1">View</a>
> Acme Y Corp <a href="clients/2">View</a>
> etc.
>
> In Cucumber I want to specify which View link to follow (ie. Given I
> follow "View" for "Acme X Corp"). I know that in webrat's included
> steps I can specify an id to click, or do a 'within' statement however
> giving each link a unique id does not seem like the way to go.

You should be able to use an XPath query to find the tag that contains the company name, then walk the XML to the corresponding "View" link. If you can't do it with webrat, try capybara - you can definitely do that kind of thing with capybara, if your XPath fu is strong enough.

Damian Janowski

unread,
May 14, 2010, 5:29:07 PM5/14/10
to web...@googlegroups.com
On Fri, May 14, 2010 at 3:14 PM, arifba <arif....@gmail.com> wrote:
> Hi all,
>
> I have a page containing a table with multiple links ('View') with the
> same name. The only thing that differs b/w them is the text they are
> associated with. For example:
> Acme X Corp <a href="clients/1">View</a>
> Acme Y Corp <a href="clients/2">View</a>
> etc.
>
> In Cucumber I want to specify which View link to follow (ie. Given I
> follow "View" for "Acme X Corp"). I know that in webrat's included
> steps I can specify an id to click, or do a 'within' statement however
> giving each link a unique id does not seem like the way to go.
>
> What's best practice here? Should I give each anchor tag a name
> attribute matching the company name and match on that (don't love that
> as the only reason I'd be doing so would be for cucumber and not for
> any other reason)? Or how do most handle this sort of simple
> situation?

Hello Arif,

Try this:

click_link_within "//table/tr[./td[contains(., 'Acme Corp X')]]", "View"

Damian Janowski

unread,
May 14, 2010, 5:32:00 PM5/14/10
to web...@googlegroups.com
On Fri, May 14, 2010 at 4:18 PM, Matt Wynne <ma...@mattwynne.net> wrote:
> You should be able to use an XPath query to find the tag that contains the company name, then walk the XML to the corresponding "View" link. If you can't do it with webrat, try capybara - you can definitely do that kind of thing with capybara, if your XPath fu is strong enough.

Hello Matt,

We all like Capybara and that's why we have the intention of merging
the two projects.

That said – are you going to answer to every question on this list
with a suggestion to try Capybara, even when you don't provide any
answer to the problem of the OP?

Matt Wynne

unread,
May 15, 2010, 3:11:21 AM5/15/10
to web...@googlegroups.com

On 14 May 2010, at 22:32, Damian Janowski wrote:

> On Fri, May 14, 2010 at 4:18 PM, Matt Wynne <ma...@mattwynne.net> wrote:
>> You should be able to use an XPath query to find the tag that contains the company name, then walk the XML to the corresponding "View" link. If you can't do it with webrat, try capybara - you can definitely do that kind of thing with capybara, if your XPath fu is strong enough.
>
> Hello Matt,
>
> We all like Capybara and that's why we have the intention of merging
> the two projects.
>
> That said – are you going to answer to every question on this list
> with a suggestion to try Capybara, even when you don't provide any
> answer to the problem of the OP?

Sorry yeah I have sounded like a bit of a capybara fanboy recently eh?

Only because I see a lot of questions on this webrat list that go unanswered. I feel like if all I have time to do is point the person to somewhere they might get some more help, then that's better than nothing.

No diss to webrat here - I've loved it since day one.

arifba

unread,
May 26, 2010, 8:33:22 PM5/26/10
to webrat
Thanks Damian and Matt,

Damian, tried your suggestion, however I receive an error:
'unexpected '//' after '' (Nokogiri::CSS::SyntaxError)'

Tracked down this open webrat bug:
https://webrat.lighthouseapp.com/projects/10503-webrat/tickets/153-within-should-support-xpath

There is a workaround in the ticket that I will try.

Thanks again.

On May 14, 2:29 pm, Damian Janowski <djanow...@dimaion.com> wrote:

Reply all
Reply to author
Forward
0 new messages