blib/lib/WebDriver/Tiny.pm

filter: Statementstmt 85.6% 119 / 139 Branchbran 61.7% 21 / 34 Conditioncond 45.2% 19 / 42 MC/DCmcdc 22.7% 5 / 22 Subroutinesub 91.1% 41 / 45 Podpod 96.6% 29 / 30 totaltotal 75.0% 234 / 312 SCAR 43.3
CC
43
Cov
74%
find
23.4
new
13.1
screenshot
10.4
CRAP 75.7
↑ Summary blib/lib/WebDriver/Tiny/Elements.pm »
1 package WebDriver::Tiny 0.103;
2  
3 40 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use feature qw/lexical_subs postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # https://www.w3.org/TR/webdriver/#elements
9 my sub ELEMENT_ID :prototype() {'element-6066-11e4-a52e-4f735466cecf'}
10  
11 # Allow "cute" $drv->('selector') syntax.
12 #
13 # $self->[4] contains a sub ref that closes over a weakened $self that calls
14 # find, i.e. sub { $weak_self->find(@_) }. This sub ref is returned when $self
15 # is invoked as a sub ref thanks to the magic of overloading. We weaken to
16 # avoid a memory leak. The closure is built in new().
17 40 ▶use overload fallback => 1, '&{}' => sub { $_[0][4] };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 40 ▶use Carp 1.25 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use WebDriver::Tiny::Elements;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 40 our @CARP_NOT = 'WebDriver::Tiny::Elements';
25  
26 sub import {
27 # https://www.w3.org/TR/webdriver/#sendkeys
28 4 ▶ state $chars = {
Subroutine import: called CC=1 SCAR=0.0
29 WD_NULL => 57344, WD_CANCEL => 57345,
30 WD_HELP => 57346, WD_BACK_SPACE => 57347,
31 WD_TAB => 57348, WD_CLEAR => 57349,
32 WD_RETURN => 57350, WD_ENTER => 57351,
33 WD_SHIFT => 57352, WD_CONTROL => 57353,
34 WD_ALT => 57354, WD_PAUSE => 57355,
35 WD_ESCAPE => 57356, WD_SPACE => 57357,
36 WD_PAGE_UP => 57358, WD_PAGE_DOWN => 57359,
37 WD_END => 57360, WD_HOME => 57361,
38 WD_ARROW_LEFT => 57362, WD_ARROW_UP => 57363,
39 WD_ARROW_RIGHT => 57364, WD_ARROW_DOWN => 57365,
40 WD_INSERT => 57366, WD_DELETE => 57367,
41 WD_SEMICOLON => 57368, WD_EQUALS => 57369,
42 WD_NUMPAD0 => 57370, WD_NUMPAD1 => 57371,
43 WD_NUMPAD2 => 57372, WD_NUMPAD3 => 57373,
44 WD_NUMPAD4 => 57374, WD_NUMPAD5 => 57375,
45 WD_NUMPAD6 => 57376, WD_NUMPAD7 => 57377,
46 WD_NUMPAD8 => 57378, WD_NUMPAD9 => 57379,
47 WD_MULTIPLY => 57380, WD_ADD => 57381,
48 WD_SEPARATOR => 57382, WD_SUBTRACT => 57383,
49 WD_DECIMAL => 57384, WD_DIVIDE => 57385,
50 WD_F1 => 57393, WD_F2 => 57394,
51 WD_F3 => 57395, WD_F4 => 57396,
52 WD_F5 => 57397, WD_F6 => 57398,
53 WD_F7 => 57399, WD_F8 => 57400,
54 WD_F9 => 57401, WD_F10 => 57402,
55 WD_F11 => 57403, WD_F12 => 57404,
56 WD_META => 57405, WD_COMMAND => 57405,
57 WD_ZENKAKU_HANKAKU => 57408,
58 };
59  
60 4 require charnames;
61  
62 4 charnames->import( ':alias' => $chars );
63 }
64  
65 # We're a blessed arrayref (for speed) of the following parts:
66 #
67 # $self = [
68 # 0: HTTP::Tiny instance,
69 # 1: URL of the WebDriver daemon,
70 # 2: Base URL which schemeless get calls are based off,
71 # 3: The capabilities of the WebDriver daemon,
72 # 4: Cached closure of $self for ->() syntax,
73 # ]
74 ▶sub new($class, %args) {
Subroutine new: called CC=7 SCAR=25.7
Pod documented
75 Carp::croak qq/$class - Missing required parameter "port"/
76 2 ▶ unless exists $args{port};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"port"} 1 1 2
77  
78 1 ▶ $args{host} //= 'localhost';
Condition50%1/2
$args{"host"} //= "localhost"
l!l
01
MC/DC0%0/1
$args{"host"} //= "localhost"
$args{"host"}
Truth table50%1/2
A
A= $args{"host"}
Aresult
11
00
79 1 ▶ $args{path} //= '';
Condition50%1/2
$args{"path"} //= ""
l!l
01
MC/DC0%0/1
$args{"path"} //= ""
$args{"path"}
Truth table50%1/2
A
A= $args{"path"}
Aresult
11
00
80  
81 my $self = bless [
82 HTTP::Tiny->new,
83 "http://$args{host}:$args{port}$args{path}/session",
84 1 ▶ $args{base_url} // '',
Condition50%1/2
$args{"base_url"} // ""
l!l
01
MC/DC0%0/1
$args{"base_url"} // ""
$args{"base_url"}
Truth table50%1/2
A
A= $args{"base_url"}
Aresult
11
00
85 ], $class;
86  
87 my $reply = $self->_req(
88 POST => '',
89 { capabilities => { alwaysMatch => $args{capabilities} // {} } },
90 1 ▶ );
Condition50%1/2
$args{"capabilities"} // {}
l!l
01
MC/DC0%0/1
$args{"capabilities"} // {}
$args{"capabilities"}
Truth table50%1/2
A
A= $args{"capabilities"}
Aresult
11
00
91  
92 0 $self->[1] .= '/' . $reply->{sessionId};
93  
94 # Store the capabilities.
95 0 $self->[3] = $reply->{capabilities};
96  
97 # Numify bool objects, saves memory.
98 0 $_ += 0 for grep ref eq 'JSON::PP::Boolean', values $self->[3]->%*;
99  
100 # See the overloading at the top of the file for details.
101 0 Scalar::Util::weaken( my $weak_self = $self );
102 0 ▶ $self->[4] = sub { $weak_self->find(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
103  
104 0 $self;
105 }
106  
107 0 ▶sub capabilities($self) { $self->[3] }
Subroutine capabilities: not called CC=1 SCAR=6.9
Pod documented
108  
109 1 ▶sub html($self) { $self->_req( GET => '/source' ) }
Subroutine html: called CC=1 SCAR=0.0
Pod documented
110 1 ▶sub title($self) { $self->_req( GET => '/title' ) }
Subroutine title: called CC=1 SCAR=0.0
Pod documented
111 1 ▶sub url($self) { $self->_req( GET => '/url' ) }
Subroutine url: called CC=1 SCAR=0.0
Pod documented
112  
113 2 ▶sub back($self) { $self->_req( POST => '/back' ); $self }
Subroutine back: called CC=1 SCAR=0.0
Pod documented
114 2 ▶sub forward($self) { $self->_req( POST => '/forward' ); $self }
Subroutine forward: called CC=1 SCAR=0.0
Pod documented
115 2 ▶sub refresh($self) { $self->_req( POST => '/refresh' ); $self }
Subroutine refresh: called CC=1 SCAR=0.0
Pod documented
116  
117 sub status {
118 # /status is the only path without the session prefix, so surpress it.
119 0 ▶ local $_[0][1] = substr $_[0][1], 0, rindex $_[0][1], '/session/';
Subroutine status: not called CC=1 SCAR=6.9
Pod documented
120  
121 0 $_[0]->_req( GET => '/status' );
122 }
123  
124 2 ▶sub alert_accept($self) { $self->_req( POST => '/alert/accept' ); $self }
Subroutine alert_accept: called CC=1 SCAR=0.0
Pod documented
125 2 ▶sub alert_dismiss($self) { $self->_req( POST => '/alert/dismiss' ); $self }
Subroutine alert_dismiss: called CC=1 SCAR=0.0
Pod documented
126  
127 1 ▶sub alert_text($self) { $self->_req( GET => '/alert/text' ) }
Subroutine alert_text: called CC=1 SCAR=0.0
Pod documented
128  
129 sub base_url {
130 8 ▶ if ( @_ == 2 ) {
Subroutine base_url: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 4 4 8
131 4 ▶ $_[0][2] = $_[1] // '';
Condition100%2/2
$_[1] // ""
l!l
31
MC/DC100%1/1
$_[1] // ""
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
132  
133 4 return $_[0];
134 }
135  
136 4 $_[0][2];
137 }
138  
139 sub cookie {
140 4 ▶ my ( $self, $name, $value, @args ) = @_;
Subroutine cookie: called CC=2 SCAR=6.9
Pod documented
141  
142 # GET /cookie/{name} isn't supported by ChromeDriver, so get all.
143 4 ▶ return $self->cookies->{$name} if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 1 3 4
144  
145 3 $self->_req( POST => '/cookie',
146 { cookie => { name => $name, value => $value, @args } } );
147  
148 3 $self;
149 }
150  
151 ▶sub cookie_delete($self, @cookies) {
Subroutine cookie_delete: called CC=3 SCAR=11.0
Pod documented
152 5 ▶ if (@cookies) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cookies) { } 3 2 5
153 3 $self->_req( DELETE => "/cookie/$_" ) for @cookies;
154 }
155 else {
156 2 $self->_req( DELETE => '/cookie' );
157 }
158  
159 5 $self;
160 }
161  
162 sub cookies {
163 2 ▶ my @cookies = @{ $_[0]->_req( GET => '/cookie' ) // [] };
Subroutine cookies: called CC=4 SCAR=17.9
Pod documented
Condition50%1/2
$_[0]->_req("GET", "/cookie") // []
l!l
02
MC/DC0%0/1
$_[0]->_req("GET", "/cookie") // []
$_[0]->_req("GET", "/cookie")
Truth table50%1/2
A
A= $_[0]->_req("GET", "/cookie")
Aresult
11
00
164  
165 # Map the incorrect key to the correct key.
166 2 ▶ $_->{httpOnly} //= delete $_->{httponly} for @cookies;
Condition0%0/3
$_->{"httpOnly"} //= delete $_->{"httponly"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{"httpOnly"} //= delete $_->{"httponly"}
$_->{"httpOnly"} delete $_->{"httponly"}
Truth table0%0/3
A //= B
A= $_->{"httpOnly"}B= delete $_->{"httponly"}
ABresult
1X1
011
000
167  
168 2 +{ map { $_->{name} => $_ } @cookies };
169 }
170  
171 # NOTE This method can be called from a driver or a collection of elements.
172 ▶sub find($self, $selector, %args) {
Subroutine find: called CC=13 SCAR=31.5
Pod documented
173 12 state $methods = {
174 css => 'css selector',
175 ecmascript => 'ecmascript',
176 link_text => 'link text',
177 partial_link_text => 'partial link text',
178 xpath => 'xpath',
179 };
180  
181 12 ▶ my $method = $methods->{ $args{method} // '' } // 'css selector';
Condition100%4/4
$$methods{$args{"method"} // ""} // "css selector"
l!l
102
$args{"method"} // ""
l!l
102
MC/DC100%1/1
$$methods{$args{"method"} // ""} // "css selector"
$$methods{$args{"method"} // ""}
MC/DC100%1/1
$args{"method"} // ""
$args{"method"}
Truth table100%2/2
A
A= $$methods{$args{"method"} // ""}
Aresult
11
00
Truth table100%2/2
A
A= $args{"method"}
Aresult
11
00
182  
183 # FIXME
184 12 ▶ my $drv = ref $self eq 'WebDriver::Tiny::Elements' ? $self->[0] : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self eq 'WebDriver::Tiny::Elements' ? : 0 12 12
185  
186 12 my @ids;
187  
188 12 ▶ for ( 0 .. ( $args{tries} // 5 ) ) {
Condition50%1/2
$args{"tries"} // 5
l!l
012
MC/DC0%0/1
$args{"tries"} // 5
$args{"tries"}
Truth table50%1/2
A
A= $args{"tries"}
Aresult
11
00
189 12 my $reply = $self->_req(
190 POST => '/elements',
191 { using => $method, value => "$selector" },
192 );
193  
194 12 @ids = map $_->{+ELEMENT_ID}, @$reply;
195  
196 @ids = grep {
197 0 $drv->_req( GET => "/element/$_/displayed" )
198 12 ▶ } @ids if $args{visible};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"visible"} 0 12 12
199  
200 12 ▶ last if @ids;
Branch: 1 branch
BranchTrueFalseTotal
if @ids 12 0 12
201  
202 0 ▶ select undef, undef, undef, $args{sleep} // .1;
Condition0%0/2
$args{"sleep"} // "0.1"
l!l
00
MC/DC0%0/1
$args{"sleep"} // "0.1"
$args{"sleep"}
Truth table0%0/2
A
A= $args{"sleep"}
Aresult
11
00
203 }
204  
205 Carp::croak ref $self, qq/->find failed for $method = "$_[1]"/
206 12 ▶ if !@ids && !exists $args{dies} && !$args{dies};
Branch: 1 branch
BranchTrueFalseTotal
unless @ids or exists $args{"dies"} or $args{"dies"} 0 12 12
Condition33%2/6
@ids or exists $args{"dies"} or $args{"dies"}
l!l&&r!l&&!r
1200
@ids or exists $args{"dies"}
l!l&&r!l&&!r
1200
MC/DC0%0/3
@ids or exists $args{"dies"} or $args{"dies"}
@ids exists $args{"dies"} $args{"dies"}
Truth table25%1/4
A or B or C
A= @idsB= exists $args{"dies"}C= $args{"dies"}
ABCresult
1XX1
01X1
0011
0000
207  
208 12 ▶ wantarray ? map { bless [ $drv, $_ ], 'WebDriver::Tiny::Elements' } @ids
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 12 12
209 : bless [ $drv, @ids ], 'WebDriver::Tiny::Elements';
210 }
211  
212 ▶my $js = sub($path, $self, $script, @args) {
Subroutine __ANON__: called CC=2 SCAR=6.9
213 # Currently only takes the first ID in the collection, this should change.
214 $_ = { ELEMENT_ID, $_->[1] }
215 4 for grep ref eq 'WebDriver::Tiny::Elements', @args;
216 4 $self->_req( POST => $path, { script => $script, args => \@args } );
217 40 };
218  
219 2 ▶sub js { unshift @_, '/execute/sync'; goto $js }
Subroutine js: called CC=1 SCAR=0.0
Pod documented
220 1 ▶sub js_async { unshift @_, '/execute/async'; goto $js }
Subroutine js_async: called CC=1 SCAR=0.0
Pod documented
221  
222 ▶sub get($self, $url) {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
223 7 ▶ $self->_req(
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m[^https?://]u ? : 4 3 7
224 POST => '/url',
225 { url => $url =~ m(^https?://) ? $url : $self->[2] . $url },
226 );
227  
228 7 $self;
229 }
230  
231 1 ▶sub screenshot($self, $file = undef) {
Subroutine screenshot: called CC=5 SCAR=23.4
Pod documented
232 1 require MIME::Base64;
233  
234 1 my $data = MIME::Base64::decode_base64(
235 $self->_req( GET => '/screenshot' )
236 );
237  
238 1 ▶ if ( defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 0 1 1
239 0 ▶ open my $fh, '>', $file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die $! 0 0 0
240 0 print $fh $data;
241 0 ▶ close $fh or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die $! 0 0 0
242  
243 0 return $self;
244 }
245  
246 1 $data;
247 }
248  
249 1 ▶sub user_agent($self) { $js->( '/execute/sync', $self, 'return window.navigator.userAgent') }
Subroutine user_agent: called CC=1 SCAR=0.0
Pod documented
250  
251 1 ▶sub window($self) { $self->_req( GET => '/window' ) }
Subroutine window: called CC=1 SCAR=0.0
Pod documented
252 1 ▶sub windows($self) { $self->_req( GET => '/window/handles' ) }
Subroutine windows: called CC=1 SCAR=0.0
Pod documented
253  
254 2 ▶sub window_close($self) { $self->_req( DELETE => '/window' ); $self }
Subroutine window_close: called CC=1 SCAR=0.0
Pod documented
255 1 ▶sub window_fullscreen($self) { $self->_req( POST => '/window/fullscreen' ); $self }
Subroutine window_fullscreen: called CC=1 SCAR=0.0
Pod documented
256 2 ▶sub window_maximize($self) { $self->_req( POST => '/window/maximize' ); $self }
Subroutine window_maximize: called CC=1 SCAR=0.0
Pod documented
257 0 ▶sub window_minimize($self) { $self->_req( POST => '/window/minimize' ); $self }
Subroutine window_minimize: not called CC=1 SCAR=6.9
Pod undocumented
258  
259 sub window_rect {
260 1 ▶ my $self = shift;
Subroutine window_rect: called CC=3 SCAR=11.0
Pod documented
261  
262 1 ▶ return $self->_req( GET => '/window/rect' ) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
263  
264 1 $#_ = 3;
265  
266 1 my %args;
267 1 ▶ @args{ qw/width height x y/ } = map $_ // 0, @_;
Condition100%2/2
$_ // 0
l!l
22
MC/DC100%1/1
$_ // 0
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
268  
269 1 $self->_req( POST => '/window/rect', \%args );
270  
271 1 $self;
272 }
273  
274 ▶sub window_switch($self, $handle) {
Subroutine window_switch: called CC=1 SCAR=0.0
Pod documented
275 2 $self->_req( POST => '/window', { handle => $handle } );
276  
277 2 $self;
278 }
279  
280 73 ▶sub _req($self, $method, $path, $args = undef) {
Subroutine _req: called CC=7 SCAR=22.1
281 73 ▶ my $reply = $self->[0]->request(
Condition100%2/2
$args // {}
l!l
3142
MC/DC100%1/1
$args // {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
282 $method,
283 $self->[1] . $path,
284 { content => JSON::PP::encode_json( $args // {} ) },
285 );
286  
287 73 my $value = eval { JSON::PP::decode_json( $reply->{content} )->{value} };
288  
289 73 ▶ unless ( $reply->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply->{"success"}) 1 72 73
290 my $error = $value
291 ? $value->{message} || $value->{error} || $reply->{content}
292 1 ▶ : $reply->{content};
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 1 1
Condition0%0/6
$value->{"message"} or $value->{"error"} or $reply->{"content"}
l!l&&r!l&&!r
000
$value->{"message"} or $value->{"error"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$value->{"message"} or $value->{"error"} or $reply->{"content"}
$value->{"message"} $value->{"error"} $reply->{"content"}
Truth table0%0/4
A or B or C
A= $value->{"message"}B= $value->{"error"}C= $reply->{"content"}
ABCresult
1XX1
01X1
0011
0000
293  
294 1 Carp::croak ref $self, ' - ', $error;
295 }
296  
297 72 $value;
298 }
299  
300 1 ▶sub DESTROY($self) { $self->_req( DELETE => '' ) if $self->[0] && $self->[3] }
Subroutine DESTROY: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if $self->[0] and $self->[3] 0 1 1
Condition33%1/3
$self->[0] and $self->[3]
!ll&&!rl&&r
010
MC/DC0%0/2
$self->[0] and $self->[3]
$self->[0] $self->[3]
Truth table33%1/3
A and B
A= $self->[0]B= $self->[3]
ABresult
0X0
100
111
301  
302 40 1;
↑ Summary blib/lib/WebDriver/Tiny/Elements.pm »