Skip to content

Instantly share code, notes, and snippets.

@vajradog
Last active October 9, 2017 23:34
Show Gist options
  • Save vajradog/3c144005d07c7a6fece8 to your computer and use it in GitHub Desktop.
Save vajradog/3c144005d07c7a6fece8 to your computer and use it in GitHub Desktop.
capybara cheat sheet (capybara (2.4.4): May 2015)

Created this cheat-sheet for personal reference on May 17, 2015. For detailed documentation and updates you should visit https://github.com/jnicklas/capybara

###Navigating

visit('/projects') or visit '/projects'
visit(post_comments_path(post)) or 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') # clicks on either links or buttons click_on('Button Value')

###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#employee") do fill_in 'Name', :with => 'Jimmy' end

within(:xpath, "//li[@id='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

###Finding find_field('First Name').value find_link('Hello').visible? find_button('Send').click

find(:xpath, "//table/tr").click
find("#overlay").find("h1").click
all('a').each { |a| a[:href] }

find('#navigation').click_link('Home')
expect(find('#navigation')).to have_button('Sign out')

###Querying page.has_selector?('table tr') page.has_selector?(:xpath, '//table/tr')

page.has_xpath?('//table/tr')
page.has_css?('table tr.foo')
page.has_content?('foo')

expect(page).to have_selector('table tr')
expect(page).to have_selector(:xpath, '//table/tr')

expect(page).to have_xpath('//table/tr')
expect(page).to have_css('table tr.foo')
expect(page).to have_content('foo')

###Scripting page.execute_script("$('body').empty()") result = page.evaluate_script('4 + 4');

###Debugging save_and_open_page print page.html #retrieves current state of DOM as a string page.save_screenshot('screenshot.png')
save_and_open_screenshot

###Asynchronous JavaScript click_link('foo') click_link('bar') expect(page).to have_content('baz') !page.has_xpath?('a') page.has_no_xpath?('a') expect(page).not_to have_xpath('a') expect(page).to have_no_xpath('a') expect(find('#sidebar').find('h1')).to have_content('Something')

###Matching #exactness click_link("Password") # also matches "Password confirmation" Capybara.exact = true click_link("Password") # does not match "Password confirmation" click_link("Password", exact: false) # can be overridden

###XPath and CSS within(:xpath, '//ul/li') { ... } find(:xpath, '//ul/li').text find(:xpath, '//li[contains(.//a[@href = "#"]/text(), "foo")]').value

Capybara.default_selector = :xpath
find('//ul/li').text

For complete documentation please visit https://github.com/jnicklas/capybara

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment