Chrome/DevToolsProtocol/Target.pm

filter: Statementstmt 19.8% 59 / 298 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 82 MC/DCmcdc 0.0% 0 / 47 Subroutinesub 17.5% 14 / 80 Podpod 85.1% 23 / 27 totaltotal 15.7% 96 / 608 CC 77 SCAR 52.9
CC
77
Cov
13%
on_response
930.0
connect
132.0
__ANON__
72.0
CRAP 198.3
« WWW/Mechanize/Chrome.pm ↑ Summary Chrome/DevToolsProtocol.pm »
1 package Chrome::DevToolsProtocol::Target;
2 69 ▶use 5.020; # for signatures
Subroutine BEGIN: called CC=1 SCAR=0.0
3 69 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 69 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 69 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 69 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 69 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 69 ▶use Future::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 69 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 69 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 69 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 69 ▶use Chrome::DevToolsProtocol::Transport;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 69 ▶use Scalar::Util 'weaken', 'isweak';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 69 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 69 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 69 with 'MooX::Role::EventEmitter';
20  
21 69 our $VERSION = '0.80';
22 69 our @CARP_NOT;
23  
24 =head1 NAME
25  
26 Chrome::DevToolsProtocol::Target - wrapper for talking to a page in a Target
27  
28 =head1 SYNOPSIS
29  
30 # Usually, WWW::Mechanize::Chrome automatically creates a driver for you
31 my $driver = Chrome::DevToolsProtocol::Target->new(
32 transport => $target,
33 );
34 $driver->connect( new_tab => 1 )->get
35  
36 =head1 METHODS
37  
38 =head2 C<< ->new( %args ) >>
39  
40 my $driver = Chrome::DevToolsProtocol::Target->new(
41 transport => $target,
42 auto_close => 0,
43 error_handler => sub {
44 # Reraise the error
45 croak $_[1]
46 },
47 );
48  
49 These members can mostly be set through the constructor arguments:
50  
51 =over 4
52  
53 =cut
54  
55 0 ▶sub _build_log( $self ) {
Subroutine _build_log: not called CC=1 SCAR=6.9
56 0 require Log::Log4perl;
57 0 Log::Log4perl->get_logger(__PACKAGE__);
58 }
59  
60  
61 =item B<json>
62  
63 The JSON decoder used
64  
65 =cut
66  
67 has 'json' => (
68 is => 'ro',
69 0 ▶ default => sub { JSON->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 69 );
71  
72 =item B<tab>
73  
74 Which tab to reuse (if any)
75  
76 =cut
77  
78 69 has 'tab' => (
79 is => 'rw',
80 );
81  
82 =item B<autoclose>
83  
84 Close the tab when the object goes out of scope
85  
86 =cut
87  
88 69 has 'autoclose' => (
89 is => 'rw',
90 );
91  
92 =item B<log>
93  
94 A premade L<Log::Log4perl> object to act as logger
95  
96 =cut
97  
98 has 'receivers' => (
99 is => 'ro',
100 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 69 );
102  
103 =item B<app>
104  
105 If launching Chrome in app mode, connect to this page
106  
107 =cut
108  
109 has 'app' => (
110 is => 'ro',
111 0 ▶ default => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 69 );
113  
114 =back
115  
116 =head1 EVENTS
117  
118 =over 4
119  
120 =item B<message>
121  
122 A callback invoked for every message
123  
124 =cut
125  
126 has '_one_shot' => (
127 is => 'ro',
128 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 69 );
130  
131 has 'listener' => (
132 is => 'ro',
133 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 69 );
135  
136 has 'sequence_number' => (
137 is => 'rw',
138 0 ▶ default => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 69 );
140  
141 =item B<transport>
142  
143 The event-loop specific transport backend
144  
145 =cut
146  
147 69 has 'transport' => (
148 is => 'ro',
149 handles => [qw[future sleep endpoint log _log
150 getTargets
151 ]],
152 );
153  
154 # This is maybe deprecated?
155 69 has 'targetId' => (
156 is => 'rw',
157 );
158  
159 69 has 'sessionId' => (
160 is => 'rw',
161 );
162  
163 69 has 'browserContextId' => (
164 is => 'rw',
165 );
166  
167 0 ▶around BUILDARGS => sub( $orig, $class, %args ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 0 $args{ _log } = delete $args{ 'log' };
169 0 $class->$orig( %args )
170 69 };
171  
172 =back
173  
174 =head2 C<< ->future >>
175  
176 my $f = $driver->future();
177  
178 Returns a backend-specific generic future
179  
180 =head2 C<< ->endpoint >>
181  
182 my $url = $driver->endpoint();
183  
184 Returns the URL endpoint to talk to for the connected tab
185  
186 =head2 C<< ->add_listener >>
187  
188 my $l = $driver->add_listener(
189 'Page.domContentEventFired',
190 sub {
191 warn "The DOMContent event was fired";
192 },
193 );
194  
195 # ...
196  
197 undef $l; # stop listening
198  
199 Adds a callback for the given event name. The callback will be removed once
200 the return value goes out of scope.
201  
202 =cut
203  
204 0 ▶sub add_listener( $self, $event, $callback ) {
Subroutine add_listener: not called CC=2 SCAR=17.9
Pod documented
205 0 my $listener = Chrome::DevToolsProtocol::EventListener->new(
206 protocol => $self,
207 callback => $callback,
208 event => $event,
209 );
210 0 ▶ $self->listener->{ $event } ||= [];
Condition0%0/2
$self->listener->{$event} ||= []
l!l
00
MC/DC0%0/1
$self->listener->{$event} ||= []
$self->listener->{$event}
Truth table0%0/2
A
A= $self->listener->{$event}
Aresult
11
00
211 0 push @{ $self->listener->{ $event }}, $listener;
212 0 weaken $self->listener->{ $event }->[-1];
213 0 return $listener
214 }
215  
216 =head2 C<< ->remove_listener >>
217  
218 $driver->remove_listener($l);
219  
220 Explicitly remove a listener.
221  
222 =cut
223  
224 0 ▶sub remove_listener( $self, $listener ) {
Subroutine remove_listener: not called CC=4 SCAR=30.0
Pod documented
225 # $listener->{event} can be undef during global destruction
226 0 ▶ if( my $event = $listener->event ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $event = $listener->event) 0 0 0
227 0 ▶ my $l = $self->listener->{ $event } ||= [];
Condition0%0/2
$self->listener->{$event} ||= []
l!l
00
MC/DC0%0/1
$self->listener->{$event} ||= []
$self->listener->{$event}
Truth table0%0/2
A
A= $self->listener->{$event}
Aresult
11
00
228 0 @{$l} = grep { $_ != $listener }
229 0 grep { defined $_ }
230 0 @{$self->listener->{ $event }};
231 # re-weaken our references
232 0 for (0..$#$l) {
233 0 weaken $l->[$_];
234 };
235 };
236 }
237  
238 =head2 C<< ->log >>
239  
240 $driver->log('debug', "Warbling doodads", { doodad => 'this' } );
241  
242 Log a message
243  
244 =head2 C<< ->connect >>
245  
246 my $f = $driver->connect()->get;
247  
248 Asynchronously connect to the Chrome browser, returning a Future.
249  
250 =cut
251  
252 0 ▶sub connect( $self, %args ) {
Subroutine connect: not called CC=11 SCAR=48.8
Pod documented
253 0 my $s = $self;
254 0 weaken $s;
255 0 ▶ my $done = $self->transport->is_connected
Branch: 1 branch
BranchTrueFalseTotal
$self->transport->is_connected ? : 0 0 0
256 ? Future->done
257 : $self->transport->connect();
258  
259 0 ▶ $done = $done->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
260 0 ▶ $s->{l} = $s->transport->add_listener('Target.receivedMessageFromTarget', sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
261 0 ▶ if( $s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s) 0 0 0
262 #$s->log( 'trace', '(target) receivedMessage', $_[0] );
263 0 my $id = $s->targetId;
264 0 my $sid = $s->sessionId;
265 0 ▶ if( exists $_[0]->{params}->{sessionId}
Branch: 2 branches
BranchTrueFalseTotal
if (exists $_[0]{'params'}{'sessionId'} and $sid and $_[0]{'params'}{'sessionId'} eq $sid) { } 0 0 0
elsif (not $id or $_[0]{'params'}{'targetId'} and $id eq $_[0]{'params'}{'targetId'}) { } 0 0 0
Condition0%0/6
exists $_[0]{"params"}{"sessionId"} and $sid and $_[0]{"params"}{"sessionId"} eq $sid
!ll&&!rl&&r
000
exists $_[0]{"params"}{"sessionId"} and $sid
!ll&&!rl&&r
000
MC/DC0%0/3
exists $_[0]{"params"}{"sessionId"} and $sid and $_[0]{"params"}{"sessionId"} eq $sid
exists $_[0]{"params"}{"sessionId"} $sid $_[0]{"params"}{"sessionId"} eq $sid
Truth table0%0/4
A and B and C
A= exists $_[0]{"params"}{"sessionId"}B= $sidC= $_[0]{"params"}{"sessionId"} eq $sid
ABCresult
0XX0
10X0
1100
1111
266 and $sid
267 and $_[0]->{params}->{sessionId} eq $sid) {
268 0 my $payload = $_[0]->{params}->{message};
269 0 $s->on_response( undef, $payload );
270 ▶ } elsif( !$id
Condition0%0/6
not $id or $_[0]{"params"}{"targetId"} and $id eq $_[0]{"params"}{"targetId"}
l!l&&r!l&&!r
000
$_[0]{"params"}{"targetId"} and $id eq $_[0]{"params"}{"targetId"}
!ll&&!rl&&r
000
MC/DC0%0/3
not $id or $_[0]{"params"}{"targetId"} and $id eq $_[0]{"params"}{"targetId"}
not $id $_[0]{"params"}{"targetId"} $id eq $_[0]{"params"}{"targetId"}
Truth table0%0/4
not(A) or B and C
A= not $idB= $_[0]{"params"}{"targetId"}C= $id eq $_[0]{"params"}{"targetId"}
ABCresult
1XX1
0111
00X0
0100
271 or ($_[0]->{params}->{targetId} and $id eq $_[0]->{params}->{targetId})) {
272 0 my $payload = $_[0]->{params}->{message};
273 0 $s->on_response( undef, $payload );
274 };
275 #} else {
276 # warn "Target listener for answers has gone away";
277 # use Data::Dumper; warn Dumper($_[0]);
278 };
279 0 });
280 0 Future->done;
281 0 });
282  
283 0 ▶ if( $args{ new_tab } ) { # should be renamed "separate_session"
Branch: 6 branches
BranchTrueFalseTotal
if ($args{'new_tab'}) { } 0 0 0
elsif (ref $args{'tab'} eq 'Regexp') { } 0 0 0
elsif (ref $args{'tab'}) { } 0 0 0
elsif (defined $args{'tab'} and $args{'tab'} eq 'current') { } 0 0 0
elsif (defined $args{'tab'} and $args{'tab'} =~ /^\d{1,5}$/u) { } 0 0 0
elsif ($args{'tab'}) { } 0 0 0
284 0 ▶ if( $args{ separate_session }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'separate_session'}) { } 0 0 0
285 # Set up a new browser context
286 0 ▶ $done = $done->then( sub { $s->transport->send_message('Target.createBrowserContext')})
Subroutine __ANON__: not called CC=1 SCAR=6.9
287 0 ▶ ->then( sub( $info ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
288 0 $s->browserContextId( $info->{browserContextId} );
289 0 Future->done();
290 0 });
291  
292 } else {
293 # Find an existing browser context and use that one
294 0 ▶ $done = $done->then( sub { $s->getTargets })
Subroutine __ANON__: not called CC=1 SCAR=6.9
295 0 ▶ ->then( sub( @targets ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
296 #$self->browserContextId( $targets[0]->{browserContextId} );
297 0 Future->done();
298 0 });
299 }
300  
301 0 ▶ $done = $done->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
302 0 my $id = $s->browserContextId;
303 $s->createTarget(
304 0 ▶ url => $args{ start_url } || 'about:blank',
Condition0%0/2
$args{'start_url'} || 'about:blank'
l!l
00
MC/DC0%0/1
$args{'start_url'} || 'about:blank'
$args{'start_url'}
Truth table0%0/2
A
A= $args{'start_url'}
Aresult
11
00
305 maybe browserContextId => $id,
306 );
307 0 ▶ })->then(sub( $info ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
308 0 $s->tab( $info );
309 $s->attach( $info->{targetId} )
310 0 });
311  
312 } elsif( ref $args{ tab } eq 'Regexp') {
313 # Let's assume that the tab is a regex:
314  
315 0 ▶ $done = $done->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
316 0 $s->getTargets()
317 0 ▶ })->then(sub( @tabs ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
318 0 (my $tab) = grep { $_->{title} =~ /$args{ tab }/ } @tabs;
319  
320 0 ▶ if( ! $tab ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $tab) { } 0 0 0
elsif (not $tab->{'targetId'}) { } 0 0 0
321 0 $s->log('warn', "Couldn't find a tab matching /$args{ tab }/");
322 0 croak "Couldn't find a tab matching /$args{ tab }/";
323 } elsif( ! $tab->{targetId} ) {
324 0 local @CARP_NOT = ('Future',@CARP_NOT);
325 0 croak "Found the tab but it didn't have a targetId";
326 };
327 0 $s->tab( $tab );
328 $s->attach( $tab->{targetId} )
329 0 });
330  
331 } elsif( ref $args{ tab } ) {
332 # Let's assume that the tab is a Target hash:
333 0 my $tab = $args{ tab };
334 0 $self->tab($tab);
335 0 ▶ $done = $done->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
336 0 $s->attach( $s->tab->{targetId});
337 0 });
338  
339 ▶ } elsif( defined $args{ tab } and $args{ tab } eq 'current' ) {
Condition0%0/3
defined $args{"tab"} and $args{"tab"} eq "current"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"tab"} and $args{"tab"} eq "current"
defined $args{"tab"} $args{"tab"} eq "current"
Truth table0%0/3
A and B
A= defined $args{"tab"}B= $args{"tab"} eq "current"
ABresult
0X0
100
111
340 0 ▶ $done = $done->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
341 0 $s->getTargets()
342 0 ▶ })->then(sub( @tabs ) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
343 0 ▶ (my $tab) = grep { $_->{type} eq 'page' && $_->{targetId} && $_->{attached} } @tabs;
Condition0%0/6
$_->{'type'} eq 'page' && $_->{'targetId'} && $_->{'attached'}
!ll&&!rl&&r
000
$_->{"type"} eq "page" and $_->{"targetId"}
!ll&&!rl&&r
000
MC/DC0%0/3
$_->{'type'} eq 'page' && $_->{'targetId'} && $_->{'attached'}
$_->{"type"} eq "page" $_->{"targetId"} $_->{'attached'}
Truth table0%0/4
A and B && C
A= $_->{"type"} eq "page"B= $_->{"targetId"}C= $_->{'attached'}
ABCresult
0XX0
10X0
1100
1111
344 0 ▶ ($tab) ||= grep { $_->{type} eq 'page' && $_->{targetId} } @tabs;
Condition0%0/6
$tab ||= grep({$_->{"type"} eq "page" and $_->{"targetId"};} @tabs)
l!l&&r!l&&!r
000
$_->{'type'} eq 'page' && $_->{'targetId'}
!ll&&!rl&&r
000
MC/DC0%0/2
$tab ||= grep({$_->{"type"} eq "page" and $_->{"targetId"};} @tabs)
$tab grep({$_->{"type"} eq "page" and $_->{"targetId"};} @tabs)
MC/DC0%0/2
$_->{'type'} eq 'page' && $_->{'targetId'}
$_->{'type'} eq 'page' $_->{'targetId'}
Truth table0%0/3
A ||= B
A= $tabB= grep({$_->{"type"} eq "page" and $_->{"targetId"};} @tabs)
ABresult
1X1
011
000
Truth table0%0/3
A && B
A= $_->{'type'} eq 'page'B= $_->{'targetId'}
ABresult
0X0
100
111
345 0 $s->tab($tab);
346 $s->attach( $tab->{targetId} )
347 0 });
348  
349 ▶ } elsif( defined $args{ tab } and $args{ tab } =~ /^\d{1,5}$/ ) {
Condition0%0/3
defined $args{"tab"} and $args{"tab"} =~ /^\d{1,5}$/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"tab"} and $args{"tab"} =~ /^\d{1,5}$/u
defined $args{"tab"} $args{"tab"} =~ /^\d{1,5}$/u
Truth table0%0/3
A and B
A= defined $args{"tab"}B= $args{"tab"} =~ /^\d{1,5}$/u
ABresult
0X0
100
111
350 0 ▶ $done = $done->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
351 0 $s->getTargets()
352 0 ▶ })->then(sub( @tabs ) {
Subroutine __ANON__: not called CC=7 SCAR=40.3
353 0 my $res;
354 my @visible_tabs;
355 0 ▶ if( $args{ app } // $self->app ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'app'} // $self->app) { } 0 0 0
Condition0%0/3
$args{"app"} // $self->app
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"app"} // $self->app
$args{"app"} $self->app
Truth table0%0/3
A // B
A= $args{"app"}B= $self->app
ABresult
1X1
011
000
356 0 ▶ @visible_tabs = grep { $_->{type} eq 'app' && $_->{targetId} } @tabs;
Condition0%0/3
$_->{'type'} eq 'app' && $_->{'targetId'}
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'type'} eq 'app' && $_->{'targetId'}
$_->{'type'} eq 'app' $_->{'targetId'}
Truth table0%0/3
A && B
A= $_->{'type'} eq 'app'B= $_->{'targetId'}
ABresult
0X0
100
111
357 } else {
358 0 ▶ @visible_tabs = grep { $_->{type} eq 'page' && $_->{targetId} } @tabs;
Condition0%0/3
$_->{'type'} eq 'page' && $_->{'targetId'}
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'type'} eq 'page' && $_->{'targetId'}
$_->{'type'} eq 'page' $_->{'targetId'}
Truth table0%0/3
A && B
A= $_->{'type'} eq 'page'B= $_->{'targetId'}
ABresult
0X0
100
111
359 }
360 0 ▶ if( ! @visible_tabs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @visible_tabs) { } 0 0 0
361 $res = $s->createTarget(
362 0 ▶ url => $args{ start_url } || 'about:blank',
Condition0%0/2
$args{'start_url'} || 'about:blank'
l!l
00
MC/DC0%0/1
$args{'start_url'} || 'about:blank'
$args{'start_url'}
Truth table0%0/2
A
A= $args{'start_url'}
Aresult
11
00
363 );
364 } else {
365 0 $res = Future->done( $visible_tabs[$args{ tab }] );
366 };
367 0 ▶ $res = $res->then(sub($tab) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
368 0 $s->tab( $tab );
369 0 $s->attach( $s->tab->{targetId} );
370 0 });
371 0 });
372  
373 } elsif( $args{ tab } ) {
374 # Let's assume that the tab is the tab id:
375 0 ▶ $done = $done->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
376 $s->getTargetInfo( $args{tab})
377 0 ▶ })->then(sub( $tab ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
378 0 $s->tab($tab);
379 0 $s->attach( $tab->{targetId});
380 0 });
381  
382 } else {
383 # Attach to the first available tab we find
384 0 ▶ $done = $done->then(sub (@) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
385 0 $s->getTargets()
386 0 ▶ })->then(sub( @tabs ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
387 0 ▶ (my $tab) = grep { $_->{type} eq 'page' && $_->{targetId} } @tabs;
Condition0%0/3
$_->{'type'} eq 'page' && $_->{'targetId'}
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'type'} eq 'page' && $_->{'targetId'}
$_->{'type'} eq 'page' $_->{'targetId'}
Truth table0%0/3
A && B
A= $_->{'type'} eq 'page'B= $_->{'targetId'}
ABresult
0X0
100
111
388 0 $s->tab($tab);
389 $s->attach( $tab->{targetId} )
390 0 });
391 };
392  
393 0 $done
394 };
395  
396 =head2 C<< ->close >>
397  
398 $driver->close();
399  
400 Shut down the connection to our tab and close it.
401  
402 =cut
403  
404 0 ▶sub close( $self ) {
Subroutine close: not called CC=2 SCAR=17.9
Pod documented
405 0 ▶ if( my $t = $self->transport) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $self->transport) 0 0 0
406 0 $t->closeTarget(targetId => $self->targetId );
407 }
408 }
409  
410 0 ▶sub DESTROY( $self ) {
Subroutine DESTROY: not called CC=2 SCAR=17.9
411 0 ▶ if( $self->autoclose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autoclose) 0 0 0
412 0 ▶ $self->close->catch(sub {})->retain;
Subroutine __ANON__: not called CC=1 SCAR=6.9
413 }
414 };
415  
416 =head2 C<< ->sleep >>
417  
418 $driver->sleep(0.2)->get;
419  
420 Sleep for the amount of seconds in an event-loop compatible way
421  
422 =head2 C<< ->one_shot >>
423  
424 my $f = $driver->one_shot('Page.domContentEventFired')->get;
425  
426 Returns a future that resolves when the event is received
427  
428 =cut
429  
430 0 ▶sub one_shot( $self, @events ) {
Subroutine one_shot: not called CC=1 SCAR=6.9
Pod documented
431 0 my $result = $self->future;
432 0 my $ref = $result;
433 0 weaken $ref;
434 0 my %events;
435 0 undef @events{ @events };
436 0 push @{ $self->_one_shot }, { events => \%events, future => \$ref };
437 0 $result
438 };
439  
440 69 my %stack;
441 my $r;
442 0 ▶sub on_response( $self, $connection, $message ) {
Subroutine on_response: not called CC=30 SCAR=68.4
Pod undocumented
443 0 my $response = eval { $self->json->decode( $message ) };
444 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
445 0 $self->log('error', $@ );
446 0 warn $message;
447 0 return;
448 };
449  
450 0 ▶ if( ! exists $response->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $response->{'id'}) { } 0 0 0
451 # Generic message, dispatch that:
452 0 ▶ if( my $error = $response->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $response->{"error"}) 0 0 0
453 0 $self->log('error', "Error response from Chrome", $error );
454 0 return;
455 };
456  
457 0 ▶ (my $handler) = grep { exists $_->{events}->{ $response->{method} } and ${$_->{future}} } @{ $self->_one_shot };
Condition0%0/3
exists $_->{'events'}{$response->{'method'}} && ${$_->{'future'};}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $_->{'events'}{$response->{'method'}} && ${$_->{'future'};}
exists $_->{'events'}{$response->{'method'}} ${$_->{'future'};}
Truth table0%0/3
A && B
A= exists $_->{'events'}{$response->{'method'}}B= ${$_->{'future'};}
ABresult
0X0
100
111
458 0 my $handled;
459 0 ▶ if( $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler) 0 0 0
460 0 $self->log( 'trace', "Dispatching one-shot event", $response );
461 0 ${ $handler->{future} }->done( $response );
462  
463 # Remove the handler we just invoked
464 0 ▶ @{ $self->_one_shot } = grep { $_ and ${$_->{future}} and $_ != $handler } @{ $self->_one_shot };
Condition0%0/6
$_ && ${$_->{'future'};} && $_ != $handler
!ll&&!rl&&r
000
$_ and ${$_->{"future"};}
!ll&&!rl&&r
000
MC/DC0%0/3
$_ && ${$_->{'future'};} && $_ != $handler
$_ ${$_->{"future"};} $_ != $handler
Truth table0%0/4
A and B && C
A= $_B= ${$_->{"future"};}C= $_ != $handler
ABCresult
0XX0
10X0
1100
1111
465  
466 0 $handled++;
467 };
468  
469 0 ▶ if( my $listeners = $self->listener->{ $response->{method} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $listeners = $self->listener->{$response->{"method"}}) 0 0 0
470 0 @$listeners = grep { defined $_ } @$listeners;
471 0 ▶ if( $self->_log->is_trace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_log->is_trace) { } 0 0 0
472 0 $self->log( 'trace', "Notifying listeners", $response );
473 } else {
474 0 $self->log( 'debug', sprintf "Notifying listeners for '%s'", $response->{method} );
475 };
476 0 for my $listener (@$listeners) {
477 0 eval {
478 0 $listener->notify( $response );
479 };
480 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
481 };
482 # re-weaken our references
483 0 for (0..$#$listeners) {
484 0 weaken $listeners->[$_];
485 };
486  
487 0 $handled++;
488 };
489  
490 0 ▶ if( $self->has_subscribers('message') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_subscribers("message")) 0 0 0
491 0 ▶ if( $self->transport->_log->is_trace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->transport->_log->is_trace) { } 0 0 0
492 0 $self->log( 'trace', "Dispatching", $response );
493 } else {
494 0 my $frameId = $response->{params}->{frameId};
495 0 my $requestId = $response->{params}->{requestId};
496 0 ▶ if( $frameId || $requestId ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frameId or $requestId) { } 0 0 0
Condition0%0/3
$frameId or $requestId
l!l&&r!l&&!r
000
MC/DC0%0/2
$frameId or $requestId
$frameId $requestId
Truth table0%0/3
A or B
A= $frameIdB= $requestId
ABresult
1X1
011
000
497 0 ▶ $self->log( 'debug', sprintf "Dispatching '%s' (%s:%s)", $response->{method}, $frameId || '-', $requestId || '-');
Condition0%0/4
$frameId || '-'
l!l
00
$requestId || '-'
l!l
00
MC/DC0%0/1
$frameId || '-'
$frameId
MC/DC0%0/1
$requestId || '-'
$requestId
Truth table0%0/2
A
A= $frameId
Aresult
11
00
Truth table0%0/2
A
A= $requestId
Aresult
11
00
498 } else {
499 0 $self->log( 'debug', sprintf "Dispatching '%s'", $response->{method} );
500 };
501 };
502 0 $self->emit('message', $response );
503  
504 0 $handled++;
505 };
506  
507 0 ▶ if( ! $handled ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $handled) 0 0 0
508 0 ▶ if( $self->_log->is_trace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_log->is_trace) { } 0 0 0
509 0 $self->log( 'trace', "Ignored message", $response );
510 } else {
511 0 my $frameId = $response->{params}->{frameId};
512 0 my $requestId = $response->{params}->{requestId};
513 0 ▶ if( $frameId || $requestId ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frameId or $requestId) { } 0 0 0
Condition0%0/3
$frameId or $requestId
l!l&&r!l&&!r
000
MC/DC0%0/2
$frameId or $requestId
$frameId $requestId
Truth table0%0/3
A or B
A= $frameIdB= $requestId
ABresult
1X1
011
000
514 0 ▶ $self->log( 'debug', sprintf "Ignoring '%s' (%s:%s)", $response->{method}, $frameId || '-', $requestId || '-');
Condition0%0/4
$frameId || '-'
l!l
00
$requestId || '-'
l!l
00
MC/DC0%0/1
$frameId || '-'
$frameId
MC/DC0%0/1
$requestId || '-'
$requestId
Truth table0%0/2
A
A= $frameId
Aresult
11
00
Truth table0%0/2
A
A= $requestId
Aresult
11
00
515 } else {
516 0 $self->log( 'debug', sprintf "Ignoring '%s'", $response->{method} );
517 };
518 };
519  
520 };
521 } else {
522  
523 0 my $id = $response->{id};
524 0 my $receiver = delete $self->{receivers}->{ $id };
525  
526 0 ▶ if( ! $receiver) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $receiver) { } 0 0 0
elsif ($receiver eq 'ignore') { } 0 0 0
elsif ($response->{'error'}) { } 0 0 0
527 0 $self->log( 'debug', "Ignored response to unknown receiver", $response )
528  
529 } elsif( $receiver eq 'ignore') {
530 # silently ignore that reply
531  
532 } elsif( $response->{error} ) {
533 0 $self->log( 'debug', "Replying to error $response->{id}", $response );
534 # It would be nice if Future had ->croak(), so we could report
535 # the error on the line that originally called us maybe
536 0 ▶ $receiver->die( join "\n", $response->{error}->{message},$response->{error}->{data} // '',$response->{error}->{code} // '');
Condition0%0/4
$response->{"error"}{"data"} // ""
l!l
00
$response->{"error"}{"code"} // ""
l!l
00
MC/DC0%0/1
$response->{"error"}{"data"} // ""
$response->{"error"}{"data"}
MC/DC0%0/1
$response->{"error"}{"code"} // ""
$response->{"error"}{"code"}
Truth table0%0/2
A
A= $response->{"error"}{"data"}
Aresult
11
00
Truth table0%0/2
A
A= $response->{"error"}{"code"}
Aresult
11
00
537 } else {
538 0 $self->log( 'trace', "Replying to $response->{id}", $response );
539 0 $receiver->done( $response->{result} );
540 };
541 };
542 }
543  
544 0 ▶sub next_sequence( $self ) {
Subroutine next_sequence: not called CC=1 SCAR=6.9
Pod undocumented
545 0 my( $val ) = $self->current_sequence;
546 0 $self->sequence_number( $val+1 );
547 0 $val
548 };
549  
550 0 ▶sub current_sequence( $self ) {
Subroutine current_sequence: not called CC=1 SCAR=6.9
Pod undocumented
551 0 $self->sequence_number
552 };
553  
554 0 ▶sub build_url( $self, %options ) {
Subroutine build_url: not called CC=1 SCAR=6.9
Pod undocumented
555 0 croak "$self can't build URLs";
556 };
557  
558 =head2 C<< $chrome->json_get >>
559  
560 my $data = $driver->json_get( 'version' )->get;
561  
562 Requests an URL and returns decoded JSON from the future
563  
564 =cut
565  
566 0 ▶sub json_get($self, $domain, %options) {
Subroutine json_get: not called CC=1 SCAR=6.9
Pod documented
567 0 croak "$self can't GET JSON data";
568 };
569  
570 =head2 C<< $chrome->version_info >>
571  
572 print $chrome->version_info->get->{"protocolVersion"};
573  
574 =cut
575  
576 0 ▶sub version_info( $self ) {
Subroutine version_info: not called CC=1 SCAR=6.9
Pod documented
577 0 $self->getVersion
578 }
579  
580 =head2 C<< $chrome->protocol_version >>
581  
582 print $chrome->protocol_version->get;
583  
584 =cut
585  
586 0 ▶sub protocol_version( $self ) {
Subroutine protocol_version: not called CC=1 SCAR=6.9
Pod documented
587 0 ▶ $self->getVersion->then(sub( $info ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
588 0 Future->done($info->{"protocolVersion"})
589 })
590 0 }
591  
592 0 ▶sub _send_packet( $self, $response, $method, %params ) {
Subroutine _send_packet: not called CC=3 SCAR=24.8
593 0 my $id = $self->next_sequence;
594 0 ▶ if( $response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 0 0 0
595 0 $self->{receivers}->{ $id } = $response;
596 };
597  
598 0 my $s = $self;
599 0 weaken $s;
600  
601 0 my $payload = eval {
602 0 $s->json->encode({
603 id => 0+$id,
604 method => $method,
605 params => \%params
606 });
607 };
608 0 ▶ if( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
609 0 $s->log('error', $@ );
610 0 $s->log('error', Dumper \%params );
611 };
612  
613 0 $s->log( 'trace', "Sent message", $payload );
614 0 my $result;
615 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
616 # this is half right - we get an ack when the message was accepted
617 # but we want to send the real reply when it comes back from the
618 # real target. This is done in the listener for receivedMessageFromTarget
619 #my $ignore = $s->future->retain;
620 0 $result = $s->transport->_send_packet(
621 #$ignore, # this one leads to a circular reference somewhere when using AnyEvent backends?!
622 'ignore',
623 'Target.sendMessageToTarget',
624 message => $payload,
625 targetId => $s->targetId,
626 maybe sessionId => $s->sessionId,
627 );
628 0 $result->set_label('Target.sendMessageToTarget');
629 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
630 0 $s->log('error', $_ );
631 0 $result = Future->fail( $_ );
632 0 };
633 0 return $result
634 }
635  
636 =head2 C<< $chrome->send_packet >>
637  
638 $chrome->send_packet('Page.handleJavaScriptDialog',
639 accept => JSON::true,
640 );
641  
642 Sends a JSON packet to the remote end
643  
644 =cut
645  
646 0 ▶sub send_packet( $self, $topic, %params ) {
Subroutine send_packet: not called CC=1 SCAR=6.9
Pod documented
647 0 $self->_send_packet( undef, $topic, %params )
648 }
649  
650 =head2 C<< $chrome->send_message >>
651  
652 my $future = $chrome->send_message('DOM.querySelectorAll',
653 selector => 'p',
654 nodeId => $node,
655 );
656 my $nodes = $future->get;
657  
658 This function expects a response. The future will not be resolved until Chrome
659 has sent a response to this query.
660  
661 =cut
662  
663 0 ▶sub send_message( $self, $method, %params ) {
Subroutine send_message: not called CC=1 SCAR=6.9
Pod documented
664 0 my $response = $self->future;
665 # We add our response listener before we've even sent our request to
666 # Chrome. This ensures that no amount of buffering etc. will make us
667 # miss a reply from Chrome to a request
668 0 my $f = $self->_send_packet( $response, $method, %params )->retain;
669 0 $response
670 }
671  
672 =head2 C<< $chrome->callFunctionOn >>
673  
674 =cut
675  
676 0 ▶sub callFunctionOn( $self, $function, %options ) {
Subroutine callFunctionOn: not called CC=1 SCAR=6.9
Pod documented
677 $self->send_message('Runtime.callFunctionOn',
678 functionDeclaration => $function,
679 returnByValue => JSON::true,
680 arguments => $options{ arguments },
681 objectId => $options{ objectId },
682 0 %options
683 )
684 };
685  
686 =head2 C<< $chrome->evaluate >>
687  
688 =cut
689  
690 0 ▶sub evaluate( $self, $string, %options ) {
Subroutine evaluate: not called CC=1 SCAR=6.9
Pod documented
691 0 $self->send_message('Runtime.evaluate',
692 expression => $string,
693 returnByValue => JSON::true,
694 %options
695 )
696 };
697  
698 =head2 C<< $chrome->eval >>
699  
700 =cut
701  
702 0 ▶sub eval( $self, $string ) {
Subroutine eval: not called CC=1 SCAR=6.9
Pod documented
703 0 ▶ $self->evaluate( $string )->then(sub( $result ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
704 Future->done( $result->{result}->{value} )
705 0 });
706 };
707  
708 =head2 C<< $chrome->get_domains >>
709  
710 =cut
711  
712 0 ▶sub get_domains( $self ) {
Subroutine get_domains: not called CC=1 SCAR=6.9
Pod documented
713 0 $self->send_message('Schema.getDomains');
714 }
715  
716 =head2 C<< $chrome->list_tabs >>
717  
718 my @tabs = $chrome->list_tabs->get();
719  
720 =cut
721  
722 0 ▶sub list_tabs( $self, $type = 'page' ) {
Subroutine list_tabs: not called CC=2 SCAR=17.9
Pod documented
723 0 croak "Won't list tabs, even though I could";
724 };
725  
726 =head2 C<< $chrome->new_tab >>
727  
728 my $new_tab = $chrome->new_tab('https://www.google.com')->get;
729  
730 =cut
731  
732 0 ▶sub new_tab( $self, $url=undef ) {
Subroutine new_tab: not called CC=2 SCAR=17.9
Pod documented
733 0 croak "Won't create tabs, even though I could";
734 };
735  
736 =head2 C<< $chrome->activate_tab >>
737  
738 $chrome->activate_tab( $tab )->get
739  
740 Brings the tab to the foreground of the application
741  
742 =cut
743  
744 0 ▶sub activate_tab( $self, $tab=undef ) {
Subroutine activate_tab: not called CC=4 SCAR=30.0
Pod documented
745 0 ▶ my $id = ref $tab ? $tab->{targetId} : $tab;
Branch: 1 branch
BranchTrueFalseTotal
ref $tab ? : 0 0 0
746 0 ▶ $id ||= $self->targetId;
Condition0%0/3
$id ||= $self->targetId
l!l&&r!l&&!r
000
MC/DC0%0/2
$id ||= $self->targetId
$id $self->targetId
Truth table0%0/3
A ||= B
A= $idB= $self->targetId
ABresult
1X1
011
000
747 0 $self->transport->send_message('Target.activateTarget', targetId => $id );
748 };
749  
750 =head2 C<< $target->getTargetInfo >>
751  
752 Returns information about the current target
753  
754 =cut
755  
756 0 ▶sub getTargetInfo( $self, $targetId = $self->targetId ) {
Subroutine getTargetInfo: not called CC=2 SCAR=17.9
Pod documented
757 0 ▶ $self->transport->getTargetInfo( $targetId )->then(sub( $info ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
758 0 Future->done( $info )
759 0 });
760 }
761  
762 =head2 C<< $target->info >>
763  
764 Returns information about the current target
765  
766 =cut
767  
768 0 ▶sub info( $self ) {
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
769 0 $self->getTargetInfo( $self->targetId )->get
770 }
771  
772 =head2 C<< $target->title >>
773  
774 Returns the title of the current target
775  
776 =cut
777  
778 0 ▶sub title( $self ) {
Subroutine title: not called CC=1 SCAR=6.9
Pod documented
779 $self->getTargetInfo( $self->targetId )->get->{title}
780 0 }
781  
782 =head2 C<< $target->getVersion >>
783  
784 Returns information about the Chrome instance we are connected to.
785  
786 =cut
787  
788 0 ▶sub getVersion( $self ) {
Subroutine getVersion: not called CC=1 SCAR=6.9
Pod documented
789 0 $self->send_message( 'Browser.getVersion' )
790 }
791  
792 =head2 C<< $target->createTarget >>
793  
794 my $info = $chrome->createTarget(
795 url => 'about:blank',
796 width => 1280,
797 height => 800,
798 newWindow => JSON::false,
799 background => JSON::false,
800 )->get;
801 print $info->{targetId};
802  
803 Creates a new target
804  
805 =cut
806  
807 0 ▶sub createTarget( $self, %options ) {
Subroutine createTarget: not called CC=2 SCAR=17.9
Pod documented
808 0 ▶ $options{ url } //= 'about:blank';
Condition0%0/2
$options{"url"} //= "about:blank"
l!l
00
MC/DC0%0/1
$options{"url"} //= "about:blank"
$options{"url"}
Truth table0%0/2
A
A= $options{"url"}
Aresult
11
00
809 $self->transport->send_message('Target.createTarget',
810 0 ▶ %options )->then(sub( $info ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
811 0 Future->done( $info )
812 0 });
813 }
814  
815  
816 =head2 C<< $target->attach >>
817  
818 $target->attach();
819  
820 Attaches to the target set up in C<targetId> and C<sessionId>. If a targetId is
821 given, attaches to it and remembers the value.
822  
823 =cut
824  
825 0 ▶sub attach( $self, $targetId=$self->targetId ) {
Subroutine attach: not called CC=2 SCAR=17.9
Pod documented
826 0 my $s = $self;
827 0 weaken $s;
828 0 $self->targetId( $targetId );
829  
830 0 ▶ $self->{have_target_info} = $self->transport->one_shot('Target.attachedToTarget')->then(sub($r) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
831 # #$s->log('trace', "Attached to", $r );
832 #use Data::Dumper; warn Dumper $r;
833 #$s->browserContextId($r->{params}->{targetInfo}->{browserContextId});
834 # #$s->sessionId( $target->{sessionId});
835 # #$s->log('debug', "Attached to session $target->{sessionId}" );
836 # #undef $s->{have_session};
837 0 return Future->done;
838 0 })->retain;
839  
840 $self->transport->attachToTarget( targetId => $targetId )
841 0 ▶ ->on_done(sub( $sessionId ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
842 0 $s->sessionId( $sessionId );
843 0 $s->log('debug', "Attached to tab $targetId, session $sessionId" );
844 0 });
845 };
846  
847 69 1;
848  
849 =head1 SEE ALSO
850  
851 The unofficial Chrome debugger API documentation at
852 L<https://github.com/buggerjs/bugger-daemon/blob/master/README.md#api>
853  
854 Chrome DevTools at L<https://chromedevtools.github.io/devtools-protocol/1-2>
855  
856 =head1 REPOSITORY
857  
858 The public repository of this module is
859 L<https://github.com/Corion/www-mechanize-chrome>.
860  
861 =head1 SUPPORT
862  
863 The public support forum of this module is L<https://perlmonks.org/>.
864  
865 =head1 BUG TRACKER
866  
867 Please report bugs in this module via the Github bug queue at
868 L<https://github.com/Corion/WWW-Mechanize-Chrome/issues>
869  
870 =head1 AUTHOR
871  
872 Max Maischein C<corion@cpan.org>
873  
874 =head1 COPYRIGHT (c)
875  
876 Copyright 2010-2026 by Max Maischein C<corion@cpan.org>.
877  
878 =head1 LICENSE
879  
880 This module is released under the same terms as Perl itself.
881  
882 =cut
« WWW/Mechanize/Chrome.pm ↑ Summary Chrome/DevToolsProtocol.pm »