Skip to content

Instantly share code, notes, and snippets.

@elfassy
Forked from zhengjia/capybara cheat sheet
Last active March 17, 2024 11:09
Show Gist options
  • Save elfassy/11399304 to your computer and use it in GitHub Desktop.
Save elfassy/11399304 to your computer and use it in GitHub Desktop.
#=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_on('Button Value')
find('form.foo .btn').click
#=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')
choose("radio_group_selector"), option: "Option 5"
check('A Checkbox')
uncheck('A Checkbox')
attach_file('Image', '/path/to/image.jpg')
select('Option', :from => 'Select Box')
unselect('Option', from: select_box)
find("#select_id").select("value")
#=scoping=
within(:xpath, "//li[@id='employee']") do
fill_in 'Name', :with => 'Jimmy'
end
within("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')
expect(page).to have_selector 'foobar'
find_field('First Name').value
find_link('Hello').visible? #false, finds only visible
find_button('Send').click
find('//table/tr').click
all('a').each { |a| a[:href] }
#=Find actions=
find("input.file").attach_file
find("input.checkbox").check
find("input.select").choose
find(".button").click_button
find(".link").click_link
find(".link").click_link_or_button
find(".link").click_on
find(".link").click
find("input.text").set('Jimmy') #instead of fill_in
find("input.select").select
find("input.checkbox").uncheck
find("input.select").unselect
find("input.select").unselect_option(option: "Option 5")
find("input.checkbox").checked?
find(".button").disabled?
find(".link").hover
find("input.select").selected?
find("input.text").value
find(".text").text
find(".link").visible?
#=Scripting=
result = page.evaluate_script('4 + 4')
periods = page.evaluate_script("$('#MainContent_dd').map(function() { return $(this).text() }).get()")
#=Debugging=
save_and_open_page
screenshot_and_open_image # with the capybara-screenshot gem
#=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')
using_wait_time 5 do
# assertions
end
@keppy
Copy link

keppy commented Apr 30, 2014

Nice and updated, thanks.

@peacetrader
Copy link

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

@timgent
Copy link

timgent commented Sep 28, 2014

Good stuff, really helpful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment