Created
April 16, 2013 07:56
-
-
Save the-teacher/5394200 to your computer and use it in GitHub Desktop.
capybara cheat sheet (based on https://gist.github.com/zhengjia/428105)
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
https://gist.github.com/zhengjia/428105 | |
Class: Capybara::Node::Element | |
* [] - Retrieve the given attribute | |
* allow_reload! | |
* checked? - Whether or not the element is checked. | |
* click - Click the Element. | |
* disabled? - Whether or not the element is disabled. | |
* drag_to(node) - Drag the element to the given other element. | |
* hover - Hover on the Element. | |
* inspect | |
* native - The native element from the driver, this allows access to driver specific methods. | |
* path - An XPath expression describing where on the page the element can be found. | |
* reload | |
* select_option - Select this node if is an option element inside a select tag. | |
* selected? - Whether or not the element is selected. | |
* set(value) - Set the value of the form element to the given value. | |
* tag_name - The tag name of the element. | |
* text(type = nil) - Retrieve the text of the element. | |
* trigger(event) - Trigger any event on the current element, for example mouseover or focus events. | |
* unselect_option - Unselect this node if is an option element inside a multiple select tag. | |
* value - The value of the form element. | |
* visible? | |
=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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment