Ruby-on-rails – Clicking on image using RSpec & Capybara

capybararspecrubyruby-on-railstesting

I'm using Rspec and Capybara.

I am attempting to click an image, but cannot find a way to get Capybara to click the image. I'm not sure what to do, as I have in fact same class, alt and src.

Thanks

Example of html

<tr class="even">
  <td class="gridData" style="text-align:center;">
  <td class="gridData"> 221 </td>
  <td class="gridData" route="default" reset="0" urlparams="users index edit {userID}" label="Username">
  <td class="gridData">
  <img class="user-info-grid" alt="info" src="/themes/system/images/icon/16/information.png">
  </td>
</tr>

 <tr class="odd">
  <td class="gridData" style="text-align:center;">
  <td class="gridData"> 222 </td>
  <td class="gridData" route="default" reset="0" urlparams="users index edit {userID}" label="Username">
  <td class="gridData">
  <img class="user-info-grid" alt="info" src="/themes/system/images/icon/16/information.png">

In fact here is the source, which I want to test:

  <img class="user-info-grid" alt="info" src="/themes/system/images/icon/16/information.png">

But as I said, I cannot find a way to get Capybara to click the image.

Any ideas?

Best Answer

You can use this:

page.first(".user-info-grid").click

This is use to find user-info-grid class and apply click function.

If multiple classes are comes with this name then the click will execute in first found class.