Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
capybara cheat sheet
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
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')
=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

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): http://cl.ly/0q1y282S3O2j110h0O2u

fguillen commented Oct 3, 2012

thanks!

πŸ‘

@ghost

ghost commented Dec 22, 2012

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

Thanks, here's bitcookie 8635

qoobaa commented Jan 26, 2013

using_wait_time 5 do
  # assertions
end

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

+1

+1!

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

πŸ‘

thanks!

jalevin commented May 21, 2013

nice!

Thanks!

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

Thanks!

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!

Amazing!
BTW, there is no have_selector

Thank you, very useful!

zeroed commented Sep 6, 2013

Thanks!

^_^

This is awesome, thanks!

tochman commented Oct 7, 2013

Life saver. Thx.

Dorian commented Oct 24, 2013

Great, thanks, this is really useful: find('form.foo .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'
end

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

You might want to update your gist.

https://github.com/jnicklas/capybara#scoping

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')

do

find("#select_id").select("value")

cool!

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: https://gist.github.com/elfassy/11399304

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.

page.title
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.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

Thanks!

πŸ‘

πŸ‘

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

Thanks!

Ultimate

guih commented Mar 5, 2015

Thanks! πŸ‘

st0012 commented Mar 7, 2015

Thanks!

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.

Reference

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

Thanks!!

Comes in handy ALL the time. Thanks :)

fcarrega commented Apr 2, 2015

πŸ‘

Thanks man!

ppj commented May 6, 2015

Thanks πŸ‘

squanto commented May 8, 2015

πŸ˜„

πŸ‘

πŸ‘

πŸ‘

πŸ‘

πŸ‘

nice

IdarV commented Aug 10, 2015

Thank you for this

LNA commented Sep 4, 2015

Thank you for this πŸ‘ πŸ‘ πŸ‘

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! πŸ‘

kozyty commented Jan 7, 2016

thx! πŸ‘

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! πŸ‘ πŸ˜‰

EricA-G commented May 12, 2016

Good stuff

πŸ‘

Dorian commented Jun 9, 2016

@WaKeMaTTa: Thanks <3

bramj commented Jun 17, 2016

Been using this for years now without saying thanks... Thanks!

FottyM commented Jun 17, 2016

Nice

To debug save_and_open_screenshot is useful

erich commented Jun 22, 2016

πŸ‘

πŸ‘

I have used this resource so many times I just wanted to say thanks!

I really appreciate you putting this together.

πŸ‘

find("input[attribute='value']") where input is an input tag, and attribute is something like name I have found these command useful.

πŸ‘

Jbur43 commented Oct 30, 2016

Thanks!

pixelgoo commented Dec 7, 2016

Really helpful!

πŸ‘

pdxmike commented Jan 22, 2017

This is great. Thank you!

ckib16 commented Feb 24, 2017

Nice @hooverlunch, thanks!

Guys good day. Can you help me on how can i create a test report when i am using capybara-cucumber in my automation. What gem should i need to install and how should i invoke it. Thank you in advance.

πŸ‘

I am unable to hit "enter" key , any solution?

This is simple and great. Thanks!

I'm always coming back to this. Thanks for a great list!

Excellent! Thanks :)

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