WWW-Mechanize-PhantomJS
view release on metacpan or search on metacpan
lib/WWW/Mechanize/PhantomJS.pm view on Meta::CPAN
use Time::HiRes qw(time sleep);
use Carp 'croak';
our $VERSION= '0.25';
our @CARP_NOT=qw(Selenium::Remote::Driver);
=head1 NAME
WWW::Mechanize::PhantomJS - automate the PhantomJS browser
=head1 SYNOPSIS
use WWW::Mechanize::PhantomJS;
my $mech = WWW::Mechanize::PhantomJS->new();
$mech->get('http://google.com');
$mech->eval_in_page('alert("Hello PhantomJS")');
my $png= $mech->content_as_png();
=head2 C<< WWW::Mechanize::PhantomJS->new %options >>
my $mech = WWW::Mechanize::PhantomJS->new();
=over 4
=item B<autodie>
Control whether HTTP errors are fatal.
autodie => 0, # make HTTP errors non-fatal
The default is to have HTTP errors fatal,
as that makes debugging much easier than expecting
you to actually check the results of every action.
=item B<port>
Specify the port where PhantomJS should listen
port => 8910
=item B<log>
Specify the log level of PhantomJS
log => 'OFF' # Also INFO, WARN, DEBUG
=item B<launch_exe>
Specify the path to the PhantomJS executable.
The default is C<phantomjs> as found via C<$ENV{PATH}>.
You can also provide this information from the outside
by setting C<$ENV{PHANTOMJS_EXE}>.
=item B<phantomjs_arg>
Additional command line arguments to C<phantomjs>. (phantomjs -h)
phantomjs_arg => ["--proxy=$ENV{HTTP_PROXY}"]
=item B<launch_ghostdriver>
Filename of the C<ghostdriver> Javascript code
to launch. The default is the file distributed with this module.
launch_ghostdriver => "devel/my/ghostdriver/main.js",
=item B<launch_arg>
Specify additional parameters to the Ghostdriver script.
launch_arg => [ "--some-new-parameter=foo" ],
Some interesting parameters are:
"--webdriver=$port",
'--webdriver-logfile=/tmp/webdriver',
'--webdriver-loglevel=DEBUG',
'--debug=true',
note: these set config.xxx values in ghostrdriver/config.js
=item B<cookie_file>
Cookies are not directly persisted. If you pass in a path here,
that file will be used to store or retrieve cookies.
=item B<ignore_ssl_errors>
If you want C<phantomjs> to ignore SSL errors, pass a true value here.
=item B<driver>
A premade L<Selenium::Remote::Driver> object.
=item B<report_js_errors>
If set to 1, after each request tests for Javascript errors and warns. Useful
for testing with C<use warnings qw(fatal)>.
=back
=cut
sub build_command_line {
my( $class, $options )= @_;
$options->{ "log" } ||= 'OFF';
$options->{ launch_exe } ||= $ENV{PHANTOMJS_EXE} || 'phantomjs';
(my $ghostdir_default= __FILE__) =~ s!\.pm$!!;
$ghostdir_default= File::Spec->catfile( $ghostdir_default, 'ghostdriver', 'main.js' );
$options->{ launch_ghostdir } ||= $ghostdir_default;
$options->{ launch_arg } ||= [];
$options->{ phantomjs_arg } ||= [];
# config.js defaults config.port to 8910
# this is the proper way to overwrite it (not sure wtf the PhantomJS parameter does above)
if ($options->{port}) {
( run in 0.412 second using v1.01-cache-2.11-cpan-71847e10f99 )