Brock Wilcox > WWW-HtmlUnit-0.22 > WWW::HtmlUnit::Sweet

Download:
WWW-HtmlUnit-0.22.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  1
Open  0
View/Report Bugs
Source  

NAME ^

WWW::HtmlUnit::Sweet - Wrapper around WWW::HtmlUnit to add some sweetness

SYNOPSIS ^

  use WWW::HtmlUnit::Sweet;
  my $agent = WWW::HtmlUnit::Sweet->new;

  $agent->getPage('http://google.com/');

  # Type into the currently focused element
  $agent->type("Hello\n");

  # Print out the XML of the page
  print $agent->asXml;

DESCRIPTION ^

Using WWW::HtmlUnit as a foundation, this adds some convenience things. The main addition is that the $agent you get from ->new does some AUTOLOAD things to allow you to treat the $agent as either a browser, a window, or a page. That way you can treat it a lot more like a WWW::Mechanize object.

This module might change drastically, buyer beware!

IMPORT PARAMETERS ^

When you 'use' this module, you can pass some parameters. Any parameter that WWW::HtmlUnit::Sweet doesn't use will be passed on to WWW::HtmlUnit, or ultimately Inline::Java.

Useful examples:

  # Show errors on STDERR
  use WWW::HtmlUnit::Sweet show_errors => 1;

  # Append errors to /tmp/errors.txt
  use WWW::HtmlUnit::Sweet error_filename => '/tmp/errors.txt';

Note that if you don't pass anything, errors will be sent to /dev/null (or a temporary file if you don't have /dev/null).

METHODS ^

$agent = WWW::HtmlUnit::Sweet->new

Create a new sweet agent. Use this kinda like looking at a browser on the screen. The methods you call will be invoked (if possible) on the current browser, window, page, or focused element.

The 'new' method can also take a browser version and a starting url, like this:

  my $agent = WWW::HtmlUnit::Sweet->new(
    version => 'FIREFOX_3',
    url => 'http://google.com/'
  );

$agent->wait_for(sub { ... }, $timeout)

Execute the provided sub once a second until it returns true, or until the the timeout has been reached. If a timeout isn't passed, it will default to 10 seconds (which you can change by setting $WWW::HtmlUnit::Sweet::default_timeout). This is handy for waiting for the page to finish executing some javascript, or loading.

Example:

  # Wait for an element with id 'foo' to exist
  $agent->wait_for(sub {
    $agent->getElementById('foo')
  });

AUTOLOAD, aka $agent->whatever(..)

This is where the sweetness starts kicking in. First it will try to call ->whatever on the browser, and if there is no method named 'whatever' there it will be called on the current window, and if there is no method named 'whatever' there it will be called on the current page in that window, and if there is no method 'whatever' there it will be called on the currently focused element.

Examples:

  # This works at the browser level
  $agent->getPage('http://google.com/');

  # Get the 'name' for the current window
  my $window_name = $agent->getName;

  # Working from the current page, get an element by ID
  my $sidebar_element = $agent->getElementById('sidebar');

  # Click on the currently focused element
  $agent->click;

This scheme works quite well because HtmlUnit itself just so happens to not overlap their method names between different classes. Lucky us!

Note: We also call ->toArray on results if needed. Probably at some point we'll get ALL array-like results from HtmlUnit to auto-execute ->toArray.

TODO ^

Add more documentation and examples and sweetness :)

SEE ALSO ^

WWW::HtmlUnit

AUTHOR ^

  Brock Wilcox <awwaiid@thelackthereof.org> - http://thelackthereof.org/

COPYRIGHT ^

  Copyright (c) 2009-2011 Brock Wilcox <awwaiid@thelackthereof.org>. All rights
  reserved.  This program is free software; you can redistribute it and/or
  modify it under the same terms as Perl itself.
syntax highlighting: