Skip to content

Instantly share code, notes, and snippets.

@Najaf
Last active October 15, 2019 11:17
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Najaf/7309910 to your computer and use it in GitHub Desktop.
Save Najaf/7309910 to your computer and use it in GitHub Desktop.
Mechanize Cheat Sheet, take a look at the real documentation here: http://mechanize.rubyforge.org/
# Initialize Mechanize Agent
agent = Mechanize.new
# Visit a web page
agent.get 'http://localhost:3000/'
# get the url of the current page
agent.page.uri #=> http://localhost:3000
# agent remembers the scheme + host, so no need to supply it when navigating somewhere else
agent.get '/whatever'
# Click on a link with the given text
agent.page.link_with(text: "Click here").click
# Complete and submit the first form on the page
agent.page.forms.first.tap do |f|
f['user[email]'] = 'hello@whatever.com'
f['user[password]'] = '123456789'
f['user[password_confirmation]'] = '123456789'
f['a_field[that_wasnt_in_the_form]'] = 'sneaky value'
f.submit
end
# Inspect the page body
puts agent.page.body.inspect
# Search for elements on the page
puts agent.page.search('.secret').text.strip
# Set a cookie
cookie = Mechanize::Cookie.new('key', 'value').tap do |c|
c.domain = 'localhost:3000'
c.path = '/'
end
agent.cookie_jar.add(agent.history.last.uri, cookie)
# Make it a little DSL-ish with instance_eval if you like...
Mechanize.new.instance_eval do
get 'http://localhost:3000'
page.link_with(text: 'Sign up').click
page.forms.first.tap do |f|
f['user[email]'] = 'hello@whatever.com'
f['user[password]'] = '123456789'
f['user[password_confirmation]'] = '123456789'
f.submit
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment