Skip to content

Instantly share code, notes, and snippets.

@futhr
Forked from ootoovak/capybara cheat sheet.rb
Last active December 20, 2015 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save futhr/6146666 to your computer and use it in GitHub Desktop.
Save futhr/6146666 to your computer and use it in GitHub Desktop.
Collection of Capybara >= 2.1 stuff for feature specs.
# Navigating
visit '/projects'
visit post_comments_path(@post)
# Clicking links and buttons
click_link 'id_of_link'
click_link 'Link Text'
click_button 'Save'
click_on 'Link Text' # Click either a link or a button.
click_on 'Button Value'
# Matching etc. (http://www.elabs.se/blog/60-introducing-capybara-2-1)
click_link 'Link Text', exact: false # Allow substring matches.
click_link 'Link Text', match: :first # Just picks the first element that matches.
click_link 'Link Text', match: :one # Raises an error if more than one element matches.
click_link 'Link Text', match: :smart # An error is raised if more than one element is found. If no element is found, a new search is performed which allows partial matches. If that search returns multiple matches, an error is raised.
click_link 'Link Text', match: :prefer_exact # If multiple matches are found, some of which are exact, and some of which are not, then the first exactly matching element is returned.
click_link 'Link Text', wait: 1.5 # New wait method.
click_link 'Link Text', visible: false # Ignore if its a hidden element.
click_link 'Edit user', href: edit_users_path(@user)
# Note: above should also work with fill_in.
# Interacting with forms
fill_in 'First Name', with: 'John'
fill_in 'Password', with: 'Seekrit'
fill_in 'Description', with: 'Really Long Text…'
choose 'A Radio Button'
check 'A Checkbox'
uncheck 'A Checkbox'
attach_file 'Image', '/path/to/image.jpg'
select 'Option', from: 'Select Box'
# Scoping
within('//li[@id="employee"]') do
fill_in 'Name', with: 'Jimmy'
end
within('li#employee') do
fill_in 'Name', with: 'Jimmy'
end
within_fieldset('Employee') do
fill_in 'Name', with: 'Jimmy'
end
within_table('Employee') do
fill_in 'Name', with: 'Jimmy'
end
# Querying
page.has_xpath? '//table/tr'
page.has_css? 'table tr.foo'
page.has_content? 'foo'
page.should have_xpath '//table/tr'
page.should have_css 'table tr.foo'
page.should have_content 'foo'
page.should have_no_content 'foo'
find_field('First Name').value
find_link('Hello').visible?
find_button('Send').click
find('//table/tr').click
all('a').each { |a| a[:href] }
# Scripting
page.execute_script '$("body").empty()'
result = page.evaluate_script('4 + 4');
# Debugging
save_and_open_page
print page.html # Current state of the DOM as a string.
page.save_screenshot 'screenshot.png' # Some drivers support this.
Launchy.open 'screenshot.png' # Or open manually
# Asynchronous JavaScript
click_link 'foo'
click_link 'bar'
page.should have_content 'baz'
page.should_not have_xpath '//a'
page.should have_no_xpath '//a'
# XPath and CSS
within('ul li') { ... }
find('ul li').text
locate('input#name').value
Capybara.default_selector = :css
within('ul li') { ... }
find('ul li').text
# Headers
page.response_headers['Content-Type'].should eq 'application/pdf'
page.response_headers['Content-Disposition'].should eq "attachment; filename=#{@order.number}.pdf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment