The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Mozilla::Mechanize::GUITester - enhances Mozilla::Mechanize with GUI testing.


  use Mozilla::Mechanize::GUITester;

  # regular Mozilla::Mechanize initialization
  my $mech = Mozilla::Mechanize::GUITester->new(%mechanize_args);

  # convenience wrapper over GetElementById and QueryInterface
  my $elem = $mech->get_html_element_by_id("some_id");

  # click mouse at the element position + (1, 1)
  $mech->x_click($elem, 1, 1);

  # play with the mouse relative to the element position
  $mech->x_mouse_down($elem, 2, 2);
  $mech->x_mouse_move($elem, 4, 4);
  $mech->x_mouse_up($elem, 4, 4);

  # run some javascript code and print its result
  print $mech->run_js('return "js: " + 2');

  # are there any javascript errors?
  print Dumper($mech->console_messages);

  # find out HTTP response status (works only for HTTP protocol)
  print $mech->status;


This module enhances Mozilla::Mechanize with convenience functions allowing testing of DHTML/JavaScript rich pages.

It uses X11::GUITest to emulate mouse clicking, dragging and moving over elements in DOM tree.

It also allows running of arbitrary javascript code in the page context and getting back the results.



This constructor delegates to Mozilla::Mechanize::new function. See Mozilla::Mechanize manual for its description.



Returns last response status using Mozilla::ObserverService and nsIHTTPChannel:responseStatus function.

Note that it works only for HTTP requests.


Returns last alert contents intercepted through Mozilla::PromptService.

It is useful for communication from javascript.


Returns arrayref of all console messages (e.g. javascript errors) aggregated so far.

See Mozilla nsIConsoleService documentation for more details.



Wraps $js_code with JavaScript function and invokes it. Its result is returned as string and intercepted through alert().

See last_alert accessor above.


Uses GetElementById and QueryInterface to get Mozilla::DOM::HTMLElement.

See Mozilla::DOM documentation for more details.

$mech->x_click($element, $x, $y)

Emulates mouse click at ($element.left + $x, $ + $y) coordinates.

$mech->x_mouse_down($element, $x, $y)

Presses left mouse button at ($element.left + $x, $ + $y).

$mech->x_mouse_up($element, $x, $y)

Releases left mouse button at ($element.left + $x, $ + $y).

$mech->x_mouse_move($element, $x, $y)

Moves mouse to ($element.left + $x, $ + $y).


Boris Sukholitko <>


This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.