From: Michael Neumann Date: 2005-01-18T22:04:36+09:00 Subject: [ANN] WWW::Mechanize alike in Ruby Hi there, I just read the Web-testing thread in the latest "Ruby Weekly News" and want to tell the interested people that I hacked up a little library which I use in some small projects. Web::Unit didn't work for my purposes, and it was easier to reinvent the wheel than to hack Web::Unit (of course I first tried to hack it...). It depends on narf-lib (>= 0.6.3) more exactly, it's version of htmltools, and requires Ruby-current (due to limitations in the net/http library of 1.8.2). If you want to try it out with Ruby 1.8.2, just get the net/*.rb files from Ruby-current and use these instead (or use local overrides: $LOAD_PATH.unshift './net-overrides' ... ). Features: * Cookies (very preliminary, no expire etc.) * automatic redirect * Forms, Links Be aware that it's just a few-hour hack, to get my project done. It's still very generic and misses lots of usability methods (I use lots of "find" and use explicit parameters instead of implicit, e.g. last visited page). Here's a simple example, that logs into your rubyforge.org acount: require 'mechanize' agent = WWW::Mechanize.new {|a| a.log = Logger.new(STDERR) } page = agent.get('http://rubyforge.org/') link = page.links.find {|l| l.node.text =~ /Log In/ } page = agent.click(link) form = page.forms[1] form.fields.find {|f| f.name == 'form_loginname'}.value = ARGV[0] form.fields.find {|f| f.name == 'form_pw'}.value = ARGV[1] page = agent.submit(form, form.buttons.first) puts page.body Here are the sources: http://www.ntecs.de/viewcvs/viewcvs/WWW/ I've realized that Ruby has client libraries for www/http (cgi.rb, net/http) and server libraries for http (webrick), but not a common library that puts those things together... Regards, Michael