Skip to content

Instantly share code, notes, and snippets.

@Rafe
Last active August 29, 2015 14:06
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 Rafe/3ff8cb41616c39483d8f to your computer and use it in GitHub Desktop.
Save Rafe/3ff8cb41616c39483d8f to your computer and use it in GitHub Desktop.
Reliable Capybara Cheetsheet
# Find the first matching element
# Bad:
first('.active').click
# Good:
find('.active').click
find('.active', match: :first).click
# Interact with all matching elements
# Bad:
all('.active').each(&:click)
# Good:
find('.active', match: :first)
all('.active').each(&:click)
# Directly interacting with javascript
find('.active')
execute_script('$(".active").focus')
# Checking a field's value
expect(page).to have_field("Username", with: 'Joe')
# Checking an element's attribute
expect(page).to have_css(".user[data-name=''Joe]")
# Looking for matching CSS
# Bad:
expect(has_css?('active')).to be_true
# Good:
expect(page).to have_css('.active')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment