| 1 | =pod | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | WWW::Mechanize::Chrome::Cookbook - Getting things done with WWW::Mechanize::Chrome | ||
| 6 | |||
| 7 | =head1 Chrome versions | ||
| 8 | |||
| 9 | You can find various current Chrome builds at | ||
| 10 | L<https://chromium.woolyss.com/> . | ||
| 11 | |||
| 12 | The recommended approach to automation is to save a Chrome / Chromium version | ||
| 13 | and disable automatic updates so you can update at a defined point in time | ||
| 14 | instead and keep the change to your automation under control. | ||
| 15 | |||
| 16 | =head1 Web Application Testing with Chrome | ||
| 17 | |||
| 18 | =head2 Rationale | ||
| 19 | |||
| 20 | If you have an application with complex Javascript, you may want to do end to | ||
| 21 | end tests using WWW::Mechanize::Chrome. You can run your server application and | ||
| 22 | your test program in the same process if your server application can be run | ||
| 23 | under PSGI, as most web frameworks do. | ||
| 24 | |||
| 25 | Having all data within one process makes it very easy to fudge configuration | ||
| 26 | values or database entries at just the right time. | ||
| 27 | |||
| 28 | =head2 Initializing the web server in your test script | ||
| 29 | |||
| 30 | You will need to use a PSGI web server written in Perl that supports event | ||
| 31 | loops also supported by WWW::Mechanize::Chrome. L<Twiggy> is one such server. | ||
| 32 | The setup for Twiggy is as follows: | ||
| 33 | |||
| 34 | use Twiggy::Server; | ||
| 35 | use File::Temp 'tempdir'; | ||
| 36 | |||
| 37 | my $port = 5099; | ||
| 38 | my $server = Twiggy::Server->new( | ||
| 39 | host => '127.0.0.1', | ||
| 40 | port => $port, | ||
| 41 | ); | ||
| 42 | |||
| 43 | # Dancer specific parts | ||
| 44 | $ENV{DANCER_APPHANDLER} = 'Dancer::Handler::PSGI'; | ||
| 45 | my $handler = Dancer::Handler->get_handler(); | ||
| 46 | Dancer::_load_app('App::mykeep'); | ||
| 47 | my $app = $handler->psgi_app(); | ||
| 48 | |||
| 49 | # Rest of Twiggy setup | ||
| 50 | $server->register_service($app); | ||
| 51 | |||
| 52 | # Fudge the config as appropriate for our test | ||
| 53 | Dancer::config()->{mykeep}->{notes_dir} = tempdir( | ||
| 54 | CLEANUP => 1, | ||
| 55 | ); | ||
| 56 | |||
| 57 | my $mech = WWW::Mechanize::Chrome->new( | ||
| 58 | ); | ||
| 59 | my $res = $mech->get("http://127.0.0.1:$port"); | ||
| 60 | ok $res->is_success, "We can request the page"; | ||
| 61 | |||
| 62 | =head1 Debugging Headless Sessions | ||
| 63 | |||
| 64 | If you want to watch what a headless browser automation run is doing, you can | ||
| 65 | do so by sending a screencast from WWW::Mechanize::Chrome to a different browser | ||
| 66 | that supports websockets by using L<Mojolicious::Plugin::PNGCast> from within | ||
| 67 | your automation session: | ||
| 68 | |||
| 69 | use Mojolicious::Lite; | ||
| 70 | use Mojo::Server::Daemon; | ||
| 71 | use WWW::Mechanize::Chrome; | ||
| 72 | plugin 'PNGCast'; | ||
| 73 | |||
| 74 | my $daemon_url = 'http://localhost:3000'; | ||
| 75 | |||
| 76 | my $ws_monitor = Mojo::Server::Daemon->new(app => app()); | ||
| 77 | $ws_monitor->listen([$daemon_url]); | ||
| 78 | $ws_monitor->start; | ||
| 79 | |||
| 80 | my $mech = WWW::Mechanize::Chrome->new( headless => 1 ); | ||
| 81 | $mech->setScreenFrameCallback( sub { | ||
| 82 | app->send_frame( $_[1]->{data} )} | ||
| 83 | ); | ||
| 84 | |||
| 85 | print "Watch progress at $daemon_url\n"; | ||
| 86 | sleep 5; | ||
| 87 | |||
| 88 | $mech->get('https://example.com'); | ||
| 89 | # ... more automation | ||
| 90 | |||
| 91 | This will send the progress of your headless session to your browser so you can | ||
| 92 | see the differences between what you expect and what the browser displays. | ||
| 93 | |||
| 94 | =head1 Listing all requests made by a page | ||
| 95 | |||
| 96 | Sometimes you want to block a single class of requests, or just list what | ||
| 97 | requests a page makes. This is supported since Chrome 80 or so. | ||
| 98 | |||
| 99 | $mech->target->send_message('Fetch.enable')->get; | ||
| 100 | my $request_listener = $mech->add_listener('Fetch.requestPaused', sub { | ||
| 101 | my( $info ) = @_; | ||
| 102 | my $id = $info->{params}->{requestId}; | ||
| 103 | my $request = $info->{params}->{request}; | ||
| 104 | |||
| 105 | if( $request->{url} =~ /\.html(\?.*)?$/ ) { | ||
| 106 | $mech->target->send_message('Fetch.continueRequest', requestId => $id, )->retain; | ||
| 107 | return; | ||
| 108 | } else{ | ||
| 109 | diag "Ignored $request->{url}"; | ||
| 110 | $mech->target->send_message('Fetch.failRequest', requestId => $id, errorReason => 'AddressUnreachable' ); | ||
| 111 | }; | ||
| 112 | }); | ||
| 113 | |||
| 114 | =head1 SEE ALSO | ||
| 115 | |||
| 116 | L<Detecting Chrome Headless|http://antoinevastel.github.io/bot%20detection/2018/01/17/detect-chrome-headless-v2.html> | ||
| 117 | |||
| 118 | L<Making Chrome Headless Undetectable|https://intoli.com/blog/making-chrome-headless-undetectable/> | ||
| 119 | |||
| 120 | L<Chrome Headless Detection|https://github.com/paulirish/headless-cat-n-mouse> | ||
| 121 | |||
| 122 | L<How to bypass CloudFlare|https://scrapeops.io/web-scraping-playbook/how-to-bypass-cloudflare/> | ||
| 123 | |||
| 124 | =head1 REPOSITORY | ||
| 125 | |||
| 126 | The public repository of this module is | ||
| 127 | L<https://github.com/Corion/www-mechanize-chrome>. | ||
| 128 | |||
| 129 | =head1 SUPPORT | ||
| 130 | |||
| 131 | The public support forum of this module is L<https://perlmonks.org/>. | ||
| 132 | |||
| 133 | =head1 BUG TRACKER | ||
| 134 | |||
| 135 | Please report bugs in this module via the Github bug queue at | ||
| 136 | L<https://github.com/Corion/WWW-Mechanize-Chrome/issues> | ||
| 137 | |||
| 138 | =head1 AUTHOR | ||
| 139 | |||
| 140 | Max Maischein C<corion@cpan.org> | ||
| 141 | |||
| 142 | =head1 COPYRIGHT (c) | ||
| 143 | |||
| 144 | Copyright 2010-2026 by Max Maischein C<corion@cpan.org>. | ||
| 145 | |||
| 146 | =head1 LICENSE | ||
| 147 | |||
| 148 | This module is released under the same terms as Perl itself. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | package WWW::Mechanize::Chrome::Cookbook; | ||
| 153 | our $VERSION = '0.80'; | ||
| 154 | 1; |