Skip to content

Instantly share code, notes, and snippets.

@richardjortega
Forked from zhengjia/capybara cheat sheet
Last active December 26, 2015 10:59
Show Gist options
  • Save richardjortega/7141012 to your computer and use it in GitHub Desktop.
Save richardjortega/7141012 to your computer and use it in GitHub Desktop.

=Navigating= visit('/projects') visit(post_comments_path(post))

Finding stuff (using Capybara::Element unless noted) http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders

- (Capybara::Result) all([kind], locator, options)
    - Find all elements on the page matching the given selector and options.

find(*args) - Find an Element based on the given arguments.

find_button(locator, options = {})
    - Find a button on the page.

find_by_id(id, options = {})
    - Find a element on the page, given its id.

find_field(locator, options = {}) (also: #field_labeled)
    - Find a form field on the page.

find_link(locator, options = {})
    - Find a link on the page.

first([kind], locator, options)
    - Find the first element on the page matching the given selector and options, or nil if no element matches.

=Clicking links and buttons= click_link('id-of-link') click_link('Link Text') click_button('Save') find('form.foo .btn').click click_on('foo') # Link via id, text

=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(: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') 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 locate("//*[@id='overlay'").find("//h1").click all('a').each { |a| a[:href] }

=Scripting= result = page.evaluate_script('4 + 4');

=Debugging= save_and_open_page

=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(: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

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