dB'
dB'

Reputation: 8330

Cucumber/Capybara: check that a page does NOT have content?

Using Cucumber and Capybara, is there a way to verify that a string is NOT present on a page?

For example, how would I write the opposite of this step:

Then /^I should see "(.*?)"$/ do |arg1|
  page.should have_content(arg1)
end

This passes if arg1 is present.

How would I write a step that fails if arg1 is found?

Upvotes: 23

Views: 39796

Answers (5)

Raphael Abreu
Raphael Abreu

Reputation: 211

currently, you can use:

Then /^I not see "(.*?)"$/ do |arg1|
  expect(page).to have_no_content(arg1)
end

And if the content is found in the page, your test is red

Upvotes: 4

Micah
Micah

Reputation: 1716

In Rspec 3.4 currently (2016) this is the recommended way to test for not having content:

expect(page).not_to have_content(arg1)

Upvotes: 17

Adam Sheehan
Adam Sheehan

Reputation: 2172

You can also use should_not if you want it read a little better:

Then /^I should not see "(.*?)"$/ do |arg1|
  page.should_not have_content(arg1)
end

Some more info: https://www.relishapp.com/rspec/rspec-expectations/docs

Upvotes: 8

Piotr Jakubowski
Piotr Jakubowski

Reputation: 1750

http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers#has_no_text%3F-instance_method

There is a has_no_content matcher in Capybara. So you can write

  Then /^I should not see "(.*?)"$/ do |arg1|
    page.should have_no_content(arg1)
  end

Upvotes: 35

dB'
dB'

Reputation: 8330

Oh, wait, I figured it out. This works:

Then /^I should see "(.*?)"$/ do |arg1|
  page.has_content?(arg1) == false
end

Upvotes: -4

Related Questions