Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Capybara cheatsheet

Capybara Actions

# Anchor
click_link 'Save'

# Button
click_button 'awesome'

# Both above
click_link_or_button 'Save'

# Text (area) field
fill_in 'Name', with: 'Content'

# Checkbox
check 'Content'
uncheck 'Content'

# Radio button
choose 'Content'

# Select option from select tag
select 'Option', from: 'Label'

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

Capybara Finders

page.all(:xpath, '//a')

page.first(:xpath, '//a')


page.find(:xpath, "//input[@id='form_pets_dog']")['checked']
# => true

page.find(:css, '#with_focus_event').trigger(:focus)


# => 'blah'

# => 'a'

# finds invisible elements when false
page.find_by_id("hidden_via_ancestor", visible: false)

page.find_button('What an Awesome')[:value]
# => 'awesome'

# => 'labore'

# => '/some_uri'

Note: find will wait for an element to appear on the page, as explained in the Ajax section. If the element does not appear it will raise an error.

Note: In XPath the expression // means something very specific, and it might not be what you think. Contrary to common belief, // means "anywhere in the document" not "anywhere in the current context".

Capybara Scoped Finder within

within(search_form) do
  fill_in 'Name', with: 'iOS 7'
  click_button 'Search'

def search_form

within_fieldset("villain_fieldset") do
  # ...

within_table("some_table") do
  # ...

# Execute the given block within the given iframe using given frame name or index.
within_frame('some_frame') do


# You need to install launchy gem.

Capybara Common



# Execute the given script, not returning a result. This is useful for scripts that return
# complex objects, such as jQuery statements. +execute_script+ should be used over
# +evaluate_script+ whenever possible.
page.execute_script("$('#change').text('Funky Doodle')")

# Evaluate the given JavaScript and return the result. Be careful when using this with
# scripts that return complex objects, such as jQuery statements. +execute_script+ might
# be a better alternative.
# => 4

using_wait_time 6 do
  # ... Changed Capybara.default_wait_time in this block scope.

Capybara Matchers

expect(page).to have_content("Some Content")
expect(page).to have_no_content("Some Content")

# True if there is a anchor tag with text matching regex
expect(page).to have_xpath("//a")
expect(page).to have_xpath("//a",:href => "")
expect(page).to have_xpath("//a[@href => '']")
expect(page).to have_xpath("//a[contains(.,'some string')]")
expect(page).to have_xpath("//p//a", :text => /re[dab]i/i, :count => 1)

 # can take both xpath and css as input and can take arguments similar to both have_css and have_xpath
 expect(page).to have_selector(:xpath, "//p/h1")
 expect(page).to have_selector(:css, "p a#post_edit_path")

 expect(page).to have_css("input#post_title")
 expect(page).to have_css("input#post_title", :value => "Capybara cheatsheet")

 # True if there are 3 input tags in response
 expect(page).to have_css("input", :count => 3)

 # True if there or fewer or equal to 3 input tags
 expect(page).to have_css("input", :maximum => 3)

 # True if there are minimum of 3 input tags
 expect(page).to have_css("input", :minimum => 3)

 # True if there 1 to 3 input tags
 expect(page).to have_css("input", :between => 1..3)

 # True if there is a anchor tag with text hello
 expect(page).to have_css("p a", :text => "hello")
 expect(page).to have_css("p a", :text => /[hH]ello(.+)/i)

# For making capybara to take css as default selector
Capybara.default_selector = :css

# checks for the presence of the input tag
expect(page).to have_selector("input")

# checks for input tag with value
expect(page).to have_selector("input", :value =>"Post Title")

expect(page).to have_no_selector("input")

# For making capybara to take css as default selector
Capybara.default_selector = :xpath
# checks for the presence of the input tag
expect(page).to have_selector("//input")

# checks for input tag with value
expect(page).to have_selector("//input", :value =>"Post Title")

# checks for presence of a input field named FirstName in a form
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_link("Foo")
expect(page).to have_link("Foo", :href=>"")
expect(page).to have_no_link("Foo", :href=>"")
Copy link

samkay123 commented Apr 27, 2017

expect(min_warning).to eq(min_warning) the various warnings are passed in the scenario outline is this best practice of anyone know of any other way to check warnings using scenario outline?

Copy link

pardhiva16 commented Nov 18, 2017

Thank you for providing this commands for Capybara Ruby Cucumber Automation Framework

Copy link

mockdeep commented Dec 26, 2017

I don't think the value: key is valid here. I get the error:

        invalid keys :value, should be one of :count, :minimum, :maximum, :between, :text, :id, :class, :visible, :exact, :exact_text, :match, :wait, :filter_set

Instead I need to use:

expect(page).to have_selector("input[value='Post Title']")

Copy link

mveer99 commented Dec 29, 2017

@mockdeep you can use it like
expect(page).to have_selector('input', text: 'Post Title')

Copy link

Circuit8 commented Jan 26, 2018

Thanks very nice 👍

Copy link

SofiaSousa commented Feb 14, 2018

Thanks for your share!

Copy link

minghz commented Apr 26, 2018

Copy link

rafael-acerqueira commented May 13, 2018

Thanks for sharing !

Copy link

hirokihokari commented Jun 7, 2018

why do you guys think have_css matcher is named the way it is? I mean, it sure deals with css identifiers, but have_css gives(at least to me) the impression that the page has certain stylesheet loading.

Copy link

Kharouk commented Sep 24, 2018

Thanks for this!

Copy link

FarooqSyed0 commented Oct 17, 2018

Thank you! :)

Copy link

gsag commented Oct 24, 2018

Thanks! :) 👍

Copy link

tuffs commented Mar 15, 2019

A+ Very useful!

Copy link

yshmarov commented Jun 10, 2021


Copy link

ourmaninamsterdam commented Jul 22, 2021

Was looking for Capybara help and look who I found 👍

Copy link

Bartuz commented Aug 22, 2022

wow, keep coming back to it after 5 years. Thank you

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