Mechiche-Alami Saad
Mechiche-Alami Saad

Reputation: 67

Capybara is checkbox checked?

In the following HTML Code I am in a row that contains 4 columns: read, create, edit and delete (twice for cities and projects).

What I would like to do is to check if the read button is checked in projects. I am very new in capybara, I tried find('citites') then I don't know how to mention its children to check if create is checked. Your help will be very much appreciated.

<tr>
  <th class="labelCol" scope="row">Cities</th>
  <td class="dataCol col02">
    <table class="crudTable">
      <tbody>
        <tr class="crudTable">
          <td class="crudCol">
            <img src="/img/checkbox_checked.gif" alt="Checked" width="21" height="16" class="checkImg" id="crudRead___01Ib0000000118kEAA" title="Checked">
          </td>
          <td class="crudCol">
            <img src="/img/checkbox_unchecked.gif" alt="Not Checked" width="21" height="16" class="checkImg" id="crudCreate___01Ib0000000118kEAA" title="Not Checked">
          </td>
          <td class="crudCol">
            <img src="/img/checkbox_unchecked.gif" alt="Not Checked" width="21" height="16" class="checkImg" id="crudUpdate___01Ib0000000118kEAA" title="Not Checked">
          </td>
          <td class="crudCol">
            <img src="/img/checkbox_unchecked.gif" alt="Not Checked" width="21" height="16" class="checkImg" id="crudDelete___01Ib0000000118kEAA" title="Not Checked">
          </td>
          <td class="marvarCol">
            <img src="/img/checkbox_unchecked.gif" alt="Not Checked" width="21" height="16" class="checkImg" id="crudViewAll___01Ib0000000118kEAA" title="Not Checked">
          </td>
          <td class="marvarCol">
            <img src="/img/checkbox_unchecked.gif" alt="Not Checked" width="21" height="16" class="checkImg" id="crudModifyAll___01Ib0000000118kEAA" title="Not Checked">
          </td>
        </tr>
      </tbody>
    </table>
  </td>
  <th class="labelCol" scope="row">Projects</th>
  <td class="dataCol">
    <table class="crudTable">
      <tbody>
        <tr class="crudTable">
          <td class="crudCol">
            <img src="/img/checkbox_checked.gif" alt="Checked" width="21" height="16" class="checkImg" id="crudRead___01Ib00000001HDjEAM" title="Checked">
          </td>
          <td class="crudCol">
            <img src="/img/checkbox_checked.gif" alt="Checked" width="21" height="16" class="checkImg" id="crudCreate___01Ib00000001HDjEAM" title="Checked">
          </td>
          <td class="crudCol">
            <img src="/img/checkbox_checked.gif" alt="Checked" width="21" height="16" class="checkImg" id="crudUpdate___01Ib00000001HDjEAM" title="Checked">
          </td>
          <td class="crudCol">
            <img src="/img/checkbox_unchecked.gif" alt="Not Checked" width="21" height="16" class="checkImg" id="crudDelete___01Ib00000001HDjEAM" title="Not Checked">
          </td>
          <td class="marvarCol">
            <img src="/img/checkbox_unchecked.gif" alt="Not Checked" width="21" height="16" class="checkImg" id="crudViewAll___01Ib00000001HDjEAM" title="Not Checked">
          </td>
            <td class="marvarCol"><img src="/img/checkbox_unchecked.gif" alt="Not Checked" width="21" height="16" class="checkImg" id="crudModifyAll___01Ib00000001HDjEAM" title="Not Checked">
          </td>
        </tr>

enter image description here

Upvotes: 1

Views: 5237

Answers (4)

user1519240
user1519240

Reputation: 2354

expect(page).to have_checked_field("my_check_name")

Link to doc: http://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FRSpecMatchers%3Ahave_checked_field

Upvotes: 1

ChaosPredictor
ChaosPredictor

Reputation: 4051

This one work for me

assert page.has_checked_field?('your_checkbox_id')

Upvotes: 0

Olha Chekh
Olha Chekh

Reputation: 142

You can improve this with page.should have_xpath command:

page.should have_xpath('//table[@class='crudTable'][2]//td[@class='crudCol'][1]/img[@alt='Checked']')

Upvotes: -1

Evers
Evers

Reputation: 223

Your code shows obviously that your checkboxes are images.

If you had real checkboxes you could use those capybara methods to test if your checkboxes are checked or not :

has_checked_field or has_no_check_field

Upvotes: 6

Related Questions