Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

capybara cheat sheet

View capybara cheat sheet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
=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

Awesome, thanks for this

Nice...bookmarked this

Useful. Thanks.

Thanks !

mull commented

Awesome, thank you.

dreki commented

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

thanks!

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

Thanks, here's bitcookie 8635

using_wait_time 5 do
  # assertions
end

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

This is great!

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

afred commented

+1

+1!

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

:thumbsup:

(y) thanks

+1 for updates :-)

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

thanks!

jalevin commented

nice!

Thanks!

  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

Thanks!

+1

HowHai commented

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!

Thanks!

^_^

This is awesome, thanks!

Life saver. Thx.

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

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

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

great !

it's really good and helpfull

cool thanks!

Thank you!

good job, thank you

thank you

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)

Thanks!

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

Thanks!

Ultimate

guih commented

Thanks! :+1:

Thanks!

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

Thanks!!

Comes in handy ALL the time. Thanks :)

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.