Skip to content

Instantly share code, notes, and snippets.

@akampjes
Last active April 29, 2016 02:29
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 akampjes/1771103f818cc0ae041e to your computer and use it in GitHub Desktop.
Save akampjes/1771103f818cc0ae041e to your computer and use it in GitHub Desktop.
Mechanize Cheatsheet
# Cheatsheet for using mechanize
require 'mechanize'
# Initialize Mechanize Agent
agent = Mechanize.new
agent.get 'http://localhost:3000/'
# Click on a link displayed on the page
agent.page.link_with(text: "Sign in").click
# Input credentials to the first form on the page
agent.page.forms.first.tap do |f|
f['user[email]'] = 'user@example.com'
f['user[password]'] = 'password'
f.submit
end
# Agent remembers the scheme + host, so no need to supply it when navigating somewhere else
agent.get "/home"
# Search for elements on the page
if agent.page.body.inspect.include? 'your mum'
puts agent.page.body.inspect
puts '*found her!'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment