Skip to content

Instantly share code, notes, and snippets.

@stereodenis
Forked from pmeinhardt/capybara-cheatsheet.md
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stereodenis/70227f2bee9596190531 to your computer and use it in GitHub Desktop.
Save stereodenis/70227f2bee9596190531 to your computer and use it in GitHub Desktop.

Capybara Cheat Sheet

View the original gist: https://gist.github.com/428105

Navigating

visit "/projects"
visit post_comments_path(post)

Clicking Links and Buttons

click_link "link-id"
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.title
page.has_title?
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")
page.execute_script "$('#search-input').parents('form').submit()"

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

Sessions

Capybara.current_session.reset! # reset the session, removing all cookies
Capybara.reset_sessions!

Optional Driver Features

node.drag_to(element)
page.within_frame("frame-id") { ... }
page.within_window("window-handle") { ... }
page.status_code # returns the HTTP status code as an int
page.response_headers # returns a hash of response headers
page.save_screenshot("tmp/capybara/page.png", full: true)

Poltergeist-specific

View poltergeist readme

# window switching
page.driver.window_handles

# poltergeist sets the window size to 1024x768 by default
page.driver.resize(width, height)

# scroll to a specific point if the document is larger than the window
page.driver.scroll_to(left, top)

# sometimes its desirable to click a very specific area of the screen
page.driver.click(x, y)

# render a base64-encoded screenshot in-memory
page.driver.render_base64
page.driver.render_base64(:jpeg)
page.driver.render_base64(:png, full: true)

# setting request headers
page.driver.headers = { "User-Agent" => "Poltergeist" }
page.driver.add_header("Referer", "http://example.com", permanent: false)
page.driver.add_headers("Referer" => "https://example.com")

# inspect network traffic
page.driver.network_traffic

# manipulating cookies
page.driver.cookies
page.driver.set_cookie(name, value, options = {})
page.driver.remove_cookie(name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment