bts
bts

Reputation: 358

How do I assert within the first element of many in Capybara?

I want to do something like this:

within '.Collection .Thing:first-child' do
  page.must_have_content 'lorem ipsum'
end

Where .Thing is just one of many other Things inside Collection. I tried the code above but it says it's ambiguous. Is there a way to resolve the ambiguity?

Upvotes: 0

Views: 648

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49910

If you really just want the first matching element on the page you can do

within first('.Collection .Thing:first-child', minimum: 1) do
  page.must_have_content 'lorem ipsum'
end

the minimum: 1 option isn't necessarily needed if the page is already fully loaded, however it will make Capybara wait for at least one matching element to show on the page if the page isn't yet fully loaded (or the list is filled via ajax, etc). Without it #first would normally return immediately with nil if no matching elements were on the page

Upvotes: 3

Related Questions