使用Capybara确定表单字段是否存在

时间:2022-02-18 08:28:46

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

我想确定存在id =“foo”的输入字段:

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

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

我还想确保在某些条件下不存在id =“bar”的另一个输入字段。

2 个解决方案

#1


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.

注意:不要使用expect(page).to_not have_css,因为它会等待你的元素出现。

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

对has_no_selector等期望和否定形式的解释?而不是has_selector?可以在Capybara的异步JavaScript文档中找到。

#2


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.

在这些Capybara节点匹配器文档中搜索has_xpath。

#1


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.

注意:不要使用expect(page).to_not have_css,因为它会等待你的元素出现。

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

对has_no_selector等期望和否定形式的解释?而不是has_selector?可以在Capybara的异步JavaScript文档中找到。

#2


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.

在这些Capybara节点匹配器文档中搜索has_xpath。