package WWW::Mechanize::Chrome::MooseWrapper ; use Moose; use MooseX::NonMoose; use Time::HiRes qw(usleep); use File::Temp 'tempdir'; use Log::Log4perl qw(:easy); use Data::Dumper qw(Dumper); use namespace::autoclean; extends 'WWW::Mechanize::Chrome'; sub FOREIGNBUILDARGS { my $class = shift; my %args = ( @_ == 1 ? %{ $_[0] } : @_); # set default launch args $args{launch_exe} = $args{launch_exe} || "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; $args{launch_arg} = $args{launch_arg} || [ '--disable-notifications' ]; $args{data_directory} = $args{data_directory} || tempdir(); $args{incognito} = exists $args{incognito} ? $args{incognito} : 1; return %args; } ### Public methods ### sub scroll_to_bottom { my $s = shift; $s->eval( 'window.scroll(0,document.body.scrollHeight)' ); } sub scroll_to_top { my $s = shift; $s->eval( 'window.scroll(0,0)' ); } sub get_element_count { my $s = shift; my ($el_count) = $s->eval( 'document.getElementsByTagName("*").length' ); return $el_count; } around get => sub { my $orig = shift; my $s = shift; my $url = shift; $s->SUPER::get($url); my $el_count = $s->get_element_count; # wait 1/10th sec for more of the page to load usleep 100000; while ($el_count != $s->get_element_count) { # wait 1/10th sec for more of the page to load usleep 100000; $el_count = $s->get_element_count; } }; ### Private methods ### __PACKAGE__->meta->make_immutable; 1; # Magic true value