Gtk2::WebKit::Mechanize - WWW::Mechanize done with HTML WebKit engine.


Gtk2-WebKit-Mechanize documentation Contained in the Gtk2-WebKit-Mechanize distribution.

Index


Code Index:

NAME

Top

Gtk2::WebKit::Mechanize - WWW::Mechanize done with HTML WebKit engine.

SYNOPSIS

Top

    $mech = Gtk2::WebKit::Mechanize->new;

    $mech->get('http://www.example.org');

    $mech->submit_form(fields => { field_a => 'A', field_b => 'B' });

    # returns "Hello"
    $mech->run_js('return "He" + "llo"');

DESCRIPTION

Top

This module provides WWW::Mechanize like interface using WebKit browser engine.

Aditionally it allows access to some of JavaScript functionality (e.g. calling JavaScript functions, accessing alerts and console messages etc.).

CONSTRUCTION

Top

Gtk2::WebKit::Mechanize->new;

Constructs new Gtk2::WebKit::Mechanize object.

METHODS

Top

$mech->get($url)

Loads $url.

$mech->run_js($js_str)

Evaluates $js_str in the context of the current page.

$mech->submit_form(%args)

Submits first form on pages using $args{fields}.

ACCESSORS

Top

$mech->title

Returns page title.

$mech->content

Returns current page source.

At present it uses document.body.innerHTML. Therefore page source will not be identical to the one sent by server.

AUTHOR

Top

    Boris Sukholitko
    CPAN ID: BOSU
    boriss@gmail.com

COPYRIGHT

Top

SEE ALSO

Top

WWW::Mechanize, Mozilla::Mechanize, Mozilla::Mechanize::GUITester


Gtk2-WebKit-Mechanize documentation Contained in the Gtk2-WebKit-Mechanize distribution.
use strict;
use warnings FATAL => 'all';

package Gtk2::WebKit::Mechanize;
use base 'Class::Accessor::Fast';

use Gtk2 -init;
use Gtk2::WebKit;
__PACKAGE__->mk_accessors(qw(console_messages alerts view window));

our $VERSION = '0.01';

sub new {
	my $class = shift;
	my $view = Gtk2::WebKit::WebView->new;
	my $sw = Gtk2::ScrolledWindow->new;
	$sw->add($view);

	my $win = Gtk2::Window->new;
	$win->set_default_size(800, 600);
	$win->add($sw);

	my $self = bless { view => $view, window => $win
			, alerts => [], console_messages => [] }, $class;
	$view->signal_connect('load-finished' => sub { Gtk2->main_quit });
	$view->signal_connect('script-alert' => sub {
		push @{ $self->alerts }, $_[2];
	});
	$view->signal_connect('console-message' => sub {
		push @{ $self->console_messages }, $_[1];
	});

	$win->show_all;

	return $self;
}

sub get {
	my ($self, $url) = @_;
	$self->view->open($url);
	Gtk2->main;
}

sub run_js {
	my ($self, $js) = @_;
	my $fn = "___run_js_$$";
	$self->view->execute_script("function $fn() { $js }; alert($fn());");
	return pop @{ $self->alerts };
}

sub submit_form {
	my ($self, %form) = @_;
	while (my ($n, $v) = each %{ $form{fields} }) {
		$self->run_js("(document.getElementsByName('$n'))[0]"
			. ".value = '$v'");
	}
	$self->run_js('(document.getElementsByTagName("FORM"))[0].submit()');
	Gtk2->main;
}

sub title {
	return shift()->view->get_main_frame->get_title;
}

sub content {
	return shift()->run_js('return document.body.innerHTML');
}

1;