Last active
March 8, 2017 11:26
-
-
Save virolea/fe73a83b7669a8dd0613da7395458558 to your computer and use it in GitHub Desktop.
Capybara Cheat Sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Navigating | |
visit('/projects') | |
visit(post_comments_path(post)) | |
expect(page).to have_current_path(post_comments_path(post)) | |
# Clicking links and buttons | |
click_link('id-of-link') | |
click_link('Link Text') | |
click_button('Save') | |
click_on('Link Text') # clicks on either links or buttons | |
click_on('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') | |
# Querying | |
page.has_selector?('table tr') | |
page.has_selector?(:xpath, '//table/tr') | |
page.has_xpath?('//table/tr') | |
page.has_css?('table tr.foo') | |
page.has_content?('foo') | |
# Finding | |
find_field('First Name').value | |
find_link('Hello', :visible => :all).visible? | |
find_button('Send').click | |
find(:xpath, "//table/tr").click | |
find("#overlay").find("h1").click | |
all('a').each { |a| a[:href] } | |
find('#navigation').click_link('Home') | |
expect(find('#navigation')).to have_button('Sign out') | |
# Scoping | |
within("li#employee") do | |
fill_in 'Name', :with => 'Jimmy' | |
end | |
within(:xpath, "//li[@id='employee']") do | |
fill_in 'Name', :with => 'Jimmy' | |
end | |
# Working with windows | |
facebook_window = window_opened_by do | |
click_button 'Like' | |
end | |
within_window facebook_window do | |
find('#login_email').set('a@example.com') | |
find('#login_password').set('qwerty') | |
click_button 'Submit' | |
end | |
# Scripting | |
page.execute_script("$('body').empty()") | |
result = page.evaluate_script('4 + 4'); | |
# Modals | |
accept_alert do | |
click_link('Show Alert') | |
end | |
dismiss_confirm do | |
click_link('Show Confirm') | |
end | |
accept_prompt(with: 'Linus Torvalds') do | |
click_link('Show Prompt About Linux') | |
end | |
message = accept_prompt(with: 'Linus Torvalds') do | |
click_link('Show Prompt About Linux') | |
end | |
expect(message).to eq('Who is the chief architect of Linux?') | |
# Debugging | |
save_and_open_page | |
print page.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment