Benito Serna
Ruby/Rails, TDD, Software...

Capybara cheatsheet

Are you working with capybara?… Are you constantly searching for capybara helpers?

Here is a small reference with some of the most used methods/helpers.

# Navigate to a particular path
visit blogs_path
visit "/blog"

# Click on an anchor tag, button, or input with type submit
click_on "Save Project"
click_on "New Project"

# Click an anchor tag
click_link 'New Project'

# Click a button, or input with type submit
click_button 'Save'

# Find and click a Capybara::Element
find("a.title").click

Form interaction

# Fill input fields (text, textarea, number, phone, etc...)
fill_in 'Title', with: 'I love Rails!'
fill_in 'post[title]', with: 'I love Rails!'

# Checkbox
check 'I accept the terms and conditions'
uncheck 'I accept the terms and conditions'

# Radio button
choose 'Female'

# Select option from select tag
select 'MA', from: 'State'

# File input
attach_file Rails.root.join('spec/fixture/some_file.png')

# Click a button, or input with type submit
click_button 'Save'

# Accept a confirm dialog
accept_confirm do
  click_on 'Destroy'
end

Page interaction and scoping

# Returns a single Capybara::Node::Element instance from the page.
# It will wait for an element to appear on the page.
# If the element does not appear it will raise an error.
page.find('.todos li:first-child')

# Returns an array of Capybara::Node::Element instances from the page.
page.all('.todos li:nth-of-type(odd)')

# Within will scope interaction to within a particular selector.
within('footer') do
  expect(page).to have_content('Copyright')
end

# Is the selector present on the page?
page.has_css?('nav[data-role="user-menu"] li', text: 'Profile')

# Is the content present on the page?
page.has_content?('Sign in')

Matchers

# Is the content present on the page?
expect(page).to have_content("Some Content")
expect(page).to have_no_content("Some Content")

# Is the selector present on the page?
expect(page).to have_css("input#post_title")
expect(page).to have_css("input#post_title", value: "Capybara cheatsheet")

# Is the selector present X times on the page?
expect(page).to have_css("input", count: 3)
expect(page).to have_css("input", maximum: 3)
expect(page).to have_css("input", minimum: 3)
expect(page).to have_css("input", between: 1..3)

# Is the selector with the right text on the page?
expect(page).to have_css("p a", text: "hello")
expect(page).to have_css("p a", text: /[hH]ello(.+)/i)

# Is the field present?
expect(page).to have_field("FirstName")
expect(page).to have_field("FirstName", value: "Rambo")
expect(page).to have_field("FirstName", with: "Rambo")
expect(page).to have_field("FirstName", disabled: true)

# Is the link present?
expect(page).to have_link("Foo")
expect(page).to have_link("Foo", href: "example.com")
expect(page).to have_no_link("Foo", href: "example.com")

Node interaction

# Trigger a click on a Capybara::Element
find("a.title").click

# Trigger allow triggering a custom event.
find("a.title").trigger("focus")

# Is the element visible?
find(".navigation").visible?

Debugging

# Save the current page and attempt to open the HTML
# in the default web browser.
save_and_open_page

References

This cheatsheet is just a mix of other very usefull cheatsheets and other references.