lib/WWW/Mechanize/Chrome/Webshot.pm

filter: Statementstmt 53.6% 249 / 464 Branchbran 36.3% 85 / 234 Conditioncond 31.6% 58 / 183 MC/DCmcdc 3.6% 4 / 109 Subroutinesub 86.4% 32 / 37 Podpod 54.5% 12 / 22 totaltotal 41.9% 440 / 1049 CC 192 SCAR 66.7
CC
192
Cov
44%
shoot
1471.0
init
1353.4
_check_if_exif_tags_exist_in_image
30.0
CRAP 791.4
↑ Summary script/www-mechanize-webshot.pl »
1 package WWW::Mechanize::Chrome::Webshot;
2  
3 #### warning, size is 100,100 and not 100x100!!!
4  
5 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #use Exporter qw(import);
10 #our @EXPORT = qw(
11 # _check_if_exif_tags_exist_in_image
12 #);
13  
14 4 our $VERSION = '0.05';
15  
16 4 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 # >= v5.31 which supports EXIF for png files
18 4 ▶use Image::ExifTool qw/ :Public /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use WWW::Mechanize::Chrome;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Encode qw/encode_utf8 decode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Config::JSON::Enhanced;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use WWW::Mechanize::Chrome::DOMops qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
26 domops_zap
27 $domops_VERBOSITY
28 $domops_LOGGER
29 4 /;
30  
31 # a basic default configuration file in enhanced JSON (see L<Config::JSON::Enhanced>)
32 4 my $DEFAULT_CONFIGSTRING = <<'EOCS';
33 </* $VERSION = '0.05'; */>
34 </* comments are allowed */>
35 </* and <% vars %> and <% verbatim sections %> */>
36 {
37 "debug" : {
38 "verbosity" : 1,
39 </* cleanup temp files on exit */>
40 "cleanup" : 1
41 },
42 "logger" : {
43 </* log to file if you uncomment this */>
44 </* "filename" : "..." */>
45 },
46 "constructor" : {
47 </* for slow connections */>
48 "settle-time" : "3",
49 "resolution" : "1600x1200",
50 "stop-on-error" : "0",
51 "remove-dom-elements" : []
52 },
53 "WWW::Mechanize::Chrome" : {
54 "headless" : "1",
55 "launch_arg" : [
56 </* this will change as per the 'resolution' setting above */>
57 "--window-size=600x800",
58 "--password-store=basic", </* do not ask me for stupid chrome account password */>
59 </* "--remote-debugging-port=9223", */>
60 </* "--enable-logging", */>
61 "--disable-gpu",
62 </* "--no-sandbox", NO LONGER VALID */>
63 "--ignore-certificate-errors",
64 "--disable-background-networking",
65 "--disable-client-side-phishing-detection",
66 "--disable-component-update",
67 "--disable-hang-monitor",
68 "--disable-save-password-bubble",
69 "--disable-default-apps",
70 "--disable-infobars",
71 "--disable-popup-blocking"
72 ]
73 }
74 }
75 EOCS
76  
77 3 ▶sub new {
Subroutine new: called CC=5 SCAR=17.6
Pod documented
78 3 my $class = $_[0];
79 3 my $params = $_[1]; # a hash of params, see below
80  
81 3 ▶ my $parent = ( caller(1) )[3] || "N/A";
Condition50%1/2
(caller 1)[3] || 'N/A'
l!l
03
MC/DC0%0/1
(caller 1)[3] || 'N/A'
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
82 3 my $whoami = ( caller(0) )[3];
83  
84 3 my $self = {
85 '_private' => {
86 'confighash' => undef,
87 'configfile' => '', # this should never be undef
88 'debug' => {
89 'verbosity' => 0,
90 'cleanup' => 1,
91 },
92 'log' => {
93 'logger-object' => undef,
94 'logfile' => undef
95 },
96 'settle-time' => 5,
97 'resolution' => '1000,1002', # not x but , !!!
98 'stop-on-error' => 0,
99 'remove-dom-elements' => [],
100 # this is a global exif metadata to be set
101 # each time we shoot() UNLESSS shoot()'s
102 # params have their own, in which case this is not used:
103 'exif' => undef,
104 # this may be instantiated lazily,
105 # when needed as mech is expensive to load
106 # or it can be passed on to us via the constructor
107 'WWW::Mechanize::Chrome-object' => undef,
108 # these are the final mech params,
109 # from confighash and user-specified $params
110 'WWW::Mechanize::Chrome-params' => undef,
111 }
112 };
113 3 bless $self => $class;
114  
115 # this will read configuration and create confighash,
116 # make logger, verbosity,
117 # instantiate any objects we need here etc.
118 0 ▶ if( $self->init($params) ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, call to init() has failed.\n"; return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->init($params)) 0 3 3
119  
120 # Now we have a logger
121 3 my $log = $self->log();
122  
123 # do module-specific init like instantiating the mech, unless params has 'launch-mech-on-demand'=>1
124 0 ▶ if( $self->init_module_specific($params) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to init_module_specific() has failed."); return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->init_module_specific($params)) 0 2 2
125  
126 2 my $verbosity = $self->verbosity;
127 2 ▶ if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : done, success (verbosity is set to ".$self->verbosity." and cleanup to ".$self->cleanup.").") }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbosity > 0) 2 0 2
128  
129 2 return $self;
130 }
131  
132 # blank the browser
133 0 ▶sub blank_browser { $_[0]->mech_obj->get('about:blank'); $_[0]->mech_obj->sleep(1); }
Subroutine blank_browser: not called CC=1 SCAR=6.9
Pod undocumented
134  
135 # hits the page, waits for some settle-time, removes some elements from the DOM,
136 # if asked, that obstruct the shot, e.g. banners and dumps the browser's window
137 # to a PNG or PDF file (whose filename must be supplied as input param).
138 # NOTE: PDF output does not seem to contain the whole browser content!
139 # it has a preset size.
140 # optional 'remove-dom-elements' overwrites the self if any
141 # optional 'exif' parameter (as hash) specifies tag/value pairs to be inserted as
142 # metadata into the final output image
143 # { k => v, ... }
144 # exif tagnames have restrictions: no unicode, no spaces, not ':'
145 # returns 0 on failure
146 # returns 1 on success
147 1 ▶sub shoot {
Subroutine shoot: called CC=52 SCAR=72.9
Pod documented
148 1 my $self = $_[0];
149 1 my $params = $_[1]; # a hashref of 'output-filename', 'url' etc.
150 1 ▶ my $parent = ( caller(1) )[3] || "N/A";
Condition50%1/2
(caller 1)[3] || 'N/A'
l!l
01
MC/DC0%0/1
(caller 1)[3] || 'N/A'
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
151 1 my $whoami = ( caller(0) )[3];
152 1 my $verbos = $self->verbosity;
153 1 my $log = $self->log;
154  
155 # this is required
156 1 ▶ my $outfile = exists($params->{'output-filename'}) ? $params->{'output-filename'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{'output-filename'} ? : 1 0 1
157 0 ▶ if( ! defined $outfile ){ $log->error("$whoami (via $parent), line ".__LINE__." : error, 'output-filename' parameter was not specified."); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $outfile) 0 1 1
158  
159 1 my $outformat;
160 1 ▶ if( exists($params->{'output-format'}) && defined($params->{'output-format'}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{'output-format'} and defined $params->{'output-format'}) { } 0 1 1
Condition33%1/3
exists $params->{"output-format"} and defined $params->{"output-format"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $params->{"output-format"} and defined $params->{"output-format"}
exists $params->{"output-format"} defined $params->{"output-format"}
Truth table33%1/3
A and B
A= exists $params->{"output-format"}B= defined $params->{"output-format"}
ABresult
0X0
100
111
161 0 ▶ if( $params->{'output-format'} =~ /^(png|pdf)$/ ){ $outformat = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'output-format'} =~ /^(png|pdf)$/) { } 0 0 0
162 0 else { $log->error("$whoami (via $parent), line ".__LINE__." : error, parameter 'output-format' was specified as '".$params->{'output-format'}."' but one of 'png' or 'pdf' was expected."); return 0 }
163 } else {
164 # try and deduce the format from the file extension
165 1 ▶ if( $outfile =~ /\.(pdf|png)$/i ){ $outformat = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile =~ /\.(pdf|png)$/i) { } 1 0 1
166 0 else { $log->error("$whoami (via $parent), line ".__LINE__." : error, parameter 'output-format' was not specified and failed to deduce the output format from the specified output filename ($outfile). Either specify 'output-format' as one of 'png' or 'pdf' or make the 'output-filename' clearer."); return 0 }
167 }
168  
169 # this is optional, because mech already may be in current page
170 # if you will use URI for local file (file://...) then
171 # USE ABSOLUTE FILEPATH
172 1 ▶ my $URL = exists($params->{'url'}) ? $params->{'url'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{'url'} ? : 1 0 1
173  
174 # optional or default
175 my $stoperr = (exists($params->{'stop-on-error'}) && defined($params->{'stop-on-error'}))
176 1 ▶ ? $params->{'stop-on-error'}
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{'stop-on-error'} && defined $params->{'stop-on-error'} ? : 0 1 1
Condition33%1/3
exists $params->{"stop-on-error"} and defined $params->{"stop-on-error"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $params->{"stop-on-error"} and defined $params->{"stop-on-error"}
exists $params->{"stop-on-error"} defined $params->{"stop-on-error"}
Truth table33%1/3
A and B
A= exists $params->{"stop-on-error"}B= defined $params->{"stop-on-error"}
ABresult
0X0
100
111
177 : $self->stop_on_error()
178 ;
179  
180 # optional
181 my $remove_dom_elements = (exists($params->{'remove-dom-elements'}) && defined($params->{'remove-dom-elements'}))
182 ? $params->{'remove-dom-elements'}
183 : (exists($self->{'_private'}->{'remove-dom-elements'}) && defined($self->{'_private'}->{'remove-dom-elements'}))
184 1 ▶ ? $self->{'_private'}->{'remove-dom-elements'}
Branch: 2 branches
BranchTrueFalseTotal
exists $params->{'remove-dom-elements'} && defined $params->{'remove-dom-elements'} ? : 0 1 1
exists $self->{'_private'}{'remove-dom-elements'} && defined $self->{'_private'}{'remove-dom-elements'} ? : 1 0 1
Condition33%2/6
exists $params->{"remove-dom-elements"} and defined $params->{"remove-dom-elements"}
!ll&&!rl&&r
100
exists $self->{"_private"}{"remove-dom-elements"} and defined $self->{"_private"}{"remove-dom-elements"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $params->{"remove-dom-elements"} and defined $params->{"remove-dom-elements"}
exists $params->{"remove-dom-elements"} defined $params->{"remove-dom-elements"}
MC/DC0%0/2
exists $self->{"_private"}{"remove-dom-elements"} and defined $self->{"_private"}{"remove-dom-elements"}
exists $self->{"_private"}{"remove-dom-elements"} defined $self->{"_private"}{"remove-dom-elements"}
Truth table33%1/3
A and B
A= exists $params->{"remove-dom-elements"}B= defined $params->{"remove-dom-elements"}
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= exists $self->{"_private"}{"remove-dom-elements"}B= defined $self->{"_private"}{"remove-dom-elements"}
ABresult
0X0
100
111
185 : undef
186 ;
187  
188 # optional
189 1 my $exif;
190 1 ▶ if( exists($params->{'exif'}) && defined($params->{'exif'}) ){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params->{'exif'} and defined $params->{'exif'}) { } 1 0 1
elsif (exists $self->{'_private'}{'exif'} and defined $self->{'_private'}{'exif'} and scalar keys %{$$self{'_private'}{'exif'};} > 0) { } 0 0 0
Condition33%1/3
exists $params->{"exif"} and defined $params->{"exif"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $params->{"exif"} and defined $params->{"exif"}
exists $params->{"exif"} defined $params->{"exif"}
Truth table33%1/3
A and B
A= exists $params->{"exif"}B= defined $params->{"exif"}
ABresult
0X0
100
111
191 1 $exif = $params->{'exif'};
192 0 ▶ } elsif( exists($self->{'_private'}->{'exif'}) && defined($self->{'_private'}->{'exif'}) && (scalar(keys %{ $self->{'_private'}->{'exif'} })>0) ){
Condition0%0/6
exists $self->{"_private"}{"exif"} and defined $self->{"_private"}{"exif"} and scalar keys %{$$self{"_private"}{"exif"};} > 0
!ll&&!rl&&r
000
exists $self->{"_private"}{"exif"} and defined $self->{"_private"}{"exif"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $self->{"_private"}{"exif"} and defined $self->{"_private"}{"exif"} and scalar keys %{$$self{"_private"}{"exif"};} > 0
exists $self->{"_private"}{"exif"} defined $self->{"_private"}{"exif"} scalar keys %{$$self{"_private"}{"exif"};} > 0
Truth table0%0/4
A and B and C
A= exists $self->{"_private"}{"exif"}B= defined $self->{"_private"}{"exif"}C= scalar keys %{$$self{"_private"}{"exif"};} > 0
ABCresult
0XX0
10X0
1100
1111
193 0 $exif = $self->{'_private'}->{'exif'};
194 }
195 1 ▶ if( defined $exif ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exif) 1 0 1
196 # check that the exif data format is valid : { k => v, ... }
197 0 ▶ if( ref($exif)ne'HASH' ){ $log->error("$whoami (via $parent), line ".__LINE__." : error, 'exif' parameter expects a HASH of tagname => tagvalue pairs, not ".ref($exif)."."); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $exif ne "HASH") 0 1 1
198 }
199  
200 # here we can check if we have a mech instantiated or not
201 # if there is no mech, we WARN if no URL was specified!
202 # where is it going to go? to the homepage of the browser, that's all
203 # we are instantiating the mech, if no url was specified then what to print? perhaps homepage?
204 1 ▶ if( ! defined($self->{'_private'}->{'WWW::Mechanize::Chrome-object'})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"} or defined $URL) 0 1 1
Condition33%1/3
defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"} or defined $URL
l!l&&r!l&&!r
010
MC/DC0%0/2
defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"} or defined $URL
defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"} defined $URL
Truth table33%1/3
A or B
A= defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"}B= defined $URL
ABresult
1X1
011
000
205 && ! defined($URL)
206 0 ){ $log->info("$whoami (via $parent), line ".__LINE__." : warning, mech is being instantiated now but there was no 'url' parameter specified, are you sure?") }
207  
208 1 my $mech = $self->mech_obj;
209  
210 # if 2 consecutive urls have the same path (except the params) then we get stuck
211 # with error Page.navigatedWithinDocument
212 # see https://perlmonks.org/?node_id=1219646
213 # let's see if this bug appears again, we need to operate even without URL
214 # perhaps mech page is already in a satate and don't want to move out and in again!
215 #$self->blank_browser();
216  
217 # and then get the page
218 0 ▶ if( defined($URL) && ! $mech->get($URL) ){ $log->error("$whoami (via $parent), line ".__LINE__." : call to ".'get()'." has failed for url: $URL"); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $URL and not $mech->get($URL)) 0 0 0
Condition0%0/3
defined $URL and not $mech->get($URL)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $URL and not $mech->get($URL)
defined $URL not $mech->get($URL)
Truth table0%0/3
A and not(B)
A= defined $URLB= not $mech->get($URL)
ABresult
0X0
100
111
219  
220 # move by nothing in order to load images
221 0 $self->scroll(0,0);
222  
223 # sleep a second or two till it gets settled
224 0 ▶ if( $verbos > 0 ){ $log->info("$whoami (via $parent), line ".__LINE__." : allowing settle time of ".$self->{'settle-time'}." seconds."); }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbos > 0) 0 0 0
225 0 sleep($self->settle_time());
226 0 ▶ if( $verbos > 0 ){ $log->info("$whoami (via $parent), line ".__LINE__." : woke up after settle time of ".$self->{'settle-time'}." seconds."); }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbos > 0) 0 0 0
227  
228 # move by nothing in order to load images
229 0 $self->scroll(0,0);
230  
231 # do we have any elements to remove?
232 0 ▶ if( defined $remove_dom_elements ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $remove_dom_elements) 0 0 0
233 0 foreach my $anentry (@$remove_dom_elements){
234 0 ▶ if( ! defined $anentry ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $anentry) 0 0 0
235 0 ▶ if( $verbos > 0 ){ $log->info("$whoami (via $parent), line ".__LINE__." : found undef in array of 'remove-dom-elements' which signifies that I should skip the rest (if any)."); }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbos > 0) 0 0 0
236 # any undef in the array is a signal to skip the rest
237 last
238 0 }
239 0 ▶ if( $verbos > 0 ){ $log->info("$whoami (via $parent), line ".__LINE__." : removing DOM element(s):\n".perl2dump($anentry)); }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbos > 0) 0 0 0
240 0 my %delparms = (
241 %$anentry,
242 'mech-obj' => $mech,
243 );
244 0 my $zapret = WWW::Mechanize::Chrome::DOMops::domops_zap(\%delparms);
245 0 ▶ if( $zapret->{'status'} < 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($zapret->{"status"} < 0) 0 0 0
246 0 $log->error(perl2dump($zapret)."$whoami (via $parent), line ".__LINE__." : call to ".'WWW::Mechanize::Chrome::DOMOps::domops_zap()'." has failed, with above error, for these parameters:\n".perl2dump($anentry)."\n---end of parameters.");
247 0 ▶ if( $stoperr > 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($stoperr > 0) { } 0 0 0
248 0 $log->error("$whoami (via $parent), line ".__LINE__." : stopping now because 'stop-on-error' was set.");
249 0 return 0 # do not continue if error!
250 0 } else { next }
251 }
252 0 ▶ if( $verbos > 1 ){ $log->info("$whoami (via $parent), line ".__LINE__." : done zapped DOM element :\n".perl2dump($anentry)) }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbos > 1) 0 0 0
253 }
254 }
255  
256 # settle for a bit before taking the screenshot
257 0 sleep($self->settle_time());
258  
259 0 ▶ if( $verbos > 0 ){ $log->info("$whoami (via $parent), line ".__LINE__." : taking a screenshot and dumping it to file '$outfile' ...") }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbos > 0) 0 0 0
260  
261 # smile!!! (or say cheese)
262 0 my $fh;
263 0 ▶ if( ! open $fh, '>:raw', $outfile ){ $log->error("$whoami (via $parent), line ".__LINE__." : error, failed to open file '$outfile' for writing, $!"); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, ">:raw", $outfile) 0 0 0
264 0 ▶ print $fh ($outformat eq 'pdf' ? $mech->content_as_pdf(format=>'A4') : $mech->content_as_png());
Branch: 1 branch
BranchTrueFalseTotal
$outformat eq 'pdf' ? : 0 0 0
265 0 close $fh;
266  
267 0 ▶ if( $exif ){
Branch: 1 branch
BranchTrueFalseTotal
if ($exif) 0 0 0
268 # WARNING: exif keys do not like unicode,
269 # neither special chars (space, '::' etc.)
270 # exif values can have anything, including unicode
271  
272 # if caller supplied exif as an ARRAY, then here we go
273 # { k => v, ... }
274  
275 0 my $exifTool = Image::ExifTool->new();
276 0 ▶ if( ! defined $exifTool ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $exifTool) 0 0 0
277 0 $log->error("$whoami (via $parent), line ".__LINE__." : error, failed to instantiate an ".'Image::ExifTool'." object. Continuing with the rest - output is saved but exif data will be missing ...");
278 0 goto FINISH; # mummy look! they are using gotos!!!
279 }
280 # Add custom user-defined tags:
281 # Gosh! https://exiftool.org/forum/index.php?topic=7377.0
282 0 my %tagnames = map { $_ => { Name => $_, Writable => 'string' } } keys %$exif;
283 0 %Image::ExifTool::UserDefined = ( 'Image::ExifTool::XMP::xmp' => { %tagnames } );
284  
285 0 for my $tn (keys %$exif){
286 0 my $tv = $exif->{$tn};
287 0 my @rs = $exifTool->SetNewValue($tn, $tv);
288 0 ▶ if( $rs[0] == 0 ){ $log->error("$whoami (via $parent), line ".__LINE__." : error, failed to add tag '${tn}' => '${tv}', ignoring this error and continuing with the rest - output is saved but exif data may be missing. Error: ".$rs[1]."") }
Branch: 1 branch
BranchTrueFalseTotal
if ($rs[0] == 0) 0 0 0
289 }
290 0 my $ws = $exifTool->WriteInfo($outfile);
291 0 ▶ if( $ws == 0 ){ $log->error(perl2dump($exif)."$whoami (via $parent), line ".__LINE__." : error, failed to write above EXIF data to output file '$outfile'. Ignoring this and continuing with the rest - output is saved but exif data will be missing ...") }
Branch: 2 branches
BranchTrueFalseTotal
if ($ws == 0) { } 0 0 0
elsif ($ws == 2) { } 0 0 0
292 0 elsif( $ws == 2 ){ $log->error(perl2dump($exif)."$whoami (via $parent), line ".__LINE__." : warning, there were no EXIF data changes to be written to the output file '$outfile' (see the data above). None of the user-specified exif data seems to have been written because there was probably none.") }
293  
294 # do a check? if not paranoid, comment this out:
295 0 my $have_errors = 0;
296 0 my $retc = WWW::Mechanize::Chrome::Webshot::_check_if_exif_tags_exist_in_image($outfile, $exif, $log);
297 0 ▶ if( ! defined($retc) ){ $log->error("$whoami (via $parent), line ".__LINE__." : warning, call to ".'_check_if_exif_tags_exist_in_image()'." has failed. Ignoring this error and continuing with the rest - output is saved but exif data may be missing ...") }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $retc) { } 0 0 0
298 else {
299 0 for my $tn (keys %$exif){
300 0 ▶ if( ! exists($retc->{$tn}) ){ $log->error("$whoami (via $parent), line ".__LINE__." : warning, tagname '${tn}' is not contained in the output image as it should."); $have_errors = 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $retc->{$tn}) 0 0 0
301 0 ▶ if( $retc->{$tn} == 0 ){ $log->error("$whoami (via $parent), line ".__LINE__." : warning, tagname '${tn}' is not contained in the output image. Ignoring this and continuing with the rest - output is saved but exif data may be missing ..."); $have_errors = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($retc->{$tn} == 0) 0 0 0
302 0 ▶ if( $retc->{$tn} == 2 ){ $log->error("$whoami (via $parent), line ".__LINE__." : warning, tagname '${tn}' is contained in the output image but its value is not correct (expected: ".$exif->{$tn}."'). Ignoring this and continuing with the rest - output is saved but exif data may be missing ..."); $have_errors = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($retc->{$tn} == 2) 0 0 0
303 }
304 }
305 0 ▶ if( ($have_errors==0) && ($verbos > 0) ){ $log->info(perl2dump($exif)."$whoami (via $parent), line ".__LINE__." : above exif data was inserted into the screenshot image '$outfile' ...") }
Branch: 1 branch
BranchTrueFalseTotal
if ($have_errors == 0 and $verbos > 0) 0 0 0
Condition0%0/3
$have_errors == 0 and $verbos > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$have_errors == 0 and $verbos > 0
$have_errors == 0 $verbos > 0
Truth table0%0/3
A and B
A= $have_errors == 0B= $verbos > 0
ABresult
0X0
100
111
306 }
307  
308 FINISH:
309 0 return 1 # success
310 }
311  
312 # launches a mech obj given optional parameters which
313 # may overwrite some defaults we have
314 # On success, it sets mech obj in $self and also returns that same object
315 # On failure, it returns undef
316 3 ▶sub launch_mech_obj {
Subroutine launch_mech_obj: called CC=16 SCAR=32.4
Pod undocumented
317 3 my $self = $_[0];
318 3 ▶ my $params = $_[1] // {};
Condition50%1/2
$_[1] // {}
l!l
30
MC/DC0%0/1
$_[1] // {}
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
319 3 my $verbos = $self->verbosity;
320 3 my $log = $self->log;
321 # the above params have the same structure as the default below
322 # we overwrite defaults with params (if any)
323  
324 3 ▶ my $parent = ( caller(1) )[3] || "N/A";
Condition50%1/2
(caller 1)[3] || 'N/A'
l!l
30
MC/DC0%0/1
(caller 1)[3] || 'N/A'
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
325 3 my $whoami = ( caller(0) )[3];
326  
327 # note ! this will be modified by the W:M:C constructor!!!
328 3 my %my_mech_params = (
329 # some defaults which can be overriden by $params
330 headless => $self->headless(),
331 no_sandbox => 0,
332 # log => $mylogger,
333 launch_arg => [
334 # a string e.g. 800x600 WxH specified in construction
335 '--window-size='.$self->resolution(),
336 '--password-store=basic', # do not ask me for stupid chrome account password
337 # '--remote-debugging-port=9223',
338 # '--enable-logging', # see also log above
339 '--disable-gpu',
340 # '--no-sandbox', # obsolete now
341 '--ignore-certificate-errors',
342 '--disable-background-networking',
343 '--disable-client-side-phishing-detection',
344 '--disable-component-update',
345 '--disable-hang-monitor',
346 '--disable-save-password-bubble',
347 '--disable-default-apps',
348 '--disable-infobars',
349 '--disable-popup-blocking',
350 ],
351 );
352  
353 # input param 'WWW::Mechanize::Chrome-params' : specify params to launching chrome as a hashref
354 # 'launch_arg' is a hashref though (unlike its counterpart in defaults above)
355 # if it has a value like '--window-size' => '1600x1200' it will be converted to '--window-size=1600x1200'
356 # and overwrite previous setting if any in the defaults.
357 # if it starts with a /^no\s*/i then the defaults will have this option removed from it.
358 3 my $m;
359 3 foreach my $k (keys %$params){
360 6 my $v = $params->{$k};
361 6 ▶ if( ref($v) eq '' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "") 3 3 6
362 # overwrite all the scalars of the default params
363 3 $my_mech_params{$k} = $params->{$k};
364 }
365 }
366 3 ▶ if( exists $params->{'launch_arg'} && ref($m=$params->{'launch_arg'}) eq 'ARRAY' ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"launch_arg"} and ref($m = $params->{"launch_arg"}) eq "ARRAY") 3 0 3
Condition33%1/3
exists $params->{"launch_arg"} and ref($m = $params->{"launch_arg"}) eq "ARRAY"
!ll&&!rl&&r
003
MC/DC0%0/2
exists $params->{"launch_arg"} and ref($m = $params->{"launch_arg"}) eq "ARRAY"
exists $params->{"launch_arg"} ref($m = $params->{"launch_arg"}) eq "ARRAY"
Truth table33%1/3
A and B
A= exists $params->{"launch_arg"}B= ref($m = $params->{"launch_arg"}) eq "ARRAY"
ABresult
0X0
100
111
367 3 ▶ my %la = map { /=/ ? split/=/,$_ : ($_=>undef) } @$m;
Branch: 1 branch
BranchTrueFalseTotal
/=/ ? : 6 30 36
368 3 ▶ my %dla = map { /=/ ? split/=/,$_ : ($_=>undef) } @{$my_mech_params{'launch_arg'}};
Branch: 1 branch
BranchTrueFalseTotal
/=/ ? : 6 30 36
369  
370 3 foreach my $k (keys %la){
371 # it was an option starting with 'no', e.g. 'no --disable-infobars'
372 0 ▶ if( $k =~ s/^no\s*//i ){ delete $dla{$k} }
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ s/^no\s*//i) { } 0 36 36
373 36 else { $dla{$k} = $la{$k} } # else set the option or option with value
374 }
375 3 ▶ $my_mech_params{'launch_arg'} = [ map { defined($dla{$_}) ? $_.'='.$dla{$_} : $_ } keys %dla ];
Branch: 1 branch
BranchTrueFalseTotal
defined $dla{$_} ? : 6 30 36
376 }
377  
378 # from https://perlmaven.com/logging-in-modules-with-log4perl-the-easy-way
379 3 ▶ if(not Log::Log4perl->initialized()){
Branch: 1 branch
BranchTrueFalseTotal
unless ("Log::Log4perl"->initialized) 3 0 3
380 # Set priority of root logger to ERROR
381 3 Log::Log4perl->easy_init(Log::Log4perl::Level::to_priority('ERROR'));
382 }
383  
384 0 ▶ if( $verbos > 2 ){ $log->info(perl2dump(\%my_mech_params)."$whoami (via $parent), line ".__LINE__." : instantiating a WWW::Mechanize::Chrome object with above parameters ...") }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbos > 2) 0 3 3
385 3 $self->{'_private'}->{'WWW::Mechanize::Chrome-object'} = WWW::Mechanize::Chrome->new(%my_mech_params);
386 0 ▶ if( ! defined $self->{'_private'}->{'WWW::Mechanize::Chrome-object'} ){ $log->error("_create_mech_obj() : call to ".'WWW::Mechanize::Chrome->new()'." with params:\n".perl2dump(%my_mech_params)."\n---end mech launch params\n has failed, parameters to this sub (launch_mech_obj) are:\n".perl2dump($params)."\n---- end of sub parameters."); return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"}) 0 0 0
387 # at this stage the my_mech_params are different, appended by W::M::C !!!
388 # --no-sandbox exists there too!
389  
390 # now that we have a mech we need to re-do the verbosity thing
391 # to fix the mech obj too, after we created it above:
392 0 $verbos = $self->verbosity($self->verbosity);
393  
394 0 ▶ if( $verbos > 0 ){ $log->info("$whoami (via $parent), line ".__LINE__." : launched ".'WWW::Mechanize::Chrome'." with the following parameters:\n".perl2dump(\%my_mech_params)."\n--- end mech launch parameters."); }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbos > 0) 0 0 0
395  
396 0 return $self->{'_private'}->{'WWW::Mechanize::Chrome-object'};
397 }
398  
399 # scroll by x pixels and y pixels.
400 0 ▶sub scroll { $_[0]->mech_obj->eval('window.scrollBy('.$_[1].', '.$_[2].');') }
Subroutine scroll: not called CC=1 SCAR=6.9
Pod documented
401 0 ▶sub scroll_to_bottom { $_[0]->mech_obj->eval('window.scrollTo(0,document.body.scrollHeight);') }
Subroutine scroll_to_bottom: not called CC=1 SCAR=6.9
Pod documented
402 17 ▶sub verbosity {
Subroutine verbosity: called CC=4 SCAR=17.0
Pod documented
403 17 my $self = $_[0];
404 17 my $m = $_[1];
405 17 ▶ return $self->{'_private'}->{'debug'}->{'verbosity'} unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 14 3 17
406 3 $self->{'_private'}->{'debug'}->{'verbosity'} = $m;
407  
408 3 $WWW::Mechanize::Chrome::DOMops::domops_VERBOSITY = $m;
409 # make a mech console for showing js output (and other things)
410 # this will show the warning about not to paste things on the console
411 # because of XSS attacks etc. nothing to freak out.
412 $self->{'_private'}->{'debug'}->{'mech-console'} =
413 # WARNING: do not use mech_obj, because it will instantiate it
414 ( ($m > 0) && _is_mech_obj_instantiated() )
415 0 ▶ ? $self->mech_obj->add_listener('Runtime.consoleAPICalled', sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
416 warn join ", ",
417 0 ▶ map { $_->{value} // $_->{description} }
Condition0%0/3
$_->{"value"} // $_->{"description"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{"value"} // $_->{"description"}
$_->{"value"} $_->{"description"}
Truth table0%0/3
A // B
A= $_->{"value"}B= $_->{"description"}
ABresult
1X1
011
000
418 0 @{ $_[0]->{params}->{args} };
419 })
420 3 ▶ : undef # console goes away, all gone
Branch: 1 branch
BranchTrueFalseTotal
$m > 0 && _is_mech_obj_instantiated() ? : 0 3 3
Condition33%1/3
$m > 0 and _is_mech_obj_instantiated()
!ll&&!rl&&r
030
MC/DC0%0/2
$m > 0 and _is_mech_obj_instantiated()
$m > 0 _is_mech_obj_instantiated()
Truth table33%1/3
A and B
A= $m > 0B= _is_mech_obj_instantiated()
ABresult
0X0
100
111
421 ;
422 3 return $m
423 }
424  
425 # check if we have a mech obj but don't instantiate one
426 # do not use defined($self->mech_obj()) to check if we have a mech object!
427 3 ▶sub _is_mech_obj_instantiated { return defined $_[0]->{'_private'}->{'WWW::Mechanize::Chrome-object'} }
Subroutine _is_mech_obj_instantiated: called CC=1 SCAR=0.0
428  
429 # it will return the mech obj or if not defined, it will
430 # instantiate it assuming it is needed.
431 # NOTE: use _is_mech_obj_instantiated() to just
432 # check if we have a mech obj (and it will not be instantiated)
433 2 ▶sub mech_obj {
Subroutine mech_obj: called CC=5 SCAR=25.6
Pod documented
434 2 my $self = $_[0];
435 2 my $m = $_[1];
436  
437 2 ▶ if( defined $m ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $m) 0 2 2
438 0 $self->{'_private'}->{'WWW::Mechanize::Chrome-object'} = $m;
439 0 return $m
440 }
441 2 ▶ if( ! defined $self->{'_private'}->{'WWW::Mechanize::Chrome-object'} ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"}) 2 0 2
442 # launch a chrome on demand
443 2 $self->{'_private'}->{'WWW::Mechanize::Chrome-object'} = $self->launch_mech_obj(
444 $self->mech_params()
445 );
446 0 ▶ if( ! defined $self->{'_private'}->{'WWW::Mechanize::Chrome-object'} ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"}) 0 0 0
447 0 ▶ my $parent = ( caller(1) )[3] || "N/A";
Condition0%0/2
(caller 1)[3] || 'N/A'
l!l
00
MC/DC0%0/1
(caller 1)[3] || 'N/A'
(caller 1)[3]
Truth table0%0/2
A
A= (caller 1)[3]
Aresult
11
00
448 0 my $whoami = ( caller(0) )[3];
449 0 my $log = $self->log();
450 0 $log->error("$whoami (via $parent), line ".__LINE__." : call to ".'launch_mech_obj()'." with parameters:\n".perl2dump($self->mech_params())."\n--- end mech launch parameters\n has failed.");
451 return undef
452 0 }
453 }
454 0 return $self->{'_private'}->{'WWW::Mechanize::Chrome-object'};
455 }
456 6 ▶sub mech_params {
Subroutine mech_params: called CC=2 SCAR=6.9
Pod undocumented
457 6 my $self = $_[0];
458 6 my $m = $_[1];
459 6 ▶ return $self->{'_private'}->{'WWW::Mechanize::Chrome-params'} unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 3 3 6
460 3 $self->{'_private'}->{'WWW::Mechanize::Chrome-params'} = $m;
461 3 return $m
462 }
463 4 ▶sub stop_on_error {
Subroutine stop_on_error: called CC=2 SCAR=6.9
Pod undocumented
464 4 my ($self, $m) = @_;
465 4 ▶ return $self->{'stop-on-error'} unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 1 3 4
466 3 $self->{'stop-on-error'} = $m;
467 3 return $m;
468 }
469 3 ▶sub settle_time {
Subroutine settle_time: called CC=2 SCAR=7.0
Pod undocumented
470 3 my $self = $_[0];
471 3 my $m = $_[1];
472 3 ▶ return $self->{'settle-time'} unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 0 3 3
473 3 $self->{'settle-time'} = $m;
474 3 return $m
475 }
476 # has no meaning to change once the mech is instantiated, so ro:
477 6 ▶sub resolution { return $_[0]->{'_private'}->{'resolution'} }
Subroutine resolution: called CC=1 SCAR=0.0
Pod documented
478 # has no meaning to change once the mech is instantiated, so ro:
479 6 ▶sub headless { return $_[0]->{'_private'}->{'headless'} }
Subroutine headless: called CC=1 SCAR=0.0
Pod documented
480 1 ▶sub shutdown {
Subroutine shutdown: called CC=2 SCAR=12.4
Pod documented
481 1 my $self = $_[0];
482 1 ▶ return unless defined $self->mech_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->mech_obj 0 0 0
483 0 $self->mech_obj->close;
484 0 sleep(0.5);
485 0 $self->{'_private'}->{'WWW::Mechanize::Chrome-object'} = undef
486 }
487 19 ▶sub log { return $_[0]->{'_private'}->{'log'}->{'logger-object'} }
Subroutine log: called CC=1 SCAR=0.0
Pod undocumented
488 5 ▶sub cleanup {
Subroutine cleanup: called CC=2 SCAR=6.9
Pod documented
489 5 my ($self, $m) = @_;
490 #my $log = $self->log();
491 5 ▶ if( defined $m ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $m) 3 2 5
492 3 $self->{'_private'}->{'debug'}->{'cleanup'} = $m;
493 3 return $m;
494 }
495 2 return $self->{'_private'}->{'debug'}->{'cleanup'}
496 }
497  
498 # return configfile or read+check+set a configfile,
499 # returns undef on failure or the configfile on success
500 3 ▶sub configfile {
Subroutine configfile: called CC=6 SCAR=21.2
Pod documented
501 3 my ($self, $infile) = @_;
502  
503 3 ▶ return $self->{'_private'}->{'configfile'} unless defined $infile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $infile 0 3 3
504  
505 3 ▶ my $parent = ( caller(1) )[3] || "N/A";
Condition50%1/2
(caller 1)[3] || 'N/A'
l!l
30
MC/DC0%0/1
(caller 1)[3] || 'N/A'
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
506 3 my $whoami = ( caller(0) )[3];
507  
508 # this can be called before the logger is created, so create a temp logger for this
509 3 ▶ my $log = $self->log() // Mojo::Log->new();
Condition33%1/3
$self->log // "Mojo::Log"->new
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->log // "Mojo::Log"->new
$self->log "Mojo::Log"->new
Truth table33%1/3
A // B
A= $self->logB= "Mojo::Log"->new
ABresult
1X1
011
000
510  
511 3 my $ch = parse_configfile($infile, $log);
512 0 ▶ if( ! defined $ch ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'parse_configfile()'." has failed for configuration file '$infile'."); return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ch) 0 3 3
513  
514 # set it in self, it will also do checks on required keys
515 0 ▶ if( ! defined $self->confighash($ch) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to load specified confighash, , there must be errors in the configuration."); return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->confighash($ch)) 0 3 3
516  
517 3 $self->{'_private'}->{'configfile'} = $infile;
518  
519 3 return $infile #success
520 }
521  
522 # parse a config string (the exact contents of a config file)
523 # returns undef on failure or the confighash on success
524 3 ▶sub parse_configstring {
Subroutine parse_configstring: called CC=4 SCAR=15.5
Pod undocumented
525 3 my ($instring, $log) = @_;
526  
527 3 ▶ my $parent = ( caller(1) )[3] || "N/A";
Condition50%1/2
(caller 1)[3] || 'N/A'
l!l
30
MC/DC0%0/1
(caller 1)[3] || 'N/A'
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
528 3 my $whoami = ( caller(0) )[3];
529  
530 # this can be called before the logger is created, so create a temp logger for this
531 3 ▶ $log //= Mojo::Log->new();
Condition33%1/3
$log //= "Mojo::Log"->new
l!l&&r!l&&!r
300
MC/DC0%0/2
$log //= "Mojo::Log"->new
$log "Mojo::Log"->new
Truth table33%1/3
A //= B
A= $logB= "Mojo::Log"->new
ABresult
1X1
011
000
532 3 my $xp = {
533 'string' => $instring,
534 'commentstyle' => 'custom(</*)(*/>)',
535 'tags' => ['<%','%>'],
536 'variable-substitutions' => {
537 'SCRIPTDIR' => Cwd::abs_path($FindBin::Bin),
538 },
539 };
540 3 my $ch = Config::JSON::Enhanced::config2perl($xp);
541 0 ▶ if( ! defined $ch ){ $log->error("Configuration parameters:\n".perl2dump($xp)."\nConfiguration string to parse:\n${instring}\n\n${whoami} (via $parent), line ".__LINE__." : error, call to ".'Config::JSON::Enhanced::config2perl()'." has failed for above configuration string with above parameters."); return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ch) 0 3 3
542 3 return $ch; #success
543 }
544  
545 # parses the configuration file specified and returns a confighash
546 # returns undef on failure or the confighash on success
547 3 ▶sub parse_configfile {
Subroutine parse_configfile: called CC=5 SCAR=17.8
Pod undocumented
548 3 my ($infile, $log) = @_;
549  
550 3 ▶ my $parent = ( caller(1) )[3] || "N/A";
Condition50%1/2
(caller 1)[3] || 'N/A'
l!l
30
MC/DC0%0/1
(caller 1)[3] || 'N/A'
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
551 3 my $whoami = ( caller(0) )[3];
552  
553 # this can be called before the logger is created, so create a temp logger for this
554 3 ▶ $log //= Mojo::Log->new();
Condition33%1/3
$log //= "Mojo::Log"->new
l!l&&r!l&&!r
300
MC/DC0%0/2
$log //= "Mojo::Log"->new
$log "Mojo::Log"->new
Truth table33%1/3
A //= B
A= $logB= "Mojo::Log"->new
ABresult
1X1
011
000
555  
556 3 my $FH;
557 0 ▶ if( ! open($FH, '<', $infile) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, failed to open input file '$infile' for reading, $!"); return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (open $FH, "<", $infile) 0 3 3
558 3 my $instring;
559 3 { local $/ = undef; $instring = <$FH> } close $FH;
560  
561 3 my $ch = parse_configstring($instring, $log);
562 0 ▶ if( ! defined $ch ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to ".'parse_configstring()'." has failed."); return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ch) 0 3 3
563  
564 3 return $ch; #success
565 }
566  
567 # returns the confighash stored or if one is supplied
568 # it checks it and sets it and returns it
569 # or it returns undef on failure
570 # NOTE, if param is specified then we assume we do not have any configuration,
571 # we do not have a logger yet, we have no configuration, no verbosity, etc.
572 9 ▶sub confighash {
Subroutine confighash: called CC=9 SCAR=26.0
Pod documented
573 9 my ($self, $m) = @_;
574  
575 9 ▶ return $self->{'_private'}->{'confighash'} unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 6 3 9
576  
577 3 ▶ my $parent = ( caller(1) )[3] || "N/A";
Condition50%1/2
(caller 1)[3] || 'N/A'
l!l
30
MC/DC0%0/1
(caller 1)[3] || 'N/A'
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
578 3 my $whoami = ( caller(0) )[3];
579  
580 #print STDOUT "${whoami} (via $parent), line ".__LINE__." : called ...\n";
581  
582 # we are storing specified confighash but first check it for some fields:
583 # required fields:
584 3 for ('debug', 'logger', 'WWW::Mechanize::Chrome'){
585 0 ▶ if( ! exists($m->{$_}) || ! defined($m->{$_}) ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, configuration does not have key '$_'.\n"; return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $m->{$_} and defined $m->{$_}) 0 9 9
Condition33%1/3
exists $m->{$_} and defined $m->{$_}
!ll&&!rl&&r
009
MC/DC0%0/2
exists $m->{$_} and defined $m->{$_}
exists $m->{$_} defined $m->{$_}
Truth table33%1/3
A and B
A= exists $m->{$_}B= defined $m->{$_}
ABresult
0X0
100
111
586 }
587  
588 3 my $x = $m->{'WWW::Mechanize::Chrome'};
589 3 for ('launch_arg'){
590 0 ▶ if( ! exists($x->{$_}) || ! defined($x->{$_}) ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, configuration does not have key 'WWW::Mechanize::Chrome'->'$_'.\n"; return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $x->{$_} and defined $x->{$_}) 0 3 3
Condition33%1/3
exists $x->{$_} and defined $x->{$_}
!ll&&!rl&&r
003
MC/DC0%0/2
exists $x->{$_} and defined $x->{$_}
exists $x->{$_} defined $x->{$_}
Truth table33%1/3
A and B
A= exists $x->{$_}B= defined $x->{$_}
ABresult
0X0
100
111
591 }
592  
593 # ok!
594 3 $self->{'_private'}->{'confighash'} = $m;
595 3 return $m
596 }
597  
598 # returns 1 on failure, 0 on success
599 3 ▶sub init {
Subroutine init: called CC=76 SCAR=72.1
Pod undocumented
600 3 my ($self, $params) = @_;
601 3 ▶ my $parent = ( caller(1) )[3] || "N/A";
Condition50%1/2
(caller 1)[3] || 'N/A'
l!l
30
MC/DC0%0/1
(caller 1)[3] || 'N/A'
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
602 3 my $whoami = ( caller(0) )[3];
603  
604 # Confighash
605 # first see if either user specified a config file or the default is present and read it,
606 # then we will overwrite with user-specified params if any
607 3 my ($configfile, $confighash);
608 3 ▶ if( exists($params->{'configfile'}) && defined($configfile=$params->{'configfile'}) ){
Branch: 3 branches
BranchTrueFalseTotal
if (exists $params->{'configfile'} and defined($configfile = $params->{'configfile'})) { } 3 0 3
elsif (exists $params->{'configstring'} and defined $params->{'configstring'}) { } 0 0 0
elsif (exists $params->{'confighash'} and defined $params->{'confighash'}) { } 0 0 0
Condition33%1/3
exists $params->{"configfile"} and defined($configfile = $params->{"configfile"})
!ll&&!rl&&r
003
MC/DC0%0/2
exists $params->{"configfile"} and defined($configfile = $params->{"configfile"})
exists $params->{"configfile"} defined($configfile = $params->{"configfile"})
Truth table33%1/3
A and B
A= exists $params->{"configfile"}B= defined($configfile = $params->{"configfile"})
ABresult
0X0
100
111
609 0 ▶ if( ! -f $configfile ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, specified configfile '$configfile' does not exist or it is not a file.\n"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $configfile) 0 3 3
610 # this reads, creates confighash and calls confighash() which will do all the tests
611 0 ▶ if( ! defined $self->configfile($configfile) ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, call to ".'configfile()'." has failed for configfile '$configfile'.\n"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->configfile($configfile)) 0 3 3
612 3 $confighash = $self->confighash();
613 ▶ } elsif( exists($params->{'configstring'}) && defined($params->{'configstring'}) ){
Condition0%0/3
exists $params->{"configstring"} and defined $params->{"configstring"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params->{"configstring"} and defined $params->{"configstring"}
exists $params->{"configstring"} defined $params->{"configstring"}
Truth table0%0/3
A and B
A= exists $params->{"configstring"}B= defined $params->{"configstring"}
ABresult
0X0
100
111
614 0 $confighash = parse_configstring($params->{'configstring'}, undef);
615 0 ▶ if( ! defined $confighash ){ print STDERR $params->{'configstring'}."\n"."${whoami} (via $parent), line ".__LINE__." : error, call to ".'parse_configstring()'." has failed for the above configuration string provided.\n"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $confighash) 0 0 0
616 # this sets the confighash and checks it too
617 0 ▶ if( ! defined $self->confighash($confighash) ){ print STDERR $params->{'configstring'}."\n"."${whoami} (via $parent), line ".__LINE__." : error, call to ".'confighash()'." has failed for above configuration string provided.\n"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->confighash($confighash)) 0 0 0
618 ▶ } elsif( exists($params->{'confighash'}) && defined($params->{'confighash'}) ){
Condition0%0/3
exists $params->{"confighash"} and defined $params->{"confighash"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params->{"confighash"} and defined $params->{"confighash"}
exists $params->{"confighash"} defined $params->{"confighash"}
Truth table0%0/3
A and B
A= exists $params->{"confighash"}B= defined $params->{"confighash"}
ABresult
0X0
100
111
619 0 $confighash = $params->{'confighash'};
620 # this sets the confighash and checks it too
621 0 ▶ if( ! defined $self->confighash($confighash) ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, call to ".'confighash()'." has failed.\n"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->confighash($confighash)) 0 0 0
622 } else {
623 # no config specified, load default
624 0 $confighash = parse_configstring($DEFAULT_CONFIGSTRING, undef);
625 0 ▶ if( ! defined $confighash ){ print STDERR $params->{'configstring'}."\n"."${whoami} (via $parent), line ".__LINE__." : error, call to ".'parse_configstring()'." has failed for the above configuration string provided.\n"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $confighash) 0 0 0
626 # this sets the confighash and checks it too
627 0 ▶ if( ! defined $self->confighash($confighash) ){ print STDERR $params->{'configstring'}."\n"."${whoami} (via $parent), line ".__LINE__." : error, call to ".'confighash()'." has failed for above configuration string provided.\n"; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->confighash($confighash)) 0 0 0
628 0 print STDOUT "${whoami} (via $parent), line ".__LINE__." : warning, loading default configuration because none was specified ...\n";
629 }
630 # by now we have a confighash in self or died
631  
632 # for creating the logger: check
633 # 1. params if they have logger or logfile
634 # 2. our own confighash if it contains logfile
635 # 3. if all else fails, create a vanilla logger
636 3 ▶ if( exists($params->{'logger-object'}) && defined($params->{'logger-object'}) ){
Branch: 3 branches
BranchTrueFalseTotal
if (exists $params->{'logger-object'} and defined $params->{'logger-object'}) { } 1 2 3
elsif (exists $params->{'logfile'} and defined $params->{'logfile'}) { } 0 2 2
elsif (not defined $self->{'_private'}{'log'}{'logger-object'}) { } 2 0 2
Condition66%2/3
exists $params->{"logger-object"} and defined $params->{"logger-object"}
!ll&&!rl&&r
201
MC/DC50%1/2
exists $params->{"logger-object"} and defined $params->{"logger-object"}
exists $params->{"logger-object"} defined $params->{"logger-object"}
Truth table66%2/3
A and B
A= exists $params->{"logger-object"}B= defined $params->{"logger-object"}
ABresult
0X0
100
111
637 1 $self->{'_private'}->{'log'}->{'logger-object'} = $params->{'logger-object'};
638 #$log->info("${whoami} (via $parent), line ".__LINE__." : using user-supplied logger object.");
639 ▶ } elsif( exists($params->{'logfile'}) && defined($params->{'logfile'}) ){
Condition33%1/3
exists $params->{"logfile"} and defined $params->{"logfile"}
!ll&&!rl&&r
200
MC/DC0%0/2
exists $params->{"logfile"} and defined $params->{"logfile"}
exists $params->{"logfile"} defined $params->{"logfile"}
Truth table33%1/3
A and B
A= exists $params->{"logfile"}B= defined $params->{"logfile"}
ABresult
0X0
100
111
640 0 $self->{'_private'}->{'log'}->{'logger-object'} = Mojo::Log->new(path => $params->{'logfile'});
641 #$log->info("${whoami} (via $parent), line ".__LINE__." : logging to file '".$params->{'logfile'}."'.");
642 } elsif( ! defined($self->{'_private'}->{'log'}->{'logger-object'}) ){
643 2 $self->{'_private'}->{'log'}->{'logger-object'} = Mojo::Log->new();
644 #$log->info("${whoami} (via $parent), line ".__LINE__." : a vanilla logger has been created to log to the console.");
645 }
646  
647 # Now we have a logger
648 3 my $log = $self->log();
649 3 $log->short(1);
650  
651 # deprecated 'logger' has now become 'logger-object', we die if this is used
652 0 ▶ if( exists($params->{'logger'}) && defined($params->{'logger'}) ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, constructor parameter 'logger' has been replaced by 'logger-object'. This parameter specifies a logger object to be passed on this object."); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"logger"} and defined $params->{"logger"}) 0 3 3
Condition33%1/3
exists $params->{"logger"} and defined $params->{"logger"}
!ll&&!rl&&r
300
MC/DC0%0/2
exists $params->{"logger"} and defined $params->{"logger"}
exists $params->{"logger"} defined $params->{"logger"}
Truth table33%1/3
A and B
A= exists $params->{"logger"}B= defined $params->{"logger"}
ABresult
0X0
100
111
653  
654 # our logger becomes DOMops logger:
655 3 $WWW::Mechanize::Chrome::DOMops::domops_LOGGER = $log;
656  
657 # Set verbosity and cleanup as follows:
658 # 1. check if exists in params
659 # 2. check if exists in confighash
660 # 3. set default value
661 3 my $v;
662 3 ▶ if( exists($params->{'verbosity'}) && defined($params->{'verbosity'}) ){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params->{'verbosity'} and defined $params->{'verbosity'}) { } 0 3 3
elsif (exists $confighash->{'debug'} and exists $confighash->{'debug'}{'verbosity'} and defined $confighash->{'debug'}{'verbosity'}) { } 3 0 3
Condition33%1/3
exists $params->{"verbosity"} and defined $params->{"verbosity"}
!ll&&!rl&&r
300
MC/DC0%0/2
exists $params->{"verbosity"} and defined $params->{"verbosity"}
exists $params->{"verbosity"} defined $params->{"verbosity"}
Truth table33%1/3
A and B
A= exists $params->{"verbosity"}B= defined $params->{"verbosity"}
ABresult
0X0
100
111
663 0 $v = $params->{'verbosity'};
664 ▶ } elsif( exists($confighash->{'debug'}) && exists($confighash->{'debug'}->{'verbosity'}) && defined($confighash->{'debug'}->{'verbosity'}) ){
Condition33%2/6
exists $confighash->{"debug"} and exists $confighash->{"debug"}{"verbosity"} and defined $confighash->{"debug"}{"verbosity"}
!ll&&!rl&&r
003
exists $confighash->{"debug"} and exists $confighash->{"debug"}{"verbosity"}
!ll&&!rl&&r
003
MC/DC0%0/3
exists $confighash->{"debug"} and exists $confighash->{"debug"}{"verbosity"} and defined $confighash->{"debug"}{"verbosity"}
exists $confighash->{"debug"} exists $confighash->{"debug"}{"verbosity"} defined $confighash->{"debug"}{"verbosity"}
Truth table25%1/4
A and B and C
A= exists $confighash->{"debug"}B= exists $confighash->{"debug"}{"verbosity"}C= defined $confighash->{"debug"}{"verbosity"}
ABCresult
0XX0
10X0
1100
1111
665 3 $v = $confighash->{'debug'}->{'verbosity'};
666 } else {
667 0 $v = 0; # default verbosity
668 }
669 # NOTE: sometimes verbosity(x) will also set verbosity to other objects
670 # which are instantiated later, e.g. mech, lwp,
671 # so, you set verbosity know for public use
672 # AND ALSO YOU MUST CALL verbosity(x) AGAIN at the end when
673 # all are instantiated, or whenever you (re-)instantiating one of these objects.
674 # like $self->verbosity( $self->verbosity );
675 # THIS also sets DOMops verbosity:
676 0 ▶ if( $self->verbosity($v) < 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to 'verbosity()' has failed for value '$v'."); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbosity($v) < 0) 0 3 3
677  
678 3 my $verbosity = $self->verbosity; # we have verbosity
679  
680 3 ▶ if( exists($params->{'cleanup'}) && defined($params->{'cleanup'}) ){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params->{'cleanup'} and defined $params->{'cleanup'}) { } 3 0 3
elsif (exists $confighash->{'debug'} and exists $confighash->{'debug'}{'cleanup'} and defined $confighash->{'debug'}{'cleanup'}) { } 0 0 0
Condition33%1/3
exists $params->{"cleanup"} and defined $params->{"cleanup"}
!ll&&!rl&&r
003
MC/DC0%0/2
exists $params->{"cleanup"} and defined $params->{"cleanup"}
exists $params->{"cleanup"} defined $params->{"cleanup"}
Truth table33%1/3
A and B
A= exists $params->{"cleanup"}B= defined $params->{"cleanup"}
ABresult
0X0
100
111
681 3 $v = $params->{'cleanup'};
682 ▶ } elsif( exists($confighash->{'debug'}) && exists($confighash->{'debug'}->{'cleanup'}) && defined($confighash->{'debug'}->{'cleanup'}) ){
Condition0%0/6
exists $confighash->{"debug"} and exists $confighash->{"debug"}{"cleanup"} and defined $confighash->{"debug"}{"cleanup"}
!ll&&!rl&&r
000
exists $confighash->{"debug"} and exists $confighash->{"debug"}{"cleanup"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $confighash->{"debug"} and exists $confighash->{"debug"}{"cleanup"} and defined $confighash->{"debug"}{"cleanup"}
exists $confighash->{"debug"} exists $confighash->{"debug"}{"cleanup"} defined $confighash->{"debug"}{"cleanup"}
Truth table0%0/4
A and B and C
A= exists $confighash->{"debug"}B= exists $confighash->{"debug"}{"cleanup"}C= defined $confighash->{"debug"}{"cleanup"}
ABCresult
0XX0
10X0
1100
1111
683 0 $v = $confighash->{'debug'}->{'cleanup'};
684 } else {
685 0 $v = 0; # default
686 }
687 0 ▶ if( $self->cleanup($v) < 0 ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to 'cleanup()' has failed for value '$v'."); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cleanup($v) < 0) 0 3 3
688  
689 # stop on error? default is no
690 3 ▶ if( exists($params->{'stop-on-error'}) && defined($params->{'stop-on-error'}) ){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params->{'stop-on-error'} and defined $params->{'stop-on-error'}) { } 0 3 3
elsif (exists $confighash->{'constructor'} and exists $confighash->{'constructor'}{'stop-on-error'} and defined $confighash->{'constructor'}{'stop-on-error'}) { } 3 0 3
Condition33%1/3
exists $params->{"stop-on-error"} and defined $params->{"stop-on-error"}
!ll&&!rl&&r
300
MC/DC0%0/2
exists $params->{"stop-on-error"} and defined $params->{"stop-on-error"}
exists $params->{"stop-on-error"} defined $params->{"stop-on-error"}
Truth table33%1/3
A and B
A= exists $params->{"stop-on-error"}B= defined $params->{"stop-on-error"}
ABresult
0X0
100
111
691 0 $v = $params->{'stop-on-error'};
692 ▶ } elsif( exists($confighash->{'constructor'}) && exists($confighash->{'constructor'}->{'stop-on-error'}) && defined($confighash->{'constructor'}->{'stop-on-error'}) ){
Condition33%2/6
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"stop-on-error"} and defined $confighash->{"constructor"}{"stop-on-error"}
!ll&&!rl&&r
003
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"stop-on-error"}
!ll&&!rl&&r
003
MC/DC0%0/3
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"stop-on-error"} and defined $confighash->{"constructor"}{"stop-on-error"}
exists $confighash->{"constructor"} exists $confighash->{"constructor"}{"stop-on-error"} defined $confighash->{"constructor"}{"stop-on-error"}
Truth table25%1/4
A and B and C
A= exists $confighash->{"constructor"}B= exists $confighash->{"constructor"}{"stop-on-error"}C= defined $confighash->{"constructor"}{"stop-on-error"}
ABCresult
0XX0
10X0
1100
1111
693 3 $v = $confighash->{'constructor'}->{'stop-on-error'};
694 } else {
695 0 $v = 0; # default
696 }
697 3 $self->stop_on_error($v);
698  
699 # input param 'settle-time' (>=0 seconds)
700 3 ▶ if( exists($params->{'settle-time'}) && defined($params->{'settle-time'}) ){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params->{'settle-time'} and defined $params->{'settle-time'}) { } 2 1 3
elsif (exists $confighash->{'constructor'} and exists $confighash->{'constructor'}{'settle-time'} and defined $confighash->{'constructor'}{'settle-time'}) { } 1 0 1
Condition66%2/3
exists $params->{"settle-time"} and defined $params->{"settle-time"}
!ll&&!rl&&r
102
MC/DC50%1/2
exists $params->{"settle-time"} and defined $params->{"settle-time"}
exists $params->{"settle-time"} defined $params->{"settle-time"}
Truth table66%2/3
A and B
A= exists $params->{"settle-time"}B= defined $params->{"settle-time"}
ABresult
0X0
100
111
701 2 $v = $params->{'settle-time'};
702 ▶ } elsif( exists($confighash->{'constructor'}) && exists($confighash->{'constructor'}->{'settle-time'}) && defined($confighash->{'constructor'}->{'settle-time'}) ){
Condition33%2/6
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"settle-time"} and defined $confighash->{"constructor"}{"settle-time"}
!ll&&!rl&&r
001
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"settle-time"}
!ll&&!rl&&r
001
MC/DC0%0/3
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"settle-time"} and defined $confighash->{"constructor"}{"settle-time"}
exists $confighash->{"constructor"} exists $confighash->{"constructor"}{"settle-time"} defined $confighash->{"constructor"}{"settle-time"}
Truth table25%1/4
A and B and C
A= exists $confighash->{"constructor"}B= exists $confighash->{"constructor"}{"settle-time"}C= defined $confighash->{"constructor"}{"settle-time"}
ABCresult
0XX0
10X0
1100
1111
703 1 $v = $confighash->{'constructor'}->{'settle-time'};
704 } else {
705 0 $v = 0; # default
706 }
707 3 $self->settle_time($v);
708  
709 # browser size = resolution size
710 3 ▶ if( exists($params->{'resolution'}) && defined($params->{'resolution'}) ){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params->{'resolution'} and defined $params->{'resolution'}) { } 0 3 3
elsif (exists $confighash->{'constructor'} and exists $confighash->{'constructor'}{'resolution'} and defined $confighash->{'constructor'}{'resolution'}) { } 3 0 3
Condition33%1/3
exists $params->{"resolution"} and defined $params->{"resolution"}
!ll&&!rl&&r
300
MC/DC0%0/2
exists $params->{"resolution"} and defined $params->{"resolution"}
exists $params->{"resolution"} defined $params->{"resolution"}
Truth table33%1/3
A and B
A= exists $params->{"resolution"}B= defined $params->{"resolution"}
ABresult
0X0
100
111
711 0 $v = $params->{'resolution'};
712 ▶ } elsif( exists($confighash->{'constructor'}) && exists($confighash->{'constructor'}->{'resolution'}) && defined($confighash->{'constructor'}->{'resolution'}) ){
Condition33%2/6
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"resolution"} and defined $confighash->{"constructor"}{"resolution"}
!ll&&!rl&&r
003
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"resolution"}
!ll&&!rl&&r
003
MC/DC0%0/3
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"resolution"} and defined $confighash->{"constructor"}{"resolution"}
exists $confighash->{"constructor"} exists $confighash->{"constructor"}{"resolution"} defined $confighash->{"constructor"}{"resolution"}
Truth table25%1/4
A and B and C
A= exists $confighash->{"constructor"}B= exists $confighash->{"constructor"}{"resolution"}C= defined $confighash->{"constructor"}{"resolution"}
ABCresult
0XX0
10X0
1100
1111
713 3 $v = $confighash->{'constructor'}->{'resolution'};
714 } else {
715 0 $v = '1600x1200'; # default
716 }
717 3 $v =~ s/x/,/; # !!!!!!
718 3 $_[0]->{'_private'}->{'resolution'} = $v; # it has no setter, because no point to change res midstream
719  
720 #headless mode? default is yes, but for debugging can be set to 1
721 3 ▶ if( exists($params->{'headless'}) && defined($params->{'headless'}) ){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params->{'headless'} and defined $params->{'headless'}) { } 0 3 3
elsif (exists $confighash->{'constructor'} and exists $confighash->{'constructor'}{'headless'} and defined $confighash->{'constructor'}{'headless'}) { } 0 3 3
Condition33%1/3
exists $params->{"headless"} and defined $params->{"headless"}
!ll&&!rl&&r
300
MC/DC0%0/2
exists $params->{"headless"} and defined $params->{"headless"}
exists $params->{"headless"} defined $params->{"headless"}
Truth table33%1/3
A and B
A= exists $params->{"headless"}B= defined $params->{"headless"}
ABresult
0X0
100
111
722 0 $v = $params->{'headless'};
723 ▶ } elsif( exists($confighash->{'constructor'}) && exists($confighash->{'constructor'}->{'headless'}) && defined($confighash->{'constructor'}->{'headless'}) ){
Condition33%2/6
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"headless"} and defined $confighash->{"constructor"}{"headless"}
!ll&&!rl&&r
300
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"headless"}
!ll&&!rl&&r
030
MC/DC0%0/3
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"headless"} and defined $confighash->{"constructor"}{"headless"}
exists $confighash->{"constructor"} exists $confighash->{"constructor"}{"headless"} defined $confighash->{"constructor"}{"headless"}
Truth table25%1/4
A and B and C
A= exists $confighash->{"constructor"}B= exists $confighash->{"constructor"}{"headless"}C= defined $confighash->{"constructor"}{"headless"}
ABCresult
0XX0
10X0
1100
1111
724 0 $v = $confighash->{'constructor'}->{'headless'};
725 } else {
726 3 $v = 1; # default, yes headless!
727 }
728 3 $_[0]->{'_private'}->{'headless'} = $v; # it has no setter, because no point to change res midstream
729  
730 # input param 'remove-dom-elements' as an array of hashrefs to be passed as params
731 # to WWW::Mechanize::Chrome::DOMOps::domops_zap (see that for what keys to use)
732 # usually, for each element to zap create a hashref with e.g. 'element-class' => [...]
733 # Or just specify 1 item in the array if you can find them with a single selector
734 # see https://metacpan.org/pod/WWW::Mechanize::Chrome::DOMops#ELEMENT-SELECTORS
735 # for more information
736 # NOTE: place an undef anywhere in this array in order to skip the rest of the elements
737 3 ▶ if( exists($params->{'remove-dom-elements'}) && defined($params->{'remove-dom-elements'}) ){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params->{'remove-dom-elements'} and defined $params->{'remove-dom-elements'}) { } 2 1 3
elsif (exists $confighash->{'constructor'} and exists $confighash->{'constructor'}{'remove-dom-elements'} and defined $confighash->{'constructor'}{'remove-dom-elements'}) { } 1 0 1
Condition66%2/3
exists $params->{"remove-dom-elements"} and defined $params->{"remove-dom-elements"}
!ll&&!rl&&r
102
MC/DC50%1/2
exists $params->{"remove-dom-elements"} and defined $params->{"remove-dom-elements"}
exists $params->{"remove-dom-elements"} defined $params->{"remove-dom-elements"}
Truth table66%2/3
A and B
A= exists $params->{"remove-dom-elements"}B= defined $params->{"remove-dom-elements"}
ABresult
0X0
100
111
738 0 ▶ if( ref($params->{'remove-dom-elements'}) ne 'ARRAY' ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input param 'remove-dom-elements' must be an ARRAYref and not '".ref($params->{'remove-dom-elements'})."'."); return undef }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->{"remove-dom-elements"} ne "ARRAY") 0 2 2
739 2 $v = $params->{'remove-dom-elements'};
740 ▶ } elsif( exists($confighash->{'constructor'}) && exists($confighash->{'constructor'}->{'remove-dom-elements'}) && defined($confighash->{'constructor'}->{'remove-dom-elements'}) ){
Condition33%2/6
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"remove-dom-elements"} and defined $confighash->{"constructor"}{"remove-dom-elements"}
!ll&&!rl&&r
001
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"remove-dom-elements"}
!ll&&!rl&&r
001
MC/DC0%0/3
exists $confighash->{"constructor"} and exists $confighash->{"constructor"}{"remove-dom-elements"} and defined $confighash->{"constructor"}{"remove-dom-elements"}
exists $confighash->{"constructor"} exists $confighash->{"constructor"}{"remove-dom-elements"} defined $confighash->{"constructor"}{"remove-dom-elements"}
Truth table25%1/4
A and B and C
A= exists $confighash->{"constructor"}B= exists $confighash->{"constructor"}{"remove-dom-elements"}C= defined $confighash->{"constructor"}{"remove-dom-elements"}
ABCresult
0XX0
10X0
1100
1111
741 1 $v = $confighash->{'constructor'}->{'remove-dom-elements'};
742 } else {
743 0 $v = 0; # default
744 }
745 3 $self->{'_private'}->{'remove-dom-elements'} = $v;
746  
747 # do we have exif data? This will be overwritten by any
748 # 'exif' specified in the shoot()
749 3 ▶ if( exists($params->{'exif'}) && defined($params->{'exif'}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"exif"} and defined $params->{"exif"}) 0 3 3
Condition33%1/3
exists $params->{"exif"} and defined $params->{"exif"}
!ll&&!rl&&r
300
MC/DC0%0/2
exists $params->{"exif"} and defined $params->{"exif"}
exists $params->{"exif"} defined $params->{"exif"}
Truth table33%1/3
A and B
A= exists $params->{"exif"}B= defined $params->{"exif"}
ABresult
0X0
100
111
750 # check that the exif data format is valid : { k => v, ... }
751 0 ▶ if( ref($params->{'exif'})ne'HASH' ){ $log->error("$whoami (via $parent), line ".__LINE__." : error, 'exif' parameter expects a HASH of tagname => tagvalue pairs, not ".ref($params->{'exif'})."."); return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->{"exif"} ne "HASH") 0 0 0
752 0 $self->{'_private'}->{'exif'} = $params->{'exif'};
753 }
754  
755 # do we have a recycled mech object passed on to us?
756 # in this case we will reuse it and not instantiate a fresh one:
757 3 ▶ if( exists($params->{'WWW::Mechanize::Chrome-object'}) && defined($params->{'WWW::Mechanize::Chrome-object'}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"WWW::Mechanize::Chrome-object"} and defined $params->{"WWW::Mechanize::Chrome-object"}) 0 3 3
Condition33%1/3
exists $params->{"WWW::Mechanize::Chrome-object"} and defined $params->{"WWW::Mechanize::Chrome-object"}
!ll&&!rl&&r
300
MC/DC0%0/2
exists $params->{"WWW::Mechanize::Chrome-object"} and defined $params->{"WWW::Mechanize::Chrome-object"}
exists $params->{"WWW::Mechanize::Chrome-object"} defined $params->{"WWW::Mechanize::Chrome-object"}
Truth table33%1/3
A and B
A= exists $params->{"WWW::Mechanize::Chrome-object"}B= defined $params->{"WWW::Mechanize::Chrome-object"}
ABresult
0X0
100
111
758 0 ▶ if( ref($params->{'WWW::Mechanize::Chrome-object'}) ne 'WWW::Mechanize::Chrome' ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input param 'WWW::Mechanize::Chrome-object' must be a WWW::Mechanize::Chrome object and not '".ref($params->{'WWW::Mechanize::Chrome-object'})."'."); return undef }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->{"WWW::Mechanize::Chrome-object"} ne "WWW::Mechanize::Chrome") 0 0 0
759 0 $self->{'_private'}->{'WWW::Mechanize::Chrome-object'} = $params->{'WWW::Mechanize::Chrome-object'};
760 0 ▶ if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : re-using already existing mech object ".$params->{'WWW::Mechanize::Chrome-object'}." ...") }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbosity > 0) 0 0 0
761 }
762  
763 # save the WWW::Mechanize::Chrome-params for later, we may need them if crash
764 # here, first read the confighash and then override any entries with $params
765 # this works only for 1st level keys
766 3 ▶ if( exists($params->{'WWW::Mechanize::Chrome'}) && defined($params->{'WWW::Mechanize::Chrome'}) ){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params->{'WWW::Mechanize::Chrome'} and defined $params->{'WWW::Mechanize::Chrome'}) { } 0 3 3
elsif (exists $confighash->{'WWW::Mechanize::Chrome'} and defined $confighash->{'WWW::Mechanize::Chrome'}) { } 3 0 3
Condition33%1/3
exists $params->{"WWW::Mechanize::Chrome"} and defined $params->{"WWW::Mechanize::Chrome"}
!ll&&!rl&&r
300
MC/DC0%0/2
exists $params->{"WWW::Mechanize::Chrome"} and defined $params->{"WWW::Mechanize::Chrome"}
exists $params->{"WWW::Mechanize::Chrome"} defined $params->{"WWW::Mechanize::Chrome"}
Truth table33%1/3
A and B
A= exists $params->{"WWW::Mechanize::Chrome"}B= defined $params->{"WWW::Mechanize::Chrome"}
ABresult
0X0
100
111
767 0 ▶ if( ref($params->{'WWW::Mechanize::Chrome'}) ne 'ARRAY' ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input param 'WWW::Mechanize::Chrome' must be an ARRAYref and not '".ref($params->{'WWW::Mechanize::Chrome'})."'."); return undef }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->{"WWW::Mechanize::Chrome"} ne "ARRAY") 0 0 0
768 $v = (exists($confighash->{'WWW::Mechanize::Chrome'}) && defined($confighash->{'WWW::Mechanize::Chrome'}))
769 ? {
770 0 %{ $confighash->{'WWW::Mechanize::Chrome'} },
771 0 %{ $params->{'WWW::Mechanize::Chrome'} },
772 }
773 0 ▶ : $params->{'WWW::Mechanize::Chrome'}
Branch: 1 branch
BranchTrueFalseTotal
exists $confighash->{'WWW::Mechanize::Chrome'} && defined $confighash->{'WWW::Mechanize::Chrome'} ? : 0 0 0
Condition0%0/3
exists $confighash->{"WWW::Mechanize::Chrome"} and defined $confighash->{"WWW::Mechanize::Chrome"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $confighash->{"WWW::Mechanize::Chrome"} and defined $confighash->{"WWW::Mechanize::Chrome"}
exists $confighash->{"WWW::Mechanize::Chrome"} defined $confighash->{"WWW::Mechanize::Chrome"}
Truth table0%0/3
A and B
A= exists $confighash->{"WWW::Mechanize::Chrome"}B= defined $confighash->{"WWW::Mechanize::Chrome"}
ABresult
0X0
100
111
774 ;
775 ▶ } elsif( exists($confighash->{'WWW::Mechanize::Chrome'}) && defined($confighash->{'WWW::Mechanize::Chrome'}) ){
Condition33%1/3
exists $confighash->{"WWW::Mechanize::Chrome"} and defined $confighash->{"WWW::Mechanize::Chrome"}
!ll&&!rl&&r
003
MC/DC0%0/2
exists $confighash->{"WWW::Mechanize::Chrome"} and defined $confighash->{"WWW::Mechanize::Chrome"}
exists $confighash->{"WWW::Mechanize::Chrome"} defined $confighash->{"WWW::Mechanize::Chrome"}
Truth table33%1/3
A and B
A= exists $confighash->{"WWW::Mechanize::Chrome"}B= defined $confighash->{"WWW::Mechanize::Chrome"}
ABresult
0X0
100
111
776 3 $v = $confighash->{'WWW::Mechanize::Chrome'};
777 } else {
778 0 $v = 0; # default
779 }
780  
781 # and modify some mech params:
782 3 ▶ for (@{$v->{'launch_arg'}}){ next unless $_ =~ /window\-size\s*=/; my $res = $self->resolution; $_ =~ s/=.+?$/=${res}/; last }
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /window\-size\s*=/ 0 3 3
783 3 $v->{'headless'} = $self->headless;
784  
785 # save them
786 3 $self->mech_params($v);
787  
788 # resize:
789 # google-chrome -app="data:text/html,<html><body><script>window.moveTo(580,240);window.resizeTo(1800,1600);</script></body></html>"
790  
791 3 return 0 # success
792 }
793  
794 # initialises module-specific things, no need to copy this to other modules
795 # returns 1 on failure, 0 on success
796 3 ▶sub init_module_specific {
Subroutine init_module_specific: called CC=10 SCAR=26.4
Pod undocumented
797 3 my ($self, $params) = @_;
798 3 ▶ my $parent = ( caller(1) )[3] || "N/A";
Condition50%1/2
(caller 1)[3] || 'N/A'
l!l
30
MC/DC0%0/1
(caller 1)[3] || 'N/A'
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
799 3 my $whoami = ( caller(0) )[3];
800 3 my $log = $self->log();
801 3 my $verbosity = $self->verbosity();
802 3 my $confighash = $self->confighash();
803 3 ▶ if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : called ...") }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbosity > 0) 3 0 3
804  
805 # TODO we can instantiate the shooter (PLUS chrome which is expensive to do !!!) lazily
806 # set or create the WWW::Mechanize::Chrome::Webshot object
807 3 ▶ if( exists($params->{'WWW::Mechanize::Chrome-object'}) && defined($params->{'WWW::Mechanize::Chrome-object'}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{'WWW::Mechanize::Chrome-object'} and defined $params->{'WWW::Mechanize::Chrome-object'}) { } 0 3 3
Condition33%1/3
exists $params->{"WWW::Mechanize::Chrome-object"} and defined $params->{"WWW::Mechanize::Chrome-object"}
!ll&&!rl&&r
300
MC/DC0%0/2
exists $params->{"WWW::Mechanize::Chrome-object"} and defined $params->{"WWW::Mechanize::Chrome-object"}
exists $params->{"WWW::Mechanize::Chrome-object"} defined $params->{"WWW::Mechanize::Chrome-object"}
Truth table33%1/3
A and B
A= exists $params->{"WWW::Mechanize::Chrome-object"}B= defined $params->{"WWW::Mechanize::Chrome-object"}
ABresult
0X0
100
111
808 # caller supplied an existing WWW::Mechanize::Chrome::Webshot object
809 0 $self->{'_private'}->{'WWW::Mechanize::Chrome-object'} = $params->{'WWW::Mechanize::Chrome-object'}
810 } else {
811 # instantiate only if not lazily
812 3 ▶ if( (! defined $self->{'_private'}->{'WWW::Mechanize::Chrome-object'} )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"} and !exists $params->{"launch-mech-on-demand"} || $params->{"launch-mech-on-demand"} == 0) 1 2 3
Condition66%4/6
not defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"} and !exists $params->{"launch-mech-on-demand"} || $params->{"launch-mech-on-demand"} == 0
!ll&&!rl&&r
021
not exists $params->{"launch-mech-on-demand"} or $params->{"launch-mech-on-demand"} == 0
l!l&&r!l&&!r
012
MC/DC33%1/3
not defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"} and !exists $params->{"launch-mech-on-demand"} || $params->{"launch-mech-on-demand"} == 0
not defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"} not exists $params->{"launch-mech-on-demand"} $params->{"launch-mech-on-demand"} == 0
Truth table50%2/4
not(A) and not(B) or C
A= not defined $self->{"_private"}{"WWW::Mechanize::Chrome-object"}B= not exists $params->{"launch-mech-on-demand"}C= $params->{"launch-mech-on-demand"} == 0
ABCresult
0XX0
1000
11X1
1011
813 && ( (! exists $params->{'launch-mech-on-demand'})
814 || ($params->{'launch-mech-on-demand'} == 0)
815 )
816 ){
817 1 ▶ if( ! defined $self->launch_mech_obj(
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->launch_mech_obj($self->mech_params)) 0 0 0
818 $self->mech_params()
819 0 ) ){ $log->error(perl2dump($self->mech_params())."${whoami} (via $parent), line ".__LINE__." : error, call to ".'WWW::Mechanize::Chrome->new()'." has failed for above parameters."); return 1; }
820 }
821 }
822  
823 # done
824 2 ▶ if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : "." has been initialised ...") }
Branch: 1 branch
BranchTrueFalseTotal
if ($verbosity > 0) 2 0 2
825 2 return 0 # success
826 }
827  
828 # It checks if specified tagname/tagvalue pairs (as a HASHref)
829 # are containd in the metadata of the input image
830 # returns a hash with tags as keys and each key having a value:
831 # 0 : missing
832 # 1 : exists and has correct value
833 # 2 : exists but has incorrect value
834 # exists or not respectively
835 0 ▶sub _check_if_exif_tags_exist_in_image {
Subroutine _check_if_exif_tags_exist_in_image: not called CC=5 SCAR=34.0
836 0 my ($imagefile, $exif, $log) = @_;
837 0 ▶ my $parent = ( caller(1) )[3] || "N/A";
Condition0%0/2
(caller 1)[3] || 'N/A'
l!l
00
MC/DC0%0/1
(caller 1)[3] || 'N/A'
(caller 1)[3]
Truth table0%0/2
A
A= (caller 1)[3]
Aresult
11
00
838 0 my $whoami = ( caller(0) )[3];
839 # the problem is that tagnames (not values) can take their own mysterious case
840 # so we UC everything just for comparisons of the keys
841 0 my $info1 = ImageInfo($imagefile);
842 0 my $info = { map { uc $_ => $info1->{$_} } keys %$info1 };
843 0 my %ret;
844 0 for my $tn (keys %$exif){
845 0 my $tv = $exif->{$tn};
846 0 my $tnuc = uc $tn;
847 0 ▶ if( ! exists($info->{$tnuc}) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $info->{$tnuc}) 0 0 0
848 # missing, output is 0
849 0 $log->error("$whoami (via $parent), line ".__LINE__." : error, tag name '${tn}' does not exist in output image '$imagefile'.");
850 0 $ret{$tn} = 0;
851 0 next;
852 }
853 0 my $gv = Encode::decode_utf8($info->{$tnuc});
854 0 ▶ if( $gv ne $tv ){
Branch: 1 branch
BranchTrueFalseTotal
if ($gv ne $tv) 0 0 0
855 # incorrect value, output is 2
856 0 $log->error("$whoami (via $parent), line ".__LINE__." : error, tag name '${tn}' in output image '$imagefile' does not have the correct value (expected: '".${tv}."', got: '".${gv}."').");
857 #$log->error("$whoami (via $parent), line ".__LINE__." : error, tag name '${tn}' in output image '$imagefile' does not have the correct value (expected: '".${tv}."', got: '".Encode::decode_utf8(${gv})."').");
858 0 $ret{$tn} = 2;
859 0 next;
860 }
861 0 $ret{$tn} = 1; # exists and has correct value
862 }
863 0 return \%ret
864 }
865  
866 ## POD starts here
867  
868 =pod
869  
870 =encoding utf8
871  
872 =head1 NAME
873  
874 WWW::Mechanize::Chrome::Webshot - cheap and cheerful html2pdf converter, take a screenshot of rendered HTML, complete with CSS and Javascript
875  
876 =head1 VERSION
877  
878 Version 0.05
879  
880 =head1 SYNOPSIS
881  
882 This module provides L</shoot($params)> which loads
883 a specified URL or local file into a spawned, possibly headless, browser
884 (thank you Corion for L<WWW::Mechanize::Chrome>),
885 waits for some settle time, optionally removes
886 specified DOM elements (e.g. advertisements and consents),
887 takes a screenshot of the rendered content and saves into
888 the output file, as PDF or PNG, optionally adding any specified EXIF tags.
889  
890 At the same time, this functionality can be seen as a
891 round-about way for converting HTML,
892 complete with CSS and JS, to PDF or PNG. And that is no mean feat.
893  
894 Actually it's a mean hack.
895  
896 Did I say that it supports as much HTML, CSS and JS
897 as the modern browser does?
898  
899 Here are some examples:
900  
901 use WWW::Mechanize::Chrome::Webshot;
902  
903 my $shooter = WWW::Mechanize::Chrome::Webshot->new({
904 'settle-time' => 10,
905 # optionally specify a Mojo::Log logger,
906 # useful if you have a global logger you want to use:
907 'logger-object' => Mojo::Log->new(path=>'webshot.log'),
908 # or just specify a file to log output to:
909 #'logfile' => 'webshot.log',
910 });
911 $shooter->shoot({
912 'output-filename' => 'abc.png',
913 # optional unless it can not be deduced from filename
914 'output-format' => 'png', # or pdf
915  
916 # URL or local file, e.g. 'file:///A/B/C.html'
917 # !!! BUT USE ABSOLUTE FILEPATH in uri
918 'url' => 'https://www.902.gr',
919  
920 # remove irritating DOM elements cluttering our view...
921 'remove-DOM-elements' => [
922 {'element-xpathselector' => '//div[id="advertisments"]'},
923 {...}
924 ],
925  
926 # optionally add exif metadata to the output image
927 'exif' => {'created' => 'by the shooter', 'tag2' => 'hehe', ...},
928 }) or die;
929 ...
930  
931 =head1 CONSTRUCTOR
932  
933 =head2 C<new($params)>
934  
935 Creates a new C<WWW::Mechanize::Chrome::Webshot> object. C<$params>
936 is a hash reference used to pass initialization options which may
937 or should include the following:
938  
939 =over 4
940  
941 =item * B<C<confighash>> or B<C<configfile>> or B<C<configstring>>
942  
943 Optional, default will be used. The configuration file/hash/string holds
944 configuration parameters and its format is "enhanced" JSON
945 (see L<use Config::JSON::Enhanced>) which is basically JSON
946 which allows comments between C< E<lt>/* > and C< */E<gt> >.
947  
948 Here is an example configuration file to get you started,
949 this configuration is used as default when none is provided:
950  
951 </* $VERSION = '0.01'; */>
952 </* comments are allowed */>
953 </* and <% vars %> and <% verbatim sections %> */>
954 {
955 "debug" : {
956 "verbosity" : 1,
957 </* cleanup temp files on exit */>
958 "cleanup" : 1
959 },
960 "logger" : {
961 </* log to file if you uncomment this */>
962 </* "filename" : "..." */>
963 },
964 "constructor" : {
965 </* for slow connections */>
966 "settle-time" : "3",
967 "resolution" : "1600x1200",
968 "stop-on-error" : "0",
969 "remove-dom-elements" : []
970 },
971 "WWW::Mechanize::Chrome" : {
972 "headless" : "1",
973 "launch_arg" : [
974 </* this will change as per the 'resolution' setting above */>
975 "--window-size=600x800",
976 "--password-store=basic", </* do not ask me for stupid chrome account password */>
977 </* "--remote-debugging-port=9223", */>
978 </* "--enable-logging", */>
979 "--disable-gpu",
980 </* "--no-sandbox", NO LONGER VALID */>
981 "--ignore-certificate-errors",
982 "--disable-background-networking",
983 "--disable-client-side-phishing-detection",
984 "--disable-component-update",
985 "--disable-hang-monitor",
986 "--disable-save-password-bubble",
987 "--disable-default-apps",
988 "--disable-infobars",
989 "--disable-popup-blocking"
990 ]
991 }
992 }
993  
994  
995 All sections in the configuration are mandatory.
996  
997 C<confighash> is a hash of configuration options with
998 structure as above and can be supplied to the constructor
999 instead of the configuration file.
1000  
1001 If no configuration is specified, then a default
1002 configuration will be used. This is hardcoded in the source code.
1003  
1004 =item * B<C<logger>> or B<C<logfile>>
1005  
1006 Optional. Specify a logger object which adheres
1007 to L<Mojo::Log>'s API or a logfile to write
1008 log info into. It must implement methods C<info()>, C<error()>, C<warn()>.
1009  
1010 =item * B<C<verbosity>>
1011  
1012 Optional. Verbosity level as an integer, default is 0, silent.
1013  
1014  
1015 =item * B<C<cleanup>>
1016  
1017 Optional. Cleanup all temporary files after exit. Default is 1 (yes). It is useful when debugging.
1018  
1019 =item * B<C<settle-time>>
1020  
1021 Optional. Seconds to wait between loading the specified URL and taking the screenshot.
1022 This is very important if target URL has lots to do or on a slow connection. Default is 2 seconds.
1023  
1024 =item * B<C<resolution>>
1025  
1026 Optional. Specify the size of the
1027 mechanized browser in the form C<WxH>. Ideally,
1028 this should set the size of the output image. Default
1029 value is C<1600x1200>.
1030  
1031 =item * B<C<headless>>
1032  
1033 Optional. When debugging you may find it useful to display the browser while
1034 it loads the URL. Set this to C<0> if you want this.
1035 Default is 1 (yes, headless, the browser window does not show).
1036 I am not sure if the browser dies soon after the mechanized browser object
1037 goes out of scope. You may want to place a C<sleep($long_time);>
1038 before that in order to inspect its contents at your leisure.
1039  
1040 =item * B<C<remove-dom-elements>>
1041  
1042 Optional. After the URL is loaded and settle time has passed, DOM elements can
1043 be removed. Annoyances like advertisements, consents, warnings can be
1044 zapped by specifying their XPath selectors. This is an ARRAY_REF of HASH_REF.
1045 Each HASH_REF is a selector for DOM elements to be zapped. See L<https://metacpan.org/pod/WWW::Mechanize::Chrome::DOMops#ELEMENT-SELECTORS>
1046 on the exact spec of the DOM selectors.
1047  
1048 =item * B<C<exif>>
1049  
1050 Optional. Specify one or more EXIF tags to be
1051 inserted into the output image as a HASH_REF of tag/value pairs
1052 each time L</shoot($params)> is called. This value will be overwritten
1053 if C<$params> (of L</shoot($params)>) contains its own C<exif> parameter.
1054  
1055 =item * B<C<WWW::Mechanize::Chrome>>
1056  
1057 Optional. Specify any parameters to be passed on to the
1058 constructor of L<WWW::Mechanize::Chrome> as a HASH_REF of parameters.
1059  
1060 =back
1061  
1062 =head1 METHODS
1063  
1064 =head2 B<C<shoot($params)>>
1065  
1066 It takes a screenshot of the specified URL as
1067 rendered by L<WWW::Mechanize::Chrome> (usually headless)
1068 and saves it as an image to the specified file.
1069  
1070 It returns C<0> on failure, C<1> on success.
1071  
1072 Input parameters C<$params>:
1073  
1074 =over 4
1075  
1076 =item * B<C<url>>: specifies the target URL or even a URI pointing
1077 to a local file (e.g. C<file:///A/B/C.html>, use absolute filepath).
1078  
1079 =item * B<C<remove-dom-elements>>: specifies DOM elements to
1080 be removed after the URL has been loaded and settle time has passed.
1081 Annoyances like advertisements, consents, warnings can be
1082 zapped by specifying their XPath selectors. This is an ARRAY_REF of HASH_REF.
1083 Each HASH_REF is a selector for DOM elements to be zapped. See L<https://metacpan.org/pod/WWW::Mechanize::Chrome::DOMops#ELEMENT-SELECTORS>
1084 on the exact spec of the DOM selectors. Note that a parameter with the same
1085 name can be specified in the constructor. If one is specified here,
1086 then the one specified in the constructor will be ignored, else, it will
1087 be used.
1088  
1089 =item * B<C<exif>>: optionally specify one or more EXIF tags to be
1090 inserted into the output image as a HASH_REF of tag/value pairs.
1091 If B<C<exif>> data is specified here, then
1092 any exif data specified in the constructor will be ignored. This works
1093 well for both PNG and PDF output images.
1094  
1095 =back
1096  
1097 =head2 B<C<shutdown()>>
1098  
1099 It shutdowns the current L<WWW::Mechanize::Chrome> object, if any.
1100  
1101  
1102 =head2 B<C<scroll_to_bottom()>>
1103  
1104 It scrolls the browser's contents to the very bottom without
1105 changing its horizontal position.
1106  
1107 =head2 B<C<scroll($w, $h)>>
1108  
1109 It scrolls the browser's screen by C<$w> pixels in the horizontal
1110 direction and by C<$h> pixels in the vertical direction.
1111  
1112 =head2 B<C<mech_obj()>>
1113  
1114 It returns the currently used L<WWW::Mechanize::Chrome> object.
1115  
1116 =head1 SCRIPTS
1117  
1118 For convenience, the following scripts are provided:
1119  
1120 =over 2
1121  
1122 =item * B<C<script/www-mechanize-webshot.pl>>
1123  
1124 It will take a URL, load it, render it, optionally zap any
1125 specified DOM elements and save the rendered content into
1126 an output image:
1127  
1128 This will save the screenshot and also adds the specified exif data:
1129  
1130 C<< script/www-mechanize-webshot.pl --url 'https://www.902.gr' --resolution 2000x2000 --exif 'created' 'bliako' --output-filename '902.png' --settle-time 10 >>
1131  
1132 Debug why the output is not what you expect, show the browser and let it live for huge settle time, also log output to a file:
1133  
1134 C<< script/www-mechanize-webshot.pl --no-headless --url 'https://www.902.gr' --resolution 2000x2000 --output-filename '902.png' --settle-time 100000 --verbosity 10 --logfile debug.log >>
1135  
1136 This will also remove specified DOM elements by tag name and XPath selector. Note that
1137 the output format will be deduced as PDF because of the filename:
1138  
1139 C<< script/www-mechanize-webshot.pl --remove-dom-elements '[{\"element-tag\":\"div\",\"element-id\":\"sickle-and-hammer\",\"&&\":\"1\"},{\"element-xpathselector\":\"//div[id=ads]\"}]' --url 'https://www.902.gr' --resolution 2000x2000 --exif 'created' 'bliako' --output-filename '902.pdf' --settle-time 10 >>
1140  
1141 Explicitly save the output as PDF:
1142  
1143 C<< script/www-mechanize-webshot.pl --url 'https://www.902.gr' --resolution 2000x2000 --exif 'created' 'bliako' --output-filename 'tmpimg' --output-format 'PDF' --settle-time 10 >>
1144  
1145 =back
1146  
1147 =head1 CREATING THE MECH OBJECT
1148  
1149 The mech (L<WWW::Mechanize::Chrome>) object must be supplied
1150 to the functions in this module. It must be created by the caller.
1151 This is how I do it:
1152  
1153 use WWW::Mechanize::Chrome;
1154 use Log::Log4perl qw(:easy);
1155 Log::Log4perl->easy_init($ERROR);
1156  
1157 my %default_mech_params = (
1158 headless => 1,
1159 # log => $mylogger,
1160 launch_arg => [
1161 '--window-size=600x800',
1162 '--password-store=basic', # do not ask me for stupid chrome account password
1163 # '--remote-debugging-port=9223',
1164 # '--enable-logging', # see also log above
1165 '--disable-gpu',
1166 '--no-sandbox',
1167 '--ignore-certificate-errors',
1168 '--disable-background-networking',
1169 '--disable-client-side-phishing-detection',
1170 '--disable-component-update',
1171 '--disable-hang-monitor',
1172 '--disable-save-password-bubble',
1173 '--disable-default-apps',
1174 '--disable-infobars',
1175 '--disable-popup-blocking',
1176 ],
1177 );
1178  
1179 my $mech_obj = eval {
1180 WWW::Mechanize::Chrome->new(%default_mech_params)
1181 };
1182 die $@ if $@;
1183  
1184 # This transfers all javascript code's console.log(...)
1185 # messages to perl's warn()
1186 # we need to keep $console var in scope!
1187 my $console = $mech_obj->add_listener('Runtime.consoleAPICalled', sub {
1188 warn
1189 "js console: "
1190 . join ", ",
1191 map { $_->{value} // $_->{description} }
1192 @{ $_[0]->{params}->{args} };
1193 })
1194 ;
1195  
1196 # and now fetch a page
1197 my $URL = '...';
1198 my $retmech = $mech_obj->get($URL);
1199 die "failed to fetch $URL" unless defined $retmech;
1200 $mech_obj->sleep(1); # let it settle
1201 # now the mech object has loaded the URL and has a DOM hopefully.
1202 # You can pass it on to domops_find() or domops_zap() to operate on the DOM.
1203  
1204  
1205 =head1 SECURITY WARNING
1206  
1207 L<WWW::Mechanize::Chrome> invokes the C<google-chrome>
1208 executable
1209 on behalf of the current user. Headless or not, C<google-chrome>
1210 is invoked. Depending on the launch parameters, either
1211 a fresh, new browser session will be created or the
1212 session of the current user with their profile, data, cookies,
1213 passwords, history, etc. will be used. The latter case is very
1214 dangerous.
1215  
1216 This behaviour is controlled by L<WWW::Mechanize::Chrome>'s
1217 L<constructor|WWW::Mechanize::Chrome#WWW::Mechanize::Chrome-%3Enew(-%options-)>
1218 parameters which, in turn, are used for launching
1219 the C<google-chrome> executable. Specifically,
1220 see L<WWW::Mechanize::Chrome#separate_session>,
1221 L<<WWW::Mechanize::Chrome#data_directory>
1222 and L<WWW::Mechanize::Chrome#incognito>.
1223  
1224 B<Unless you really need to mechsurf with your current session, aim
1225 to launching the browser with a fresh new session.
1226 This is the safest option.>
1227  
1228 B<Do not rely on default behaviour as this may change over
1229 time. Be explicit.>
1230  
1231 Also, be warned that L<WWW::Mechanize::Chrome::DOMops> executes
1232 javascript code on that C<google-chrome> instance.
1233 This is done nternally with javascript code hardcoded
1234 into the L<WWW::Mechanize::Chrome::DOMops>'s package files.
1235  
1236 On top of that L<WWW::Mechanize::Chrome::DOMops> allows
1237 for B<user-specified javascript code> to be executed on
1238 that C<google-chrome> instance. For example the callbacks
1239 on each element found, etc.
1240  
1241 This is an example of what can go wrong if
1242 you are not using a fresh C<google-chrome>
1243 session:
1244  
1245 You have just used C<google-chrome> to access your
1246 yahoo webmail and you did not logout.
1247 So, there will be an
1248 access cookie in the C<google-chrome> when you later
1249 invoke it via L<WWW::Mechanize::Chrome> (remember
1250 you have not told it to use a fresh session).
1251  
1252 If you allow
1253 unchecked user-specified (or copy-pasted from ChatGPT)
1254 javascript code in
1255 L<WWW::Mechanize::Chrome::DOMops>'s
1256 C<domops_find()>, C<domops_zap()>, etc. then it is, theoretically,
1257 possible that this javascript code
1258 initiates an XHR to yahoo and fetch your emails and
1259 pass them on to your perl code.
1260  
1261 But there is another problem,
1262 L<WWW::Mechanize::Chrome::DOMops>'s
1263 integrity of the embedded javascript code may have
1264 been compromised to exploit your current session.
1265  
1266 This is very likely with a Windows installation which,
1267 being the security swiss cheese it is, it
1268 is possible for anyone to compromise your module's code.
1269 It is less likely in Linux, if your modules are
1270 installed by root and are read-only for normal users.
1271 But, still, it is possible to be compromised (by root).
1272  
1273 Another issue is with the saved passwords and
1274 the browser's auto-fill when landing on a login form.
1275  
1276 Therefore, for all these reasons, B<it is advised not to invoke (via L<WWW::Mechanize::Chrome>)
1277 C<google-chrome> with your
1278 current/usual/everyday/email-access/bank-access
1279 identity so that it does not have access to
1280 your cookies, passwords, history etc.>
1281  
1282 It is better to create a fresh
1283 C<google-chrome>
1284 identity/profile and use that for your
1285 C<WWW::Mechanize::Chrome::DOMops> needs.
1286  
1287 No matter what identity you use, you may want
1288 to erase the cookies and history of C<google-chrome>
1289 upon its exit. That's a good practice.
1290  
1291 It is also advised to review the
1292 javascript code you provide
1293 via L<WWW::Mechanize::Chrome::DOMops> callbacks if
1294 it is taken from 3rd-party, human or not, e.g. ChatGPT.
1295  
1296 Additionally, make sure that the current
1297 installation of L<WWW::Mechanize::Chrome::DOMops>
1298 in your system is not compromised with malicious javascript
1299 code injected into it. For this you can check its MD5 hash
1300  
1301 =head2 REQUIREMENTS
1302  
1303 =head1 DEPENDENCIES
1304  
1305 This module depends on L<WWW::Mechanize::Chrome> which, in turn,
1306 depends on the C<google-chrome> executable be installed on the
1307 host computer. See L<WWW::Mechanize::Chrome::Install> on
1308 how to install the executable.
1309  
1310 Test scripts (which create there own mech object) will detect the absence
1311 of C<google-chrome> binary and exit gracefully, meaning the test passes.
1312 But with a STDERR message to the user. Who will hopefully notice it and
1313 proceed to C<google-chrome> installation. In any event, this module
1314 will be installed with or without C<google-chrome>.
1315  
1316 The browser will be run, usually headless -- so a headless host system is fine,
1317 the first time you take a screenshot. It will only be re-spawned if
1318 you have shutdown the browser in the meantime. Exiting your script
1319 will shutdown the browser. And so, running a script again will
1320 re-spawn the browser (AFAICU/sic/).
1321  
1322  
1323 =head2 CAVEATS
1324  
1325 In exporting to PDF, the size of the output image does not
1326 seem to be the same as the browser size. This does not happen
1327 with exporting to PNG.
1328  
1329  
1330 =head1 AUTHOR
1331  
1332 Andreas Hadjiprocopis, C<< <bliako at cpan.org> >>
1333  
1334 =head1 BUGS
1335  
1336 Please report any bugs or feature requests to C<bug-www-mechanize-chrome-webshot at rt.cpan.org>, or through
1337 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Mechanize-Chrome-Webshot>. I will be notified, and then you'll
1338 automatically be notified of progress on your bug as I make changes.
1339  
1340  
1341  
1342  
1343 =head1 SUPPORT
1344  
1345 You can find documentation for this module with the perldoc command.
1346  
1347 perldoc WWW::Mechanize::Chrome::Webshot
1348  
1349 You can also look for information at:
1350  
1351 =over 4
1352  
1353 =item * RT: CPAN's request tracker (report bugs here)
1354  
1355 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Mechanize-Chrome-Webshot>
1356  
1357 =item * AnnoCPAN: Annotated CPAN documentation
1358  
1359 L<http://annocpan.org/dist/WWW-Mechanize-Chrome-Webshot>
1360  
1361 =item * CPAN Ratings
1362  
1363 L<https://cpanratings.perl.org/d/WWW-Mechanize-Chrome-Webshot>
1364  
1365 =item * Search CPAN
1366  
1367 L<https://metacpan.org/release/WWW-Mechanize-Chrome-Webshot>
1368  
1369 =back
1370  
1371  
1372 =head1 ACKNOWLEDGEMENTS
1373  
1374  
1375 =head1 LICENSE AND COPYRIGHT
1376  
1377 Copyright 2019 Andreas Hadjiprocopis.
1378  
1379 This program is free software; you can redistribute it and/or modify it
1380 under the terms of the the Artistic License (2.0). You may obtain a
1381 copy of the full license at:
1382  
1383 L<http://www.perlfoundation.org/artistic_license_2_0>
1384  
1385 Any use, modification, and distribution of the Standard or Modified
1386 Versions is governed by this Artistic License. By using, modifying or
1387 distributing the Package, you accept this license. Do not use, modify,
1388 or distribute the Package, if you do not accept this license.
1389  
1390 If your Modified Version has been derived from a Modified Version made
1391 by someone other than you, you are nevertheless required to ensure that
1392 your Modified Version complies with the requirements of this license.
1393  
1394 This license does not grant you the right to use any trademark, service
1395 mark, tradename, or logo of the Copyright Holder.
1396  
1397 This license includes the non-exclusive, worldwide, free-of-charge
1398 patent license to make, have made, use, offer to sell, sell, import and
1399 otherwise transfer the Package with respect to any patent claims
1400 licensable by the Copyright Holder that are necessarily infringed by the
1401 Package. If you institute patent litigation (including a cross-claim or
1402 counterclaim) against any party alleging that the Package constitutes
1403 direct or contributory patent infringement, then this Artistic License
1404 to you shall terminate on the date that such litigation is filed.
1405  
1406 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
1407 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
1408 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
1409 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
1410 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
1411 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
1412 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
1413 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1414  
1415  
1416 =cut
1417  
1418 4 1; # End of WWW::Mechanize::Chrome::Webshot
↑ Summary script/www-mechanize-webshot.pl »