Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
My collection of Capybara examples
# This is an example of various ways I use Capybara to test the Submit button on my crud forms.
# I am using these tests to:
# 1. Confirm the form has a submit/commit button
# 2. Confirm that the submit/commit button has a particular css class ('btn-primary' from twitter bootstrap)
# 3. Confirm that the displayed value (or caption, if you will) of the submit/commit says "Save"
# Given this html inside of a form tag:
# <input class="btn btn-primary" id="submit" name="commit" type="submit" value="Save" />
# Note, in this first example, you don't have to use [name] and/or [id], but I wanted to show that you can
# get that detailed if you want or need to.
within 'form' do
page.should have_selector("input [type=submit] [name='commit'] [id='submit'] [value='Save']")
page.should have_css("input#submit.btn.btn-primary")
# Here's a slightly different way, that does not test or depend on the [name] or [id] attributes
within 'form' do
find("input [type=submit]").value.should == "Save"
page.should have_css("input.btn.btn-primary")
# Other examples that can be helpful to study:
page.should have_selector("head title", :text => "Page Title")
page.should have_selector("h2", :text => "Some text")
page.should have_link("Link text", :href => "/url/path")
# Check the current URL
current_path.should == "/foos/new"
current_path.should == new_foo_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.