Alex Rivera | Logout

Clicking on image using Capybara in cucumber

Asked 2011-11-17T21:44:07.117
9

I am attempting to click an image with Capybara for a Cucumber test, but cannot find a way to get Capybara to see the image as a link.

My code for the image is:

link_to(image_tag('ico_edit_16.png', alt: 'Edit', class: 'icon16', title: "Edit #{qualification.title}"), edit_qualification_path(qualification))

Which shows up as

<a href="/qualifications/1/edit">
    <img class="icon16" title="Title" src="/images/ico_edit_16.png?1279303992" alt="Edit">
</a>

in the html, and I have been unable to find a good way to use capybara to click the image.

Any suggestions?

Edit
Report

1 Answer

0

Also note, in Rails (3.1.1 in my case), for images without a given :alt value, a default alt is set by the image_tag helper:

:alt - If no alt text is given, the file name part of the source is used (capitalized and without the extension) [From the Rails API]

So for example, for image file "kitten.jpg", you would use find("img[@alt='Kitten']").click (note capitalization and removal of extension).

Using this rails convention, you can search for arbitrary image links in cucumber/capybara.

answered 2012-01-02T21:22:46.013

Your Answer