See Also: Home Links Personal Site Blogroll  FriendFeed CV

Tags:

WWW::Mechanize

The talk by Randal Schwartz (RS) at OSDC 2006 about using WWW Mechanize to test web apps but began with an intro to the original Test.pm module and the evolution of that into the more feature rich Test::More which is now bundled as a standard module in the Perl distribution (which Test.pm wasnt).

I've been using Test More for some time and actually found the stuff about this bloody useful, can't really put hand-on-heart and say I always write tests before I start coding, but thats the idea. I'd not really seen (or understood?) the use of the skip and todo methods of Test::More but after seeing some examples from Randal it's all much clearer to me.

I had seen the isa_ok() method (which tests class type) and hadnt thought of using it, but that and can_ok (which tests method availabilty in a package) should be essential in a test file now that I've seen them in anger. I also hadn't noticed the diag() method which is a useful way to spit out more verbose progress info.

The docs didnt seem clear to me but the skip count field actually logically 'skips' that number of tests following the statement. The other thing RS pointed out is that logically you wanna use skip for tests which you think will fail coz of some environmental or temporary thing and use todo for those that you havent actually finished coding.

WWW::Mechanize is a Perl toolkit used to perform (and repeat) mechanised testing of web applications. Its used programatically to to fetch the content of a URL using LWP::UserAgent, uses HTML::Parser to load the content into a queryable object, HTML::Forms to query and set form variables on the page, and then potentially resubmit the form, cookies and all.

After fetching a page the module provides methods which return a link collection from the page, you can iterate over the links and do testing (plain text and regexp) on the values of their labels and urls. You can also do a page fetch on those urls following use of those conditional tests. Similarly you can also test the actual base text of the page. Very nice.

Another nice feature is back() and fwd() methods which pop and push recent or new page fetches onto a stack to save having to re-request them. RS pointed out an issue here tho which is really important, if a test returns a false-positive and breaks out of the test block but you where expecting to continue and do a back call, you will still have the original page request in memory. This could be a nightmare to debug if you werent aware of it.

The tool ships with a command-line tool which can run a bunch of test files (stored using the usual Perl test file *.t extension) sequentially. There is also a Mech Dump tool (and similar mode in the Perl module) which spits out raw info about the currently fetched page with details on forms, images and links.

Overall I was impressed with the potential of this as a testing tool, but as discussed with a couple of fellow attendees after the session, it's not really the kind of tool you can deploy to non-programmer folks, and it seems like it'd be easier to build proper unit tests or one of the browser-based plugins that support user/ui focused testing modes (e.g. Selenium )

I asked Randal if was aware of anyone auto-generating WWW::Mechanize test scripts, he said he wasnt, but I reckon a great idea would be something that introspectively took a XForms file for example and built a test script from that. Even easier would be an XML config file that could achieve something similar with an XSLT transform possibly


See Also: OSDC 2006 | Web Development | Notes Index