Ryan Glenn
Ryan Glenn

Reputation: 1416

Capybara / RSpec Ensure somethinng does not exist anywhere on the page

I want to find out how to ensure something does not exist on the page, specifically, a div with a known value. Essentially, I'm removing an element and I want to test that the element with 3 classes does not exist anywhere on the page, also there are multiple elements with the same classes, so the important part is making sure the inner of the div tag does not exist.

For example:

<div class="blah blah2 blah3">my pizza</div>

There may be 20+ divs with the same classes blah blah2 and blah3, however, I need to ensure that NONE of them include the term "my pizza", how can I do this with an expect()?

I tried something like this but it didn't work:

expect(all(:css, 'div.blah blah2 blah3').value).not_to eq('value'))

Any ideas? Tips?

UPDATE: I tried adding assert_no_text('my pizza') but this checks the whole page and I don't know if this will change in a future release of the software.

Upvotes: 0

Views: 1139

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49890

You can do

expect(page).to have_no_css('div.blah.blah2.blah3', text: 'my pizza')

or

expect(page).not_to have_css('div.blah.blah2.blah3', text: 'my pizza')

Assuming you have correctly included Capybaras RSpec matchers each of those will do exactly the same thing, wait up to Capybara.default_max_wait_time for any matching element on the page to go away. If you don't like writing CSS class selectors (note you need a . before each class on the same element in a CSS selector - your example with space separation would be looking for nested elements) you can also use the class option

expect(page).not_to have_css('div', class: %w[blah blah2 blah3], text: 'my pizza') 

Also if you have elements that could have my pizza, my pizza 2 , my pizza 3 and you want to only ensure exactly my pizza is not there you could use the exact_text option

expect(page).not_to have_css('div.blah.blah2.blah3', exact_text: 'my pizza')

Upvotes: 1

Oleksandr Holubenko
Oleksandr Holubenko

Reputation: 4440

I think something like this should help you:

expect(page).not_to have_css('div.blah blah2 blah3', text: 'value')

Upvotes: 0

Related Questions