Screenshot.pm

filter: Statementstmt 36.8% 66 / 179 Branchbran 21.0% 8 / 38 Conditioncond 15.9% 7 / 44 MC/DCmcdc 8.0% 2 / 25 Subroutinesub 41.1% 14 / 34 Podpod 100% 7 / 7 totaltotal 31.8% 104 / 327 CC 38 SCAR 29.1
CC
38
Cov
31%
_img_exclude
56.0
_extract_image
56.0
_set_opponent
30.0
CRAP 18.3
↑ Summary Screenshot/CanPad.pm »
1 package Selenium::Screenshot;
2 1 $Selenium::Screenshot::VERSION = '0.08';
3 # ABSTRACT: Compare and contrast Webdriver screenshots in PNG format
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Image::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Imager qw/:handy/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Imager::Color;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Imager::Fountain;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp qw/croak carp confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Cwd qw/abs_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 has png => (
16 is => 'rwp',
17 lazy => 1,
18 5 ▶ coerce => sub {
Subroutine __ANON__: called CC=5 SCAR=17.1
19 5 my ($png_or_image) = @_;
20  
21 # We are prepared to handle an Imager object, or a base64
22 # encoded png.
23 5 ▶ if ( blessed( $png_or_image ) && $png_or_image->isa('Imager')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($png_or_image) and $png_or_image->isa('Imager')) { } 4 1 5
Condition66%2/3
builtin::blessed($png_or_image) and $png_or_image->isa("Imager")
!ll&&!rl&&r
104
MC/DC50%1/2
builtin::blessed($png_or_image) and $png_or_image->isa("Imager")
builtin::blessed($png_or_image) $png_or_image->isa("Imager")
Truth table66%2/3
A and B
A= builtin::blessed($png_or_image)B= $png_or_image->isa("Imager")
ABresult
0X0
100
111
24 4 return $png_or_image;
25 }
26 else {
27 1 my $data = decode_base64($png_or_image);
28 1 my $image = Imager->new(data => $data);
29  
30 1 ▶ if (! $image && Imager->errstr) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $image and 'Imager'->errstr) { } 1 0 1
Condition33%1/3
not $image and "Imager"->errstr
!ll&&!rl&&r
001
MC/DC0%0/2
not $image and "Imager"->errstr
not $image "Imager"->errstr
Truth table33%1/3
not(A) and B
A= not $imageB= "Imager"->errstr
ABresult
0X0
100
111
31 1 confess "you must provide a base64 encoded png. We were not able to create an Imager object after base64 decoding your input; Imager's error message was:\n\n" . Imager->errstr;
32 }
33 else {
34 0 return $image;
35 }
36 }
37 },
38 1 required => 1
39 );
40  
41  
42 has exclude => (
43 is => 'ro',
44 lazy => 1,
45 0 ▶ default => sub { [ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 ▶ coerce => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
47 0 my ($exclude) = @_;
48  
49 0 foreach my $rect (@{ $exclude }) {
50 croak 'Each exclude region must have size and location.'
51 0 ▶ unless exists $rect->{size} && exists $rect->{location};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rect->{"size"} and exists $rect->{"location"} 0 0 0
Condition0%0/3
exists $rect->{"size"} and exists $rect->{"location"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $rect->{"size"} and exists $rect->{"location"}
exists $rect->{"size"} exists $rect->{"location"}
Truth table0%0/3
A and B
A= exists $rect->{"size"}B= exists $rect->{"location"}
ABresult
0X0
100
111
52 }
53  
54 0 return $exclude;
55 },
56 1 predicate => 1
57 );
58  
59  
60 # TODO: add threshold tests
61 # TODO: provide reference images
62  
63 has threshold => (
64 is => 'ro',
65 lazy => 1,
66 0 ▶ coerce => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 my ($threshold) = @_;
68  
69 0 my $scaling = 255 * sqrt(3) / 100;
70 0 return $threshold * $scaling;
71 },
72 0 ▶ default => sub { 5 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 1 );
74  
75  
76 has folder => (
77 is => 'rw',
78 5 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
79 5 my ($folder) = @_;
80 5 ▶ $folder //= 'screenshots/';
Condition50%1/2
$folder //= "screenshots/"
l!l
50
MC/DC0%0/1
$folder //= "screenshots/"
$folder
Truth table50%1/2
A
A= $folder
Aresult
11
00
81 5 ▶ mkdir $folder unless -d $folder;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $folder 1 4 5
82  
83 5 return abs_path($folder) . '/';
84 },
85 0 ▶ default => sub { 'screenshots/' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 1 );
87  
88  
89 has metadata => (
90 is => 'ro',
91 lazy => 1,
92 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 1 predicate => 'has_metadata'
94 );
95  
96 has _cmp => (
97 is => 'ro',
98 lazy => 1,
99 init_arg => undef,
100 0 ▶ builder => sub {
Subroutine _build__cmp: not called CC=2 SCAR=17.9
101 0 my ($self) = @_;
102 0 my $cmp = Image::Compare->new;
103  
104  
105 0 ▶ if ($self->has_exclude) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_exclude) 0 0 0
106 0 my $png = $self->_img_exclude($self->png);
107 0 $self->_set_png($png);
108 }
109  
110 $cmp->set_image1(
111 0 img => $self->png,
112 type => 'png'
113 );
114  
115 0 return $cmp;
116 }
117 1 );
118  
119 1 with 'Selenium::Screenshot::CanPad';
120  
121  
122 5 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
123 5 my ($orig, $self, %args) = @_;
124  
125 5 ▶ if ($args{target} && $self->_coerce_target($args{target})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'target'} and $self->_coerce_target($args{'target'})) { } 0 5 5
Condition33%1/3
$args{"target"} and $self->_coerce_target($args{"target"})
!ll&&!rl&&r
500
MC/DC0%0/2
$args{"target"} and $self->_coerce_target($args{"target"})
$args{"target"} $self->_coerce_target($args{"target"})
Truth table33%1/3
A and B
A= $args{"target"}B= $self->_coerce_target($args{"target"})
ABresult
0X0
100
111
126 0 my $cropped_png = $self->_crop_to_target($args{png}, $args{target});
127 0 return $self->$orig(%args, png => $cropped_png);
128 }
129 else {
130 5 return $self->$orig(%args);
131 }
132 1 };
133  
134  
135 0 ▶sub compare {
Subroutine compare: not called CC=2 SCAR=17.9
Pod documented
136 0 my ($self, $opponent) = @_;
137 0 $opponent = $self->_set_opponent($opponent);
138  
139 0 ▶ if (not defined $opponent) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opponent) { } 0 0 0
140 0 carp 'No reference was provided or found, so no comparison was done. We\'ve saved a reference at ' . $self->reference;
141 0 return $self->save_reference;
142 }
143 else {
144 0 $self->_cmp->set_method(
145 method => &Image::Compare::AVG_THRESHOLD,
146 args => {
147 type => &Image::Compare::AVG_THRESHOLD::MEAN,
148 value => $self->threshold,
149 }
150 );
151  
152 0 return $self->_cmp->compare;
153 }
154 }
155  
156  
157 0 ▶sub difference {
Subroutine difference: not called CC=1 SCAR=6.9
Pod documented
158 0 my ($self, $opponent) = @_;
159 0 $opponent = $self->_set_opponent($opponent);
160  
161 # We want to range from transparent (no difference) to fuschia at
162 # 100% change.
163 0 my $white = Imager::Color->new(255, 255, 255);
164 0 my $fuschia = Imager::Color->new(240, 18, 190);
165 0 my $scale = Imager::Fountain->simple(
166 positions => [ 0.0, 1.0 ],
167 colors => [ $white, $fuschia ]
168 );
169  
170 0 $self->_cmp->set_method(
171 method => &Image::Compare::IMAGE,
172 args => $scale
173 );
174  
175 # Do the actual pixel by pixel comparison. This can take a while.
176 0 my $diff = $self->_cmp->compare;
177  
178 # Post processing to overlay the difference onto the
179 # opponent. First, subtract a white box from our difference image;
180 # to make everything white transparent instead.
181 0 my $work = Imager->new(
182 xsize => $diff->getwidth,
183 ysize => $diff->getheight,
184 channels => $diff->getchannels
185 );
186 0 $work->box(filled => 1, color => $white );
187 0 $diff = $work->difference(other => $diff);
188  
189 # Place the transparent diff image on top of our opponent -
190 # anything changed will show up on top of the opponent image in
191 # varying degrees of pink.
192 0 $opponent->compose(src => $diff);
193  
194 0 my $name = $self->_diff_filename;
195 0 $opponent->write(file => $name);
196  
197 0 return $name;
198 }
199  
200  
201 0 ▶sub find_opponent {
Subroutine find_opponent: not called CC=2 SCAR=17.9
Pod documented
202 0 my ($self) = @_;
203  
204 0 my $default_reference = $self->reference;
205 0 ▶ if (-e $default_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $default_reference) 0 0 0
206 0 return Imager->new(file => $default_reference);
207 }
208 }
209  
210 0 ▶sub _diff_filename {
Subroutine _diff_filename: not called CC=1 SCAR=6.9
211 0 my ($self) = @_;
212  
213 0 my $diff = $self->filename;
214 0 $diff =~ s/\.png$/-diff.png/;
215  
216 0 return $diff;
217 }
218  
219 0 ▶sub _set_opponent {
Subroutine _set_opponent: not called CC=5 SCAR=34.0
220 0 my ($self, $opponent) = @_;
221 0 ▶ $opponent //= $self->find_opponent;
Condition0%0/3
$opponent //= $self->find_opponent
l!l&&r!l&&!r
000
MC/DC0%0/2
$opponent //= $self->find_opponent
$opponent $self->find_opponent
Truth table0%0/3
A //= B
A= $opponentB= $self->find_opponent
ABresult
1X1
011
000
222  
223 # No opponent was provided, and we can't find one.
224 0 ▶ return unless $opponent;
Branch: 1 branch
BranchTrueFalseTotal
unless $opponent 0 0 0
225  
226 0 $opponent = $self->_extract_image( $opponent );
227  
228 # Before setting this $opponent as our image2, we must ensure that
229 # our $self->png image and this $opponent are the same size.
230 0 ▶ if (! $self->cmp_image_dims( $self->png, $opponent )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->cmp_image_dims($self->png, $opponent)) 0 0 0
231 0 my ($new_png, $new_opp) = $self->coerce_image_size( $self->png, $opponent );
232 0 $self->_set_png( $new_png );
233 0 $opponent = $new_opp;
234 }
235  
236 0 ▶ $opponent = $self->_img_exclude( $opponent ) if $self->has_exclude;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_exclude 0 0 0
237  
238 0 $self->_cmp->set_image2( img => $opponent );
239  
240 0 return $opponent;
241 }
242  
243  
244 3 ▶sub filename {
Subroutine filename: called CC=4 SCAR=13.9
Pod documented
245 3 my ($self, %overrides) = @_;
246  
247 3 my @filename_parts;
248 3 ▶ if ($self->has_metadata or %overrides) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_metadata or %overrides) { } 2 1 3
Condition66%2/3
$self->has_metadata or %overrides
l!l&&r!l&&!r
201
MC/DC50%1/2
$self->has_metadata or %overrides
$self->has_metadata %overrides
Truth table66%2/3
A or B
A= $self->has_metadataB= %overrides
ABresult
1X1
011
000
249 my $metadata = {
250 2 %{ $self->metadata},
251 %overrides
252 };
253  
254 2 foreach (sort keys %{ $metadata }) {
255 2 push @filename_parts, $self->_sanitize_string($metadata->{$_});
256 }
257 }
258 else {
259 1 push @filename_parts, time
260 }
261  
262 3 my $filename = $self->folder . join('-', @filename_parts) . '.png';
263 3 $filename =~ s/\-+/-/g;
264 3 return $filename;
265 }
266  
267  
268 0 ▶sub reference {
Subroutine reference: not called CC=1 SCAR=6.9
Pod documented
269 0 my ($self) = @_;
270  
271 0 my $default_reference = $self->filename;
272 0 $default_reference =~ s/\.png$/-reference.png/;
273  
274 0 return $default_reference;
275 }
276  
277  
278 0 ▶sub save {
Subroutine save: not called CC=1 SCAR=6.9
Pod documented
279 0 my ($self, %overrides) = @_;
280  
281 0 my $png = $self->png;
282 0 my $filename = $self->filename(%overrides);
283 0 $png->write(file => $filename);
284  
285 0 return $filename;
286 }
287  
288  
289 0 ▶sub save_reference {
Subroutine save_reference: not called CC=1 SCAR=6.9
Pod documented
290 0 my ($self) = @_;
291  
292 0 my $png = $self->png;
293 0 $png->write(file => $self->reference);
294  
295 0 return $self->reference;
296 }
297  
298 0 ▶sub _img_exclude {
Subroutine _img_exclude: not called CC=7 SCAR=40.3
299 0 my ($self, $img, $exclude) = @_;
300 0 ▶ $exclude //= $self->exclude;
Condition0%0/3
$exclude //= $self->exclude
l!l&&r!l&&!r
000
MC/DC0%0/2
$exclude //= $self->exclude
$exclude $self->exclude
Truth table0%0/3
A //= B
A= $excludeB= $self->exclude
ABresult
1X1
011
000
301  
302 0 my $copy = $img->copy;
303  
304 0 foreach my $rect (@{ $exclude }) {
305 0 my ($size, $loc) = ($rect->{size}, $rect->{location});
306  
307 # skip items that don't have the valid keys
308 0 ▶ unless (exists $loc->{x}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $loc->{"x"} and exists $loc->{"y"} and exists $size->{"width"} and exists $size->{"height"}) 0 0 0
Condition0%0/9
exists $loc->{"x"} and exists $loc->{"y"} and exists $size->{"width"} and exists $size->{"height"}
!ll&&!rl&&r
000
exists $loc->{"x"} and exists $loc->{"y"} and exists $size->{"width"}
!ll&&!rl&&r
000
exists $loc->{"x"} and exists $loc->{"y"}
!ll&&!rl&&r
000
MC/DC0%0/4
exists $loc->{"x"} and exists $loc->{"y"} and exists $size->{"width"} and exists $size->{"height"}
exists $loc->{"x"} exists $loc->{"y"} exists $size->{"width"} exists $size->{"height"}
Truth table0%0/5
A and B and C and D
A= exists $loc->{"x"}B= exists $loc->{"y"}C= exists $size->{"width"}D= exists $size->{"height"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
309 && exists $loc->{y}
310 && exists $size->{width}
311 && exists $size->{height}) {
312 0 next;
313 }
314  
315 my $top_left = {
316 x => $loc->{x},
317 y => $loc->{y}
318 0 };
319  
320 my $bottom_right = {
321 x => $loc->{x} + $size->{width},
322 y => $loc->{y} + $size->{height}
323 0 };
324  
325 $copy->box(
326 xmin => $top_left->{x},
327 ymin => $top_left->{y},
328 xmax => $bottom_right->{x},
329 ymax => $bottom_right->{y},
330 0 filled => 1,
331 color => 'black'
332 );
333 }
334  
335 0 return $copy;
336 }
337  
338 0 ▶sub _crop_to_target {
Subroutine _crop_to_target: not called CC=1 SCAR=6.9
339 0 my ($self, $img, $target) = @_;
340 0 $img = $self->_extract_image( $img );
341  
342 0 my ($size, $loc) = ($target->{size}, $target->{location});
343  
344 0 my $left = $loc->{x};
345 0 my $top = $loc->{y};
346 0 my $right = $left + $size->{width};
347 0 my $bottom = $top + $size->{height};
348  
349 # copy returns the cropped image, unlike box
350 0 return $img->crop(
351 left => $left,
352 top => $top,
353 right => $right,
354 bottom => $bottom
355 );
356 }
357  
358 0 ▶sub _img_target {
Subroutine _img_target: not called CC=1 SCAR=6.9
359 0 my ($self) = shift;
360 0 return $self->_crop_to_target( @_ );
361 }
362  
363 0 ▶sub _coerce_target {
Subroutine _coerce_target: not called CC=5 SCAR=34.0
364 0 my ($self, $target) = @_;
365 0 my ($size, $loc) = ($target->{size}, $target->{location});
366  
367 0 ▶ unless (exists $loc->{x}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $loc->{"x"} and exists $loc->{"y"} and exists $size->{"width"} and exists $size->{"height"}) 0 0 0
Condition0%0/9
exists $loc->{"x"} and exists $loc->{"y"} and exists $size->{"width"} and exists $size->{"height"}
!ll&&!rl&&r
000
exists $loc->{"x"} and exists $loc->{"y"} and exists $size->{"width"}
!ll&&!rl&&r
000
exists $loc->{"x"} and exists $loc->{"y"}
!ll&&!rl&&r
000
MC/DC0%0/4
exists $loc->{"x"} and exists $loc->{"y"} and exists $size->{"width"} and exists $size->{"height"}
exists $loc->{"x"} exists $loc->{"y"} exists $size->{"width"} exists $size->{"height"}
Truth table0%0/5
A and B and C and D
A= exists $loc->{"x"}B= exists $loc->{"y"}C= exists $size->{"width"}D= exists $size->{"height"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
368 && exists $loc->{y}
369 && exists $size->{width}
370 && exists $size->{height}) {
371 0 croak 'Target is of incorrect format';
372 }
373  
374 0 return 1;
375 }
376  
377 2 ▶sub _sanitize_string {
Subroutine _sanitize_string: called CC=1 SCAR=0.0
378 2 my ($self, $dirty_string) = @_;
379  
380 2 $dirty_string =~ s/[^A-z0-9\.\-]/-/g;
381 2 return $dirty_string;
382 }
383  
384 0 ▶sub _extract_image {
Subroutine _extract_image: not called CC=7 SCAR=40.3
385 0 my ($self, $file_or_image) = @_;
386  
387 0 my $err_msg = 'We were expecting one of: a filename, Imager object, or Selenium::Screenshot object';
388 0 ▶ croak $err_msg unless defined $file_or_image;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_or_image 0 0 0
389  
390 0 ▶ if ( blessed( $file_or_image ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($file_or_image)) { } 0 0 0
391 0 ▶ if ($file_or_image->isa('Selenium::Screenshot')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file_or_image->isa('Selenium::Screenshot')) { } 0 0 0
elsif ($file_or_image->isa('Imager')) { } 0 0 0
392 0 return $file_or_image->png;
393 }
394 elsif ($file_or_image->isa('Imager')) {
395 0 return $file_or_image;
396 }
397 else {
398 0 croak $err_msg;
399 }
400 }
401 else {
402 0 ▶ if ($file_or_image !~ /\n/ && -e $file_or_image) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $file_or_image =~ /\n/ and -e $file_or_image) { } 0 0 0
Condition0%0/3
not $file_or_image =~ /\n/ and -e $file_or_image
!ll&&!rl&&r
000
MC/DC0%0/2
not $file_or_image =~ /\n/ and -e $file_or_image
not $file_or_image =~ /\n/ -e $file_or_image
Truth table0%0/3
not(A) and B
A= not $file_or_image =~ /\n/B= -e $file_or_image
ABresult
0X0
100
111
403 0 return Imager->new(file => $file_or_image);
404 }
405 else {
406 0 return Imager->new(data => decode_base64($file_or_image));
407 }
408  
409 }
410 }
411  
412  
413 1 1;
414  
415 __END__
↑ Summary Screenshot/CanPad.pm »