Aurimas
Aurimas

Reputation: 2727

How to select elements without any text with Capybara?

I'm testing a search page, where user filters records by value. Long story short - I need to check the number of elements without any text in their node (I'm reseting the filter and checking if the elements without any text appear).

How do I select all elements without any text?

I'm using this, but it returns all the elements anyway:

expect(page.all('table.index tbody td.status', text: '').count).to be == 0

Upvotes: 0

Views: 58

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

The :text option takes a string or a regex. When it's a string it does substring matching so '' will match all the elements. What you want is

expect(page).to have_no_css('table.index tbody td.status', text: /^$/)

or

expect(page).to have_css('table.index tbody td.status', text: /^$/, count: 0)

99.9% of the time you're better off using the provided matchers (have_css, have_selector, etc.), rather than calling count on the results of all, because you'll get Capybaras waiting/retrying behavior on the expectation

Upvotes: 1

Related Questions