Sauce McBoss
Sauce McBoss

Reputation: 6967

Expect Capybara to not find something?

Say I have a "log in" button in my app, which can be enabled / disabled by the admins. What's the best way to test that this button is not rendered when the log in function is disabled? (The page should render fine, but the button should not be there.)

I can't seem to find a way to expect the find to fail. Am I approaching this wrong?

Upvotes: 4

Views: 7394

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49950

If you would have checked for the existence of the button by doing

expect(page).to have_button('log in')

then you can do either of

expect(page).not_to have_button('log in')
expect(page).to have_no_button('log in')

to make sure the 'log in' button doesn't exist - similar can be done with have_link, have_field, have_selector, etc. If you're not using RSpec then then something along the lines of

page.assert_no_selector(:button, 'log in')

or

assert page.has_no_button?('log in')

would do what you want

Upvotes: 4

atw
atw

Reputation: 5840

Well, if your button has a class or an id then you can check if the page does not include that class or id:

expect(page).not_to have_selector "#botton_id" or ".button_class"

or if the name of your button is unique you could check if that doesn't appear on the page:

expect(page).not_to have_content('Button Name')

Upvotes: 7

Related Questions