Last active
April 29, 2016 02:29
-
-
Save akampjes/1771103f818cc0ae041e to your computer and use it in GitHub Desktop.
Mechanize Cheatsheet
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
# 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