Reputation: 2121
I want to have a test in rspec for the existence of a submit button. I am using capybara as well.
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.
Upvotes: 18
Views: 19306
Reputation: 2045
There is a built-in matcher has_button?.
Using RSpec you can have an assertion like
page.should have_button('Submit button')
Or with new RSpec 3 syntax:
expect(page).to have_button('Submit button')
Upvotes: 17
Reputation: 17323
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:
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):
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).
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:
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.
Upvotes: 38
Reputation: 1973
I have one (used in cucumber):
Then /^I should see "([^"]*)" button/ do |name|
should have_button name
end
In negative use: have_no_button
Upvotes: 2
Reputation: 3341
if your HTML mark up is just something like:
<input type="submit"></input>
Then you can do the following in capybara:
page.should have_selector('input[type=submit]')
Upvotes: 1
Reputation: 2227
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.
Upvotes: -1