Skip to content

Instantly share code, notes, and snippets.

@bergerjac
Forked from zhengjia/capybara cheat sheet
Last active August 29, 2015 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bergerjac/71d01b15b376031c267f to your computer and use it in GitHub Desktop.
Save bergerjac/71d01b15b376031c267f to your computer and use it in GitHub Desktop.
capybara cheat sheet
# debugging
save_and_open_page
puts page.body
puts find("#id").native
puts first("#id .class element").native
# navigating
visit('/projects')
visit(post_comments_path(post))
# clicking links and buttons
click_on('Link Text') # click either a link or a button
click_on('Button Value')
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
# 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(:css, "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')
expect(page).to have_xpath('//table/tr')
expect(page).to have_css('table tr.foo')
expect(page).to have_content('foo')
expect(page).to have_no_content('foo')
find_field('First Name').value
find_link('Hello').visible?
find_button('Send').click
find('//table/tr').click
locate("//*[@id='overlay'").find("//h1").click
all('a').each { |a| a[:href] }
# scripting
result = page.evaluate_script('4 + 4');
# Asynchronous JavaScript
click_link('foo')
click_link('bar')
expect(page).to have_content('baz')
expect(page).to_not have_xpath('//a')
expect(page).to have_no_xpath('//a')
# XPath and CSS
within(:css, 'ul li') { ... }
find(:css, 'ul li').text
locate(:css, 'input#name').value
Capybara.default_selector = :css
within('ul li') { ... }
find('ul li').text
locate('input#name').value
# email
clear_emails
# <<code to trigger email>> visit some_path
open_email("user@example.com") # find an email sent to user@example.com and set to current_email
current_email.click_link 'change password'
expect(page).to have_content 'change your password'
expect(current_email).to have_content 'Hello there.'
# email debugging
current_email.save_and_open
puts current_email.body
.* # anything (or nothing), literally "any character (except a newline) 0 or more times"
.+ # at least one of anything
[0-9]* or d* # a series of digits (or nothing)
[0-9]+ or d+ # one or more digits
/"[^"]*"/ # something (or nothing) in double quotes
an? 'a' or 'an' (? makes 'n' optional)
When /^My listing (?:is|was) approved$/ # non-captured group
Given /^I have (d+) products?$/ |number_of_products|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment