Skip to content

Instantly share code, notes, and snippets.

Embed
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

This comment has been minimized.

Copy link

Emerson commented Mar 10, 2012

Awesome, thanks for this

@nickrivadeneira

This comment has been minimized.

Copy link

nickrivadeneira commented Mar 20, 2012

Nice...bookmarked this

@s1mple

This comment has been minimized.

Copy link

s1mple commented Apr 19, 2012

Useful. Thanks.

@lfv89

This comment has been minimized.

Copy link

lfv89 commented May 2, 2012

Thanks !

@mull

This comment has been minimized.

Copy link

mull commented May 25, 2012

Awesome, thank you.

@dreki

This comment has been minimized.

Copy link

dreki commented Jun 12, 2012

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

@fguillen

This comment has been minimized.

Copy link

fguillen commented Oct 3, 2012

thanks!

@makaroni4

This comment has been minimized.

Copy link

makaroni4 commented Dec 2, 2012

πŸ‘

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 22, 2012

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

@borisreitman

This comment has been minimized.

Copy link

borisreitman commented Dec 26, 2012

Thanks, here's bitcookie 8635

@qoobaa

This comment has been minimized.

Copy link

qoobaa commented Jan 26, 2013

using_wait_time 5 do
  # assertions
end
@MilindS

This comment has been minimized.

Copy link

MilindS commented Feb 5, 2013

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

@mobile-olx

This comment has been minimized.

Copy link

mobile-olx commented Feb 8, 2013

πŸ‘

@aisensiy

This comment has been minimized.

Copy link

aisensiy commented Feb 22, 2013

This is great!

@AlabamaMike

This comment has been minimized.

Copy link

AlabamaMike commented Feb 27, 2013

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

@afred

This comment has been minimized.

Copy link

afred commented Mar 15, 2013

+1

@wiremine

This comment has been minimized.

Copy link

wiremine commented Mar 17, 2013

+1!

@amoid125

This comment has been minimized.

Copy link

amoid125 commented Mar 18, 2013

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

@morganick

This comment has been minimized.

Copy link

morganick commented Apr 3, 2013

πŸ‘

@dsengupta

This comment has been minimized.

Copy link

dsengupta commented Apr 12, 2013

(y) thanks

@ZachBeta

This comment has been minimized.

Copy link

ZachBeta commented Apr 12, 2013

+1 for updates :-)

@MissMathmagician

This comment has been minimized.

Copy link

MissMathmagician commented Apr 15, 2013

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

@mariusbutuc

This comment has been minimized.

Copy link

mariusbutuc commented Apr 24, 2013

πŸ‘

@bradleyd

This comment has been minimized.

Copy link

bradleyd commented Apr 30, 2013

thanks!

@jalevin

This comment has been minimized.

Copy link

jalevin commented May 21, 2013

nice!

@wangzifeng

This comment has been minimized.

Copy link

wangzifeng commented Jun 3, 2013

Thanks!

@abotalov

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

zhublik commented Jun 6, 2013

Thanks!

@fahimbpatel

This comment has been minimized.

Copy link

fahimbpatel commented Jun 15, 2013

+1

@HowHai

This comment has been minimized.

Copy link

HowHai commented Jun 20, 2013

Thank you!

@heymatty

This comment has been minimized.

Copy link

heymatty commented Jul 27, 2013

Awesome, thanks!

@arnewauters

This comment has been minimized.

Copy link

arnewauters commented Aug 5, 2013

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

@DoctorRu

This comment has been minimized.

Copy link

DoctorRu commented Aug 13, 2013

Really thks!

@dillonhafer

This comment has been minimized.

Copy link

dillonhafer commented Aug 22, 2013

Thank you!

@FalloutX

This comment has been minimized.

Copy link

FalloutX commented Aug 26, 2013

Amazing!
BTW, there is no have_selector

@loed-idzinga

This comment has been minimized.

Copy link

loed-idzinga commented Aug 27, 2013

Thank you, very useful!

@zeroed

This comment has been minimized.

Copy link

zeroed commented Sep 6, 2013

Thanks!

^_^

@tomreese

This comment has been minimized.

Copy link

tomreese commented Sep 19, 2013

This is awesome, thanks!

@tochman

This comment has been minimized.

Copy link

tochman commented Oct 7, 2013

Life saver. Thx.

@Dorian

This comment has been minimized.

Copy link

Dorian commented Oct 24, 2013

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

@jalcine

This comment has been minimized.

Copy link

jalcine commented Nov 2, 2013

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

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

This comment has been minimized.

Copy link

zaczheng commented Nov 12, 2013

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

This comment has been minimized.

Copy link

TomLewis commented Nov 15, 2013

Very Handy, Thanks!

@passalini

This comment has been minimized.

Copy link

passalini commented Nov 21, 2013

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

@bf4

This comment has been minimized.

Copy link

bf4 commented Dec 5, 2013

Anyone have an up-to-date fork?

@mangantj

This comment has been minimized.

Copy link

mangantj commented Dec 13, 2013

If you can't find select via

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

do

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

This comment has been minimized.

Copy link

tomaszgiba commented Jan 3, 2014

cool!

@shekibobo

This comment has been minimized.

Copy link

shekibobo commented Jan 3, 2014

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

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

This comment has been minimized.

Copy link

cirosantilli commented Feb 8, 2014

@s01ipsist

This comment has been minimized.

Copy link

s01ipsist commented Feb 11, 2014

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

This comment has been minimized.

Copy link

yawo commented Feb 14, 2014

great !

@dipak1112

This comment has been minimized.

Copy link

dipak1112 commented Feb 24, 2014

it's really good and helpfull

@smokingcat

This comment has been minimized.

Copy link

smokingcat commented Feb 27, 2014

cool thanks!

@pete2786

This comment has been minimized.

Copy link

pete2786 commented Mar 19, 2014

Thank you!

@GraceCao

This comment has been minimized.

Copy link

GraceCao commented Mar 27, 2014

good job, thank you

@JokinAspiazu

This comment has been minimized.

Copy link

JokinAspiazu commented Apr 18, 2014

πŸ‘

@equivalent

This comment has been minimized.

Copy link

equivalent commented Apr 24, 2014

thank you

@elfassy

This comment has been minimized.

Copy link

elfassy commented Apr 29, 2014

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

@Nitesh9952

This comment has been minimized.

Copy link

Nitesh9952 commented May 22, 2014

Good work!!

@prashanth-sams

This comment has been minimized.

Copy link

prashanth-sams commented May 30, 2014

U saved my Day!!

@adamorabi

This comment has been minimized.

Copy link

adamorabi commented Jun 22, 2014

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

This comment has been minimized.

Copy link

purnimashanti commented Jul 2, 2014

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

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

@purnimashanti

This comment has been minimized.

Copy link

purnimashanti commented Jul 9, 2014

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

@purnimashanti

This comment has been minimized.

Copy link

purnimashanti commented Jul 17, 2014

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

This comment has been minimized.

Copy link

davidlesches commented Aug 11, 2014

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

@jnrepo

This comment has been minimized.

Copy link

jnrepo commented Nov 13, 2014

Thanks!

@johnwake

This comment has been minimized.

Copy link

johnwake commented Dec 16, 2014

πŸ‘

@phuong3030

This comment has been minimized.

Copy link

phuong3030 commented Jan 6, 2015

πŸ‘

@NourhanAttef

This comment has been minimized.

Copy link

NourhanAttef commented Jan 15, 2015

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

@juriewessels

This comment has been minimized.

Copy link

juriewessels commented Feb 11, 2015

Thanks!

@asharma-ror

This comment has been minimized.

Copy link

asharma-ror commented Feb 19, 2015

Ultimate

@guih

This comment has been minimized.

Copy link

guih commented Mar 5, 2015

Thanks! πŸ‘

@st0012

This comment has been minimized.

Copy link

st0012 commented Mar 7, 2015

Thanks!

@dcharan

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

dantesolis commented Mar 28, 2015

Thanks!!

@cleeverz

This comment has been minimized.

Copy link

cleeverz commented Mar 31, 2015

Comes in handy ALL the time. Thanks :)

@fcarrega

This comment has been minimized.

Copy link

fcarrega commented Apr 2, 2015

πŸ‘

@lccezinha

This comment has been minimized.

Copy link

lccezinha commented Apr 30, 2015

Thanks man!

@ppj

This comment has been minimized.

Copy link

ppj commented May 6, 2015

Thanks πŸ‘

@squanto

This comment has been minimized.

Copy link

squanto commented May 8, 2015

πŸ˜„

@mike-foucault

This comment has been minimized.

Copy link

mike-foucault commented May 9, 2015

πŸ‘

@SamuraiT

This comment has been minimized.

Copy link

SamuraiT commented May 12, 2015

πŸ‘

@antwonlee

This comment has been minimized.

Copy link

antwonlee commented May 31, 2015

πŸ‘

@AndreiRailean

This comment has been minimized.

Copy link

AndreiRailean commented Jun 19, 2015

πŸ‘

@marceloribeiro

This comment has been minimized.

Copy link

marceloribeiro commented Jul 2, 2015

πŸ‘

@wolftatsu

This comment has been minimized.

Copy link

wolftatsu commented Jul 29, 2015

nice

@IdarV

This comment has been minimized.

Copy link

IdarV commented Aug 10, 2015

Thank you for this

@LNA

This comment has been minimized.

Copy link

LNA commented Sep 4, 2015

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

@ilyakrasnov

This comment has been minimized.

Copy link

ilyakrasnov commented Sep 10, 2015

Thanks a lot!

@netes

This comment has been minimized.

Copy link

netes commented Sep 10, 2015

πŸ‘

@mikesorae

This comment has been minimized.

Copy link

mikesorae commented Oct 1, 2015

it's a great help for me

@aliibrahim

This comment has been minimized.

Copy link

aliibrahim commented Oct 16, 2015

πŸ‘

@OctavianRotari

This comment has been minimized.

Copy link

OctavianRotari commented Oct 27, 2015

πŸ‘

@shashwathi

This comment has been minimized.

Copy link

shashwathi commented Oct 29, 2015

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

@mib32

This comment has been minimized.

Copy link

mib32 commented Nov 2, 2015

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

@Nyberg

This comment has been minimized.

Copy link

Nyberg commented Nov 11, 2015

πŸ‘

@gunesmes

This comment has been minimized.

Copy link

gunesmes commented Nov 17, 2015

πŸ‘

@philiboua

This comment has been minimized.

Copy link

philiboua commented Nov 26, 2015

great job ;)

@adamantnz

This comment has been minimized.

Copy link

adamantnz commented Dec 29, 2015

super handy, thanks! πŸ‘

@kozyty

This comment has been minimized.

Copy link

kozyty commented Jan 7, 2016

thx! πŸ‘

@lucasklaassen

This comment has been minimized.

Copy link

lucasklaassen commented Jan 13, 2016

thx

@alexdesi

This comment has been minimized.

Copy link

alexdesi commented Jan 21, 2016

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

This comment has been minimized.

Copy link

babasbot commented Jan 26, 2016

πŸ‘

@kishorsharma69

This comment has been minimized.

Copy link

kishorsharma69 commented Feb 18, 2016

how to fill in text in masked input field?

@katlavan

This comment has been minimized.

Copy link

katlavan commented Feb 18, 2016

πŸ‘

@manwinder123

This comment has been minimized.

Copy link

manwinder123 commented Mar 30, 2016

πŸ‘

@OnlySteveH

This comment has been minimized.

Copy link

OnlySteveH commented Apr 8, 2016

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

@iqbalhasnan

This comment has been minimized.

Copy link

iqbalhasnan commented Apr 28, 2016

:shipit:

@EricA-G

This comment has been minimized.

Copy link

EricA-G commented May 12, 2016

Good stuff

@jeffreyjurgajtis

This comment has been minimized.

Copy link

jeffreyjurgajtis commented May 16, 2016

πŸ‘

@Dorian

This comment has been minimized.

Copy link

Dorian commented Jun 9, 2016

@WaKeMaTTa: Thanks <3

@bramj

This comment has been minimized.

Copy link

bramj commented Jun 17, 2016

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

@FottyM

This comment has been minimized.

Copy link

FottyM commented Jun 17, 2016

Nice

@maxigimenez

This comment has been minimized.

Copy link

maxigimenez commented Jun 20, 2016

To debug save_and_open_screenshot is useful

@erich

This comment has been minimized.

Copy link

erich commented Jun 22, 2016

πŸ‘

@aureliosaraiva

This comment has been minimized.

Copy link

aureliosaraiva commented Jun 28, 2016

o/

@kdabrowski

This comment has been minimized.

Copy link

kdabrowski commented Jul 14, 2016

πŸ‘

@EvanTedesco

This comment has been minimized.

Copy link

EvanTedesco commented Jul 14, 2016

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

@Victorrent

This comment has been minimized.

Copy link

Victorrent commented Aug 11, 2016

I really appreciate you putting this together.

@catherine-jones

This comment has been minimized.

Copy link

catherine-jones commented Sep 5, 2016

πŸ‘

@DougMaisells

This comment has been minimized.

Copy link

DougMaisells commented Sep 12, 2016

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

@alexeymetelev

This comment has been minimized.

Copy link

alexeymetelev commented Oct 21, 2016

πŸ‘

@Jbur43

This comment has been minimized.

Copy link

Jbur43 commented Oct 30, 2016

Thanks!

@pixelgoo

This comment has been minimized.

Copy link

pixelgoo commented Dec 7, 2016

Really helpful!

@ff-dmiller

This comment has been minimized.

Copy link

ff-dmiller commented Jan 9, 2017

πŸ‘

@pdxmike

This comment has been minimized.

Copy link

pdxmike commented Jan 22, 2017

This is great. Thank you!

@smoyth

This comment has been minimized.

Copy link

smoyth commented Feb 17, 2017

@ckib16

This comment has been minimized.

Copy link

ckib16 commented Feb 24, 2017

Nice @hooverlunch, thanks!

@jepjep70

This comment has been minimized.

Copy link

jepjep70 commented Feb 25, 2017

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.

@cooljl31

This comment has been minimized.

Copy link

cooljl31 commented Mar 20, 2017

πŸ‘

@pmaderamitlasf1

This comment has been minimized.

Copy link

pmaderamitlasf1 commented May 12, 2017

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

@gustavo-freitas

This comment has been minimized.

Copy link

gustavo-freitas commented May 30, 2017

This is simple and great. Thanks!

@jumbosushi

This comment has been minimized.

Copy link

jumbosushi commented Jul 12, 2017

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

@ElizabethCobian

This comment has been minimized.

Copy link

ElizabethCobian commented Aug 24, 2017

Excellent! Thanks :)

@itumoraes

This comment has been minimized.

Copy link

itumoraes commented Sep 20, 2017

Awesome!

@Arunk1390

This comment has been minimized.

Copy link

Arunk1390 commented Oct 23, 2017

Thanks for this great gist πŸ‘

@Pomeha

This comment has been minimized.

Copy link

Pomeha commented Nov 27, 2017

Great! πŸ‘

@AndrewFReda

This comment has been minimized.

Copy link

AndrewFReda commented Dec 18, 2017

Awesome, thanks! (FYI, this gist was used by LaunchAcademy here)

@vishakhapanat

This comment has been minimized.

Copy link

vishakhapanat commented Jan 7, 2018

When i use find(<name_of_object>).click, I get following error
Unable to find css "" (Capybara::ElementNotFound)
Can someone please help?

@leylaKapi

This comment has been minimized.

Copy link

leylaKapi commented Mar 1, 2018

@vishakhapanat Did you find a solution for your error?

@igbanam

This comment has been minimized.

Copy link

igbanam commented Mar 14, 2018

To note, things like have_css, and have_xpath also have some optional arguments which help better querying. For instance have_css(".widget", maximum: 2)

@Pomeha

This comment has been minimized.

Copy link

Pomeha commented May 23, 2018

πŸ‘ πŸ‘ πŸ‘

@singh08avinash

This comment has been minimized.

Copy link

singh08avinash commented Jun 4, 2018

Hiii
I want to use xpath with select method in cucumber rails so how can i use xpath with select method?

@aashishbq

This comment has been minimized.

Copy link

aashishbq commented Jul 11, 2018

How do you wait for page to load in webrat?

@srghma

This comment has been minimized.

Copy link

srghma commented Jul 13, 2018

To match that select with label 'Country' have an option with text 'France' selected

expect(page).to have_select('Country', selected: 'France')
@philihp

This comment has been minimized.

Copy link

philihp commented Aug 18, 2018

πŸ‘

@glabrego

This comment has been minimized.

Copy link

glabrego commented Feb 27, 2019

TY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.