Create a gist now

Instantly share code, notes, and snippets.

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
Emerson commented Mar 10, 2012

Awesome, thanks for this

@nickrivadeneira

Nice...bookmarked this

@s1mple
s1mple commented Apr 19, 2012

Useful. Thanks.

@lfv89
lfv89 commented May 2, 2012

Thanks !

@mull
mull commented May 25, 2012

Awesome, thank you.

@dreki
dreki commented Jun 12, 2012

Excellent. Produced a PDF for this (and gave you credit in the metadata): http://cl.ly/0q1y282S3O2j110h0O2u

@fguillen
fguillen commented Oct 3, 2012

thanks!

@makaroni4

πŸ‘

@ghost
ghost commented Dec 22, 2012

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

@borisreitman

Thanks, here's bitcookie 8635

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

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

@mobile-olx

πŸ‘

@aisensiy

This is great!

@AlabamaMike

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

@afred
afred commented Mar 15, 2013

+1

@wiremine

+1!

@amoid125

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

@morganick

πŸ‘

@dsengupta

(y) thanks

@ZachBeta

+1 for updates :-)

@MissMathmagician

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

@mariusbutuc

πŸ‘

@bradleyd

thanks!

@jalevin
jalevin commented May 21, 2013

nice!

@wangzifeng

Thanks!

@abotalov
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
zhublik commented Jun 6, 2013

Thanks!

@fahimbpatel

+1

@HowHai
HowHai commented Jun 20, 2013

Thank you!

@heymatty

Awesome, thanks!

@arnewauters

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

@DoctorRu

Really thks!

@dillonhafer

Thank you!

@FalloutX

Amazing!
BTW, there is no have_selector

@loed-idzinga

Thank you, very useful!

@zeroed
zeroed commented Sep 6, 2013

Thanks!

^_^

@tomreese

This is awesome, thanks!

@tochman
tochman commented Oct 7, 2013

Life saver. Thx.

@Dorian
Dorian commented Oct 24, 2013

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

@jalcine
jalcine commented Nov 2, 2013

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

expect(page).to have_selector 'foobar'
@zaczheng

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

@TomLewis

Very Handy, Thanks!

@passalini

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

@bf4
bf4 commented Dec 5, 2013

Anyone have an up-to-date fork?

@mangantj

If you can't find select via

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

do

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

cool!

@shekibobo

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

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

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
yawo commented Feb 14, 2014

great !

@dipak1112

it's really good and helpfull

@smokingcat

cool thanks!

@pete2786

Thank you!

@GraceCao

good job, thank you

@JokinAspiazu

πŸ‘

@equivalent

thank you

@elfassy
elfassy commented Apr 29, 2014

I've added all the comments above to mine: https://gist.github.com/elfassy/11399304

@Nitesh9952

Good work!!

@prashanth-sams

U saved my Day!!

@adamorabi

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

@purnimashanti

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

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

@purnimashanti

page.title
it will get the title of the page.

@purnimashanti

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"

@davidlesches

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

@jnrepo
jnrepo commented Nov 13, 2014

Thanks!

@johnwake

πŸ‘

@phuong3030

πŸ‘

@NourhanAttef

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

@juriewessels

Thanks!

@asharma-ror

Ultimate

@guih
guih commented Mar 5, 2015

Thanks! πŸ‘

@st0012
st0012 commented Mar 7, 2015

Thanks!

@dcharan
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

@dantesolis

Thanks!!

@cleeverz

Comes in handy ALL the time. Thanks :)

@fcarrega
fcarrega commented Apr 2, 2015

πŸ‘

@lccezinha

Thanks man!

@ppj
ppj commented May 6, 2015

Thanks πŸ‘

@squanto
squanto commented May 8, 2015

πŸ˜„

@mike-foucault

πŸ‘

@SamuraiT

πŸ‘

@antwonlee

πŸ‘

@AndreiRailean

πŸ‘

@marceloribeiro

πŸ‘

@wolftatsu

nice

@IdarV
IdarV commented Aug 10, 2015

Thank you for this

@LNA
LNA commented Sep 4, 2015

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

@ilyakrasnov

Thanks a lot!

@netes
netes commented Sep 10, 2015

πŸ‘

@mikesorae

it's a great help for me

@aliibrahim

πŸ‘

@OctavianRotari

πŸ‘

@shashwathi

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

@mib32
mib32 commented Nov 2, 2015

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

@Nyberg
Nyberg commented Nov 11, 2015

πŸ‘

@gunesmes

πŸ‘

@philiboua

great job ;)

@adamantnz

super handy, thanks! πŸ‘

@kozyty
kozyty commented Jan 7, 2016

thx! πŸ‘

@lucasklaassen

thx

@alexdesi

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

@babasbot

πŸ‘

@kishorsharma69

how to fill in text in masked input field?

@katlavan

πŸ‘

@manwinder123

πŸ‘

@OnlySteveH

Thanks for this - very handy! πŸ‘ πŸ˜‰

@EricA-G
EricA-G commented May 12, 2016

Good stuff

@jeffreyjurgajtis

πŸ‘

@Dorian
Dorian commented Jun 9, 2016

@WaKeMaTTa: Thanks <3

@bramj
bramj commented Jun 17, 2016

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

@FottyM
FottyM commented Jun 17, 2016

Nice

@maxigimenez

To debug save_and_open_screenshot is useful

@erich
erich commented Jun 22, 2016

πŸ‘

@kdabrowski

πŸ‘

@EvanTedesco

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

@Victorrent

I really appreciate you putting this together.

@catherine-jones

πŸ‘

@DougMaisells

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

@alexeymetelev

πŸ‘

@Jbur43
Jbur43 commented Oct 30, 2016

Thanks!

@pixelgoo
pixelgoo commented Dec 7, 2016

Really helpful!

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