Capybara DSL Cheat Sheet

View All Cheat Sheets

visit('/relative-url')  
visit('http://absolute-url.com')  
visit(rails_path_helper_path(@object))  

Clicking Links and Buttons

click_link('id-of-link') #NOTE: do not include "#" before the element id  
click_link('Click me')  
click_button('Update')  
click_link_or_button('link/button text/id/value') #Match a link or a button  

Interacting with Forms Elements

fill_in('Name/user_name/user[name]', with: 'Joe Woodward') # label/id/name  
choose('I am a robot/user-type_robot/user[type][robot]') # label/id/name  
check('Accept/user_accept_terms/user[accept_terms]') # label/id/name  
uncheck('Accept/user_accept_terms/user[accept_terms]') # label/id/name  
attach_file('Rails asset or /public image', '/path/to/image.jpg')  
select('Definitely a 10', from: 'How do you rate my driving/driver_rating/driver[rating]') # label/id/name  

Scoping

within("//li[@id='contact']") do  
  fill_in('Name', with: 'Joe Woodward')
end  
within(:css, "li#bestest") do  
  fill_in 'Name', with: 'Joe Woodward'
end  
within_fieldset('Who is the man?') do  
  fill_in 'Name', with: 'Joe Woodward'
end  
within_table('All time records') do  
  fill_in 'Score', with: 'over 9000'
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  

View All Cheat Sheets