如何测试capybara中是否存在提交按钮?

时间:2022-09-25 16:13:44

I want to have a test in rspec for the existence of a submit button. I am using capybara as well.

我想在rspec中测试提交按钮的存在性。我也在使用水豚。

I have tried:

我有尝试:

should have_tag("input","Submit button")

and

should have_content("Submit, button")

but it either raises an exception or gives false positives.

但它要么提出一个例外,要么给出错误的肯定。

7 个解决方案

#1


32  

These are all good suggestions, but if you want to confirm that it's a button and that it has the right value (for display), you have to be a little more detailed:

这些都是很好的建议,但如果你想确认它是一个按钮,并且它有正确的值(用于显示),你必须更详细一些:

page.should have_selector("input[type=submit][value='Press Me']")

I don't know of an existing matcher that does this. Here's a custom RSpec2 matcher I wrote:

我不知道有一个现有的匹配程序可以做到这一点。这里有一个定制的RSpec2 matcher我写:

RSpec::Matchers.define :have_submit_button do |value|
  match do |actual|
    actual.should have_selector("input[type=submit][value='#{value}']")
  end
end

Here's the RSpec3 version (courtesy @zwippie):

以下是RSpec3版本(由@zwippie提供):

RSpec::Matchers.define :have_submit_button do |value|
  match do |actual|
    expect(actual).to have_selector("input[type=submit][value='#{value}']")
  end
end

I keep it in spec/support/matchers/request_matchers.rb with my other custom matchers. RSpec picks it up automatically. Since this is an RSpec matcher (rather than a Capybara finder), it will work in both feature specs (Capybara) and view specs (RSpec without Capybara).

我将它保存在spec/support/matchers/request_matchers中。rb和我的其他自定义配对。RSpec会自动接收它。由于这是一个RSpec matcher(而不是Capybara查找程序),它将同时适用于特性规格(Capybara)和视图规格(没有Capybara的RSpec)。

Feature spec usage:

功能规范用法:

page.should have_submit_button("Save Me")

View spec usage (after calling render):

查看规范使用情况(调用渲染后):

rendered.should have_submit_button("Save Me")

Note that if you're in a Capybara request spec, and you would like to interact with a submit button, that's a lot easier:

请注意,如果您在Capybara的请求规范中,并且您希望与提交按钮交互,那么这将会容易得多:

click_button "Save Me"

There's no guarantee that it's actually a submit button, but your feature specs should just be testing the behavior and not worrying about that level of detail.

并不能保证它实际上是一个提交按钮,但是您的特性规范应该只是测试行为,而不必担心这个级别的细节。

#2


14  

There is a built-in matcher has_button?.

有一个内置的matcher has_button?。

Using RSpec you can have an assertion like

使用RSpec可以得到类似的断言

page.should have_button('Submit button')

Or with new RSpec 3 syntax:

或者使用新的RSpec 3语法:

expect(page).to have_button('Submit button')

#3


2  

I have one (used in cucumber):

我有一个(黄瓜用的):

Then /^I should see "([^"]*)" button/ do |name|
  should have_button name
end

In negative use: have_no_button

-使用:have_no_button

#4


1  

if your HTML mark up is just something like:

如果你的HTML标记是这样的:

<input type="submit"></input>

Then you can do the following in capybara:

然后你可以在水豚做以下的事情:

page.should have_selector('input[type=submit]')

#5


0  

I have something like:

我有类似:

page.find("#submitButton").visible?

#6


0  

Try this

试试这个

it { should have_xpath("//input[@value='Sign up']") }

#7


-1  

Try:

试一试:

it { should have_selector('input', value: "Submit") }

UPDATE: I suspect this answer may not work as desired in some cases. When I use this to test for the value in other input tags, it seems to pass no matter what the value is.

更新:我怀疑这个答案在某些情况下可能并不如预期的那样有效。当我使用它来测试其他输入标记中的值时,无论值是什么,它似乎都通过。

#1


32  

These are all good suggestions, but if you want to confirm that it's a button and that it has the right value (for display), you have to be a little more detailed:

这些都是很好的建议,但如果你想确认它是一个按钮,并且它有正确的值(用于显示),你必须更详细一些:

page.should have_selector("input[type=submit][value='Press Me']")

I don't know of an existing matcher that does this. Here's a custom RSpec2 matcher I wrote:

我不知道有一个现有的匹配程序可以做到这一点。这里有一个定制的RSpec2 matcher我写:

RSpec::Matchers.define :have_submit_button do |value|
  match do |actual|
    actual.should have_selector("input[type=submit][value='#{value}']")
  end
end

Here's the RSpec3 version (courtesy @zwippie):

以下是RSpec3版本(由@zwippie提供):

RSpec::Matchers.define :have_submit_button do |value|
  match do |actual|
    expect(actual).to have_selector("input[type=submit][value='#{value}']")
  end
end

I keep it in spec/support/matchers/request_matchers.rb with my other custom matchers. RSpec picks it up automatically. Since this is an RSpec matcher (rather than a Capybara finder), it will work in both feature specs (Capybara) and view specs (RSpec without Capybara).

我将它保存在spec/support/matchers/request_matchers中。rb和我的其他自定义配对。RSpec会自动接收它。由于这是一个RSpec matcher(而不是Capybara查找程序),它将同时适用于特性规格(Capybara)和视图规格(没有Capybara的RSpec)。

Feature spec usage:

功能规范用法:

page.should have_submit_button("Save Me")

View spec usage (after calling render):

查看规范使用情况(调用渲染后):

rendered.should have_submit_button("Save Me")

Note that if you're in a Capybara request spec, and you would like to interact with a submit button, that's a lot easier:

请注意,如果您在Capybara的请求规范中,并且您希望与提交按钮交互,那么这将会容易得多:

click_button "Save Me"

There's no guarantee that it's actually a submit button, but your feature specs should just be testing the behavior and not worrying about that level of detail.

并不能保证它实际上是一个提交按钮,但是您的特性规范应该只是测试行为,而不必担心这个级别的细节。

#2


14  

There is a built-in matcher has_button?.

有一个内置的matcher has_button?。

Using RSpec you can have an assertion like

使用RSpec可以得到类似的断言

page.should have_button('Submit button')

Or with new RSpec 3 syntax:

或者使用新的RSpec 3语法:

expect(page).to have_button('Submit button')

#3


2  

I have one (used in cucumber):

我有一个(黄瓜用的):

Then /^I should see "([^"]*)" button/ do |name|
  should have_button name
end

In negative use: have_no_button

-使用:have_no_button

#4


1  

if your HTML mark up is just something like:

如果你的HTML标记是这样的:

<input type="submit"></input>

Then you can do the following in capybara:

然后你可以在水豚做以下的事情:

page.should have_selector('input[type=submit]')

#5


0  

I have something like:

我有类似:

page.find("#submitButton").visible?

#6


0  

Try this

试试这个

it { should have_xpath("//input[@value='Sign up']") }

#7


-1  

Try:

试一试:

it { should have_selector('input', value: "Submit") }

UPDATE: I suspect this answer may not work as desired in some cases. When I use this to test for the value in other input tags, it seems to pass no matter what the value is.

更新:我怀疑这个答案在某些情况下可能并不如预期的那样有效。当我使用它来测试其他输入标记中的值时,无论值是什么,它似乎都通过。