Skip to content
Create a gist now

Instantly share code, notes, and snippets.

capybara cheat sheet
=Clicking links and buttons=
click_link('Link Text')
click('Link Text') # Click either a link or a button
click('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')
within("//li[@id='employee']") do
fill_in 'Name', :with => 'Jimmy'
within(:css, "li#employee") do
fill_in 'Name', :with => 'Jimmy'
within_fieldset('Employee') do
fill_in 'Name', :with => 'Jimmy'
within_table('Employee') do
fill_in 'Name', :with => 'Jimmy'
page.should have_xpath('//table/tr')
page.should have_css('table')
page.should have_content('foo')
page.should have_no_content('foo')
find_field('First Name').value
all('a').each { |a| a[:href] }
result = page.evaluate_script('4 + 4');
=Asynchronous JavaScript=
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
Emerson commented Mar 10, 2012

Awesome, thanks for this


Nice...bookmarked this

s1mple commented Apr 19, 2012

Useful. Thanks.

lfv89 commented May 2, 2012

Thanks !

mull commented May 25, 2012

Awesome, thank you.

dreki commented Jun 12, 2012

Excellent. Produced a PDF for this (and gave you credit in the metadata):

fguillen commented Oct 3, 2012



click('Button Value') > click_on('Button Value')


Thanks, here's bitcookie 8635

qoobaa commented Jan 26, 2013
using_wait_time 5 do
  # assertions
MilindS commented Feb 5, 2013

Thanks....its a quick reference i was looking for.


This is great!


Just what I was looking for ... many thanks!

afred commented Mar 15, 2013





is there any command for refresh button and back button on browser ????


(y) thanks


+1 for updates :-)


thank you so much!!you made my life easier <3 <3



jalevin commented May 21, 2013




abotalov commented Jun 4, 2013
  1. Correct method name is click_on, not click

  2. default_selector in Capybara 2 is :css so examples in section scoping should be updated.

  3. Capybara 2.1 by default finds only visible elements so find_link('Hello').visible? doesn't really make sense

  4. There's no such method as locate in Capybara

  5. all('a').each { |a| a[:href] } is inefficient if page contains a lot of links and driver is quiet slow (like Selenium) as Capybara will make a query to driver for each invocation of #[]. Instead I advice to get array of elements' attributes using execute_script like it's shown here

zhublik commented Jun 6, 2013




HowHai commented Jun 20, 2013

Thank you!


Awesome, thanks!


@zhengjia I believe click method has changed to click_on . Thanks for the sheet


Really thks!


Thank you!


BTW, there is no have_selector


Thank you, very useful!

zeroed commented Sep 6, 2013




This is awesome, thanks!

tochman commented Oct 7, 2013

Life saver. Thx.

Dorian commented Oct 24, 2013

Great, thanks, this is really useful: find(' .btn').click

jalcine commented Nov 2, 2013

@FalloutX have_selector works when you use 'expect' syntax:

expect(page).to have_selector 'foobar'

within now works differently:

within("li#employee") do
fill_in 'Name', :with => 'Jimmy'

within(:xpath, "//li[@id='employee']") do
fill_in 'Name', :with => 'Jimmy'

You might want to update your gist.


Very Handy, Thanks!


Hi, it is very useful. Just for "add value", in multiple select exists unselect(Option, from: select_box)

bf4 commented Dec 5, 2013

Anyone have an up-to-date fork?


If you can't find select via

select('Option', :from => 'Select Box')





New ability to choose option from a specific radio group (as of 2.2.0):

choose("radio_group_selector", option: "Option 5")

When debugging, I have found
page.driver.render 'tmp/page.png'
to be particularly useful to give you an idea of what Poltergeist is looking at on screen

yawo commented Feb 14, 2014

great !


it's really good and helpfull


cool thanks!


Thank you!


good job, thank you


thank you

elfassy commented Apr 29, 2014

I've added all the comments above to mine:


Good work!!


U saved my Day!!


I have a 2 radio buttons with the same id and label, only different values, (true, false)....anything I can do to choose false?


select "United States", from: "Country", :match => :first

You can use this if you want to select the first one on the dropdown list.


it will get the title of the page.


Since the version 2.1.0 of capybara there are methods on the session to deal with the title.
page.has_title? "my title"
page.has_no_title? "my not found title"


I'm finding visibility testing to be easier, with:
expect(page).to have_selector('#selector_id', visible: false)

jnrepo commented Nov 13, 2014



I have that error undefined method `visit' for #Object:0x007f9355878f08 (NoMethodError)





guih commented Mar 5, 2015

Thanks! :+1:

st0012 commented Mar 7, 2015


dcharan commented Mar 16, 2015

Capybara automatically waits for asynchronous operations to complete. When you try to find an element that isn't on the page, it waits and retries until it is there, or a timeout duration elapses. The wait time is defined at Capybara.default_wait_time

Here are the methods that waits:

  • find(selector), find_field, find_link
  • within(selector)(scoping) *has_selector?/has_no_selector? & assertions
  • form & link actions
    • click_link/button
    • fill_in
    • check/uncheck, select, choose

Here are the methods that doesn't wait:

  • visit
  • current_path
  • all (selector)
  • first(selector)
  • execute_script
  • simple accessors: text, value, title, etc.


Note that Capybara.default_wait_time has been changed to Capybara.default_max_wait_time




Comes in handy ALL the time. Thanks :)

fcarrega commented Apr 2, 2015



Thanks man!

ppj commented May 6, 2015

Thanks :+1:

squanto commented May 8, 2015




IdarV commented Aug 10, 2015

Thank you for this

LNA commented Sep 4, 2015

Thank you for this :+1: :+1: :+1:


Thanks a lot!

netes commented Sep 10, 2015



it's a great help for me


Is the "fill_in" function taking in the ID of a textfield or the label associated with the textfield?

mib32 commented Nov 2, 2015

@shashwathi You can feed both of them, it handles that automatically

Nyberg commented Nov 11, 2015



great job ;)


super handy, thanks! :+1:

kozyty commented Jan 7, 2016

thx! :+1:




I did find on this doc a way to find a text (and then click on it),
after googling a bit found this:
find('.clickable-text', :text => 'Click me').click
hope it can help someone


how to fill in text in masked input field?


Thanks for this - very handy! :+1: :wink:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.