Skip to content

@zhengjia /capybara cheat sheet
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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

Awesome, thanks for this

@nickrivadeneira

Nice...bookmarked this

@s1mple

Useful. Thanks.

@vasconcelloslf

Thanks !

@mull

Awesome, thank you.

@dreki

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

@fguillen

thanks!

@leonardodarioperna

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

@borisreitman

Thanks, here's bitcookie 8635

@qoobaa
using_wait_time 5 do
  # assertions
end
@MilindS

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

@aisensiy

This is great!

@AlabamaMike

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

@afred

+1

@wiremine

+1!

@amoid125

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

@dsengupta

(y) thanks

@ZachBeta

+1 for updates :-)

@MissMathmagician

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

@bradleyd

thanks!

@jalevin

nice!

@wangzifeng

Thanks!

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

Thanks!

@HowHai

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

Thanks!

^_^

@tomreese

This is awesome, thanks!

@tochman

Life saver. Thx.

@Dorian

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

@jalcine

@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

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

great !

@dipak1112

it's really good and helpfull

@smokingcat

cool thanks!

@pete2786

Thank you!

@GraceCao

good job, thank you

@equivalent

thank you

@elfassy

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

Thanks!

@NourhanAttef

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

@juriewessels

Thanks!

@asharma-ror

Ultimate

@guih

Thanks! :+1:

@st0012

Thanks!

@dcharan

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

@dantesolis

Thanks!!

@cleeverz

Comes in handy ALL the time. Thanks :)

@lccezinha

Thanks man!

@ppj
ppj commented

Thanks :+1:

@wolftatsu

nice

@IdarV

Thank you for this

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.