-
-
Save artemv/e35f501357a6ed526a1732bbab86e560 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)) | |
=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') | |
all('table tr.foo .btn.remove').first.click # click first thing found by selector | |
=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') | |
expect(page).to have_xpath('//table/tr') | |
expect(page).to have_xpath('//table/tr', count: 2, visible: false) # hidden elements | |
expect(page).to have_css('table tr.foo') | |
expect(page).to have_css('table td.foo', :text => /lala/) # text matcher with regex | |
expect(page).to have_css('table tr.foo', count: 2) | |
# hidden input, name starts with this and ends with that | |
expect(page).to have_css("input[name^='item[test_dates_attributes]'][name$='[date]']", count: 2, visible: false) | |
expect(find('input.rc-time-picker-input').value).to eq '13:25' | |
expect(page).to have_content('foo') | |
expect(page).to have_no_content('foo') | |
expect(page).to have_current_path(new_user_registration_path) # will wait for redirect | |
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') | |
expect(page).to have_content('baz') | |
expect(page).not_to have_xpath('//a') | |
expect(page).to 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment