public
Last active

capybara cheat sheet

  • Download Gist
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.

Awesome, thank you.

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

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.

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

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

+1 for updates :-)

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

  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

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

Amazing!
BTW, there is no have_selector

Thank you, very useful!

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

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

it's really good and helpfull

good job, thank you

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.