Search code examples
ruby-on-railsrspeccapybararspec-rails

Rspec/Capybara matching data across rows and columns


I've got HTML looking something like:

<div class="row">
  <div class="column">
    Customer Alice
  </div>
  <div class="column">
    Status: enrolled / signed
  </div>
</div>
<div class="row">
  <div class="column">
    Customer Boris
  </div>
  <div class="column">
    Status: signed
  </div>
</div>

Having some challenges with matching across rows and columns.

When using Rspec/Capybara, I update details for Boris so that his status becomes enrolled / signed i.e.

<div class="row">
  <div class="column">
    Customer Alice
  </div>
  <div class="column">
    Status: enrolled / signed
  </div>
</div>
<div class="row">
  <div class="column">
    Customer Boris
  </div>
  <div class="column">
    Status: enrolled / signed
  </div>
</div>

This is easy to confirm on-screen visually, by a human.

Since I am using Capybara and Poltergeist :), how do I easily match to confirm that Boris's status is expected to be enrolled / signed for the spec?


Solution

  • Something like

    row = find('.row', text: 'Customer Boris')
    expect(row).to have_text('enrolled')
    

    would do what you want - if you wanted to do it all in one you could pass a regex for the text match so

    expect(page).to have_selector('.row', text: /Customer Boris.*enrolled/)

    or something like that