user664833
user664833

Reputation: 19505

Using Capybara determine whether a form field exists or not

I want to determine that an input field with id="foo" exists:

<input id="foo" ... />

And I also want to ensure that under certain conditions another input field with id="bar" does not exist.

Upvotes: 1

Views: 1429

Answers (2)

diabolist
diabolist

Reputation: 4099

Exists:

expect(page).to have_css('#bar')

Doesn't exist:

expect(page).to have_no_css('#bar')

Note: Don't use expect(page).to_not have_css, as that will wait for your element to appear.

An explanation of expect and negative forms like has_no_selector? and not has_selector? can be found in Capybara's Asynchronous JavaScript documentation.

Upvotes: 2

user664833
user664833

Reputation: 19505

To determine that a particular input field exists, use:

assert has_xpath?("//input[@id='foo']")

To determine that a particular input field does not exist, use one of:

assert has_no_xpath?("//input[@id='bar']")
refute has_xpath?("//input[@id='bar']")

Search for has_xpath in these Capybara node matcher docs.

Upvotes: 0

Related Questions