Selenium/Remote/Spec.pm

filter: Statementstmt 87.6% 78 / 89 Branchbran 70.8% 34 / 48 Conditioncond 77.7% 14 / 18 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 80.0% 12 / 15 Podpod 0.0% 0 / 3 totaltotal 78.9% 146 / 185 CC 34 SCAR 26.7
CC
34
Cov
81%
get_spec_differences
30.0
get_params
21.8
parse_response
8.2
CRAP 14.5
« Selenium/CanStartBinary/FindBinary.pm ↑ Summary Selenium/CanStartBinary.pm »
1 package Selenium::Remote::Spec;
2 28 $Selenium::Remote::Spec::VERSION = '1.50';
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Implement commands for Selenium::Remote::Driver
7  
8  
9 28 ▶use Carp qw{croak};
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use List::Util qw{any};
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 28 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 extends 'Selenium::Remote::Commands';
14  
15 #Ripped from the headlines: https://w3c.github.io/webdriver/webdriver-spec.html
16 #then add 2 params for our use
17  
18 #Method URI Template no_content_success internal_name Command
19 28 our $spec = qq{
20 POST session 0 newSession New Session
21 POST session 0 getCapabilities Get Capabilities (v2->v3 shim)
22 DELETE session/:sessionId 1 quit Delete Session
23 GET status 0 status Status
24 GET session/:sessionId/timeouts 0 getTimeouts Get Timeouts
25 POST session/:sessionId/timeouts 1 setTimeout Set Page Load timeout (v2->v3 shim)
26 POST session/:sessionId/timeouts/async_script 1 setAsyncScriptTimeout Set Async script timeout (v2->v3 shim)
27 POST session/:sessionId/timeouts/implicit_wait 1 setImplicitWaitTimeout Set Implicit wait timeout (v2->v3 shim)
28 POST session/:sessionId/url 1 get Navigate To
29 GET session/:sessionId/url 0 getCurrentUrl Get Current URL
30 POST session/:sessionId/back 1 goBack Back
31 POST session/:sessionId/forward 1 goForward Forward
32 POST session/:sessionId/refresh 1 refresh Refresh
33 GET session/:sessionId/title 0 getTitle Get Title
34 GET session/:sessionId/window 0 getCurrentWindowHandle Get Currently Focused Window Handle
35 DELETE session/:sessionId/window 1 close Close Currently Focused Window
36 POST session/:sessionId/window 1 switchToWindow Switch To Window
37 GET session/:sessionId/window/handles 0 getWindowHandles Get Window Handles
38 POST session/:sessionId/frame 1 switchToFrame Switch To Frame
39 POST session/:sessionId/frame/parent 1 switchToParentFrame Switch To Parent Frame
40 GET session/:sessionId/window/rect 0 getWindowRect Get Window Size/Position (v2->v3 shim)
41 POST session/:sessionId/window/rect 1 setWindowRect Set Window Size/Position (v2->v3 shim)
42 POST session/:sessionId/window/maximize 1 maximizeWindow Maximize Window
43 POST session/:sessionId/window/minimize 1 minimizeWindow Minimize Window
44 POST session/:sessionId/window/fullscreen 1 fullscreenWindow Fullscreen Window
45 GET session/:sessionId/element/active 0 getActiveElement Get Active Element
46 POST session/:sessionId/element 0 findElement Find Element
47 POST session/:sessionId/elements 0 findElements Find Elements
48 POST session/:sessionId/element/:id/element 0 findChildElement Find Element From Element
49 POST session/:sessionId/element/:id/elements 0 findChildElements Find Elements From Element
50 GET session/:sessionId/element/:id/selected 0 isElementSelected Is Element Selected
51 GET session/:sessionId/element/:id/attribute/:name 0 getElementAttribute Get Element Attribute
52 GET session/:sessionId/element/:id/property/:name 0 getElementProperty Get Element Property
53 GET session/:sessionId/element/:id/css/:propertyName 0 getElementValueOfCssProperty Get Element CSS Value
54 GET session/:sessionId/element/:id/text 0 getElementText Get Element Text
55 GET session/:sessionId/element/:id/name 0 getElementTagName Get Element Tag Name
56 GET session/:sessionId/element/:id/rect 0 getElementRect Get Element Rect
57 GET session/:sessionId/element/:id/enabled 0 isElementEnabled Is Element Enabled
58 POST session/:sessionId/element/:id/click 1 clickElement Element Click
59 POST session/:sessionId/element/:id/clear 1 clearElement Element Clear
60 POST session/:sessionId/element/:id/value 1 sendKeysToElement Element Send Keys
61 GET session/:sessionId/source 0 getPageSource Get Page Source
62 POST session/:sessionId/execute/sync 0 executeScript Execute Script
63 POST session/:sessionId/execute/async 0 executeAsyncScript Execute Async Script
64 GET session/:sessionId/cookie 0 getAllCookies Get All Cookies
65 GET session/:sessionId/cookie/:name 0 getCookieNamed Get Named Cookie
66 POST session/:sessionId/cookie 1 addCookie Add Cookie
67 DELETE session/:sessionId/cookie/:name 1 deleteCookieNamed Delete Cookie
68 DELETE session/:sessionId/cookie 1 deleteAllCookies Delete All Cookies
69 POST session/:sessionId/actions 1 generalAction Perform Actions
70 DELETE session/:sessionId/actions 1 releaseGeneralAction Release Actions
71 POST session/:sessionId/alert/dismiss 1 dismissAlert Dismiss Alert
72 POST session/:sessionId/alert/accept 1 acceptAlert Accept Alert
73 GET session/:sessionId/alert/text 0 getAlertText Get Alert Text
74 POST session/:sessionId/alert/text 1 sendKeysToPrompt Send Alert Text
75 GET session/:sessionId/screenshot 0 screenshot Take Screenshot
76 GET session/:sessionId/moz/screenshot/full 0 mozScreenshotFull Take Full Screenshot
77 GET session/:sessionId/element/:id/screenshot 0 elementScreenshot Take Element Screenshot
78 };
79  
80 28 our $spec_parsed;
81  
82 2 ▶sub get_spec {
Subroutine _build__cmds: called CC=4 SCAR=13.9
83 2 ▶ return $spec_parsed if $spec_parsed;
Branch: 1 branch
BranchTrueFalseTotal
if $Selenium::Remote::Spec::spec_parsed 1 1 2
84 1 my @split = split( /\n/, $spec );
85 1 foreach my $line (@split) {
86 59 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 1 58 59
87 58 my ( $method, $uri, $nc_success, $key, @description ) =
88 split( / +/, $line );
89 58 $spec_parsed->{$key} = {
90 method => $method,
91 url => $uri,
92 no_content_success => int($nc_success)
93 , #XXX this *should* always be 0, but specs lie
94 description => join( ' ', @description ),
95 };
96 }
97 1 return $spec_parsed;
98 }
99  
100 28 has '_cmds' => (
101 is => 'lazy',
102 reader => 'get_cmds',
103 builder => \&get_spec,
104 );
105  
106  
107 has '_caps' => (
108 is => 'lazy',
109 reader => 'get_caps',
110 16 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build__caps: called CC=1 SCAR=0.0
111 return [
112 16 'browserName', 'acceptInsecureCerts',
113 'browserVersion', 'platformName',
114 'proxy', 'pageLoadStrategy',
115 'setWindowRect', 'timeouts',
116 'unhandledPromptBehavior', 'moz:firefoxOptions',
117 'goog:chromeOptions', 'goog:loggingPrefs',
118 ];
119 }
120 28 );
121  
122 has '_caps_map' => (
123 is => 'lazy',
124 reader => 'get_caps_map',
125 22 ▶ builder => sub {
Subroutine _build__caps_map: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
126 return {
127 22 browserName => 'browserName',
128 acceptSslCerts => 'acceptInsecureCerts',
129 version => 'browserVersion',
130 platform => 'platformName',
131 proxy => 'proxy',
132 };
133 }
134 28 );
135  
136 8 ▶sub get_params {
Subroutine get_params: called CC=21 SCAR=30.8
Pod undocumented
137 8 my ( $self, $args ) = @_;
138 8 ▶ if ( !( defined $args->{'session_id'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"session_id"}) 0 8 8
139 0 return;
140 }
141  
142 #Allow fall-back in the event the command passed doesn't exist
143 8 ▶ return unless $self->get_cmds()->{ $args->{command} };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_cmds->{$args->{"command"}} 0 8 8
144  
145 8 my $url = $self->get_url( $args->{command} );
146  
147 8 my $data = {};
148  
149 # Do the var substitutions.
150 8 $url =~ s/:sessionId/$args->{'session_id'}/;
151 8 $url =~ s/:id/$args->{'id'}/;
152 8 $url =~ s/:name/$args->{'name'}/;
153 8 $url =~ s/:propertyName/$args->{'property_name'}/;
154 8 $url =~ s/:other/$args->{'other'}/;
155 8 $url =~ s/:windowHandle/$args->{'window_handle'}/;
156  
157 8 $data->{'method'} = $self->get_method( $args->{command} );
158 $data->{'no_content_success'} =
159 8 $self->get_no_content_success( $args->{command} );
160 8 $data->{'url'} = $url;
161  
162 #URL & data polyfills for the way selenium2 used to do things, etc
163 8 $data->{payload} = {};
164 8 ▶ if ( $args->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"type"}) 8 0 8
165 $data->{payload}->{pageLoad} = $args->{ms}
166 8 ▶ if $data->{url} =~ m/timeouts$/ && $args->{type} eq 'page load';
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"url"} =~ /timeouts$/ and $args->{"type"} eq "page load" 1 7 8
Condition100%3/3
$data->{"url"} =~ /timeouts$/ and $args->{"type"} eq "page load"
!ll&&!rl&&r
521
MC/DC100%2/2
$data->{"url"} =~ /timeouts$/ and $args->{"type"} eq "page load"
$data->{"url"} =~ /timeouts$/ $args->{"type"} eq "page load"
Truth table100%3/3
A and B
A= $data->{"url"} =~ /timeouts$/B= $args->{"type"} eq "page load"
ABresult
0X0
100
111
167 $data->{payload}->{script} = $args->{ms}
168 8 ▶ if $data->{url} =~ m/timeouts$/ && $args->{type} eq 'script';
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"url"} =~ /timeouts$/ and $args->{"type"} eq "script" 1 7 8
Condition100%3/3
$data->{"url"} =~ /timeouts$/ and $args->{"type"} eq "script"
!ll&&!rl&&r
521
MC/DC100%2/2
$data->{"url"} =~ /timeouts$/ and $args->{"type"} eq "script"
$data->{"url"} =~ /timeouts$/ $args->{"type"} eq "script"
Truth table100%3/3
A and B
A= $data->{"url"} =~ /timeouts$/B= $args->{"type"} eq "script"
ABresult
0X0
100
111
169 $data->{payload}->{implicit} = $args->{ms}
170 8 ▶ if $data->{url} =~ m/timeouts$/ && $args->{type} eq 'implicit';
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"url"} =~ /timeouts$/ and $args->{"type"} eq "implicit" 1 7 8
Condition100%3/3
$data->{"url"} =~ /timeouts$/ and $args->{"type"} eq "implicit"
!ll&&!rl&&r
521
MC/DC100%2/2
$data->{"url"} =~ /timeouts$/ and $args->{"type"} eq "implicit"
$data->{"url"} =~ /timeouts$/ $args->{"type"} eq "implicit"
Truth table100%3/3
A and B
A= $data->{"url"} =~ /timeouts$/B= $args->{"type"} eq "implicit"
ABresult
0X0
100
111
171 }
172  
173 #finder polyfills
174 #orig: class, class_name, css, id, link, link_text, partial_link_text, tag_name, name, xpath
175 #new: "css selector", "link text", "partial link text", "tag name", "xpath"
176 #map: class, class_name, id, name, link = 'css selector'
177 8 ▶ if ( $args->{using} && $args->{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"using"} and $args->{"value"}) 8 0 8
Condition33%1/3
$args->{"using"} and $args->{"value"}
!ll&&!rl&&r
008
MC/DC0%0/2
$args->{"using"} and $args->{"value"}
$args->{"using"} $args->{"value"}
Truth table33%1/3
A and B
A= $args->{"using"}B= $args->{"value"}
ABresult
0X0
100
111
178 $data->{payload}->{using} = 'css selector'
179 8 ▶ if grep { $args->{using} eq $_ } ( 'id', 'class name', 'name' );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$args->{"using"} eq $_;} "id", "class name", "name" 8 0 8
180 $data->{payload}->{value} = "[id='$args->{value}']"
181 8 ▶ if $args->{using} eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"using"} eq "id" 6 2 8
182 $data->{payload}->{value} = ".$args->{value}"
183 8 ▶ if $args->{using} eq 'class name';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"using"} eq "class name" 1 7 8
184 $data->{payload}->{value} = "[name='$args->{value}']"
185 8 ▶ if $args->{using} eq 'name';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"using"} eq "name" 1 7 8
186 }
187 8 ▶ if ( $data->{url} =~ s/timeouts\/async_script$/timeouts/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"url"} =~ s[timeouts/async_script$][timeouts]g) 1 7 8
188 1 $data->{payload}->{script} = $args->{ms};
189 1 $data->{payload}->{type} = 'script'; #XXX chrome doesn't follow the spec
190 }
191 8 ▶ if ( $data->{url} =~ s/timeouts\/implicit_wait$/timeouts/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"url"} =~ s[timeouts/implicit_wait$][timeouts]g) 4 4 8
192 4 $data->{payload}->{implicit} = $args->{ms};
193 $data->{payload}->{type} =
194 4 'implicit'; #XXX chrome doesn't follow the spec
195 }
196 $data->{payload}->{value} = $args->{text}
197 8 ▶ if $args->{text} && $args->{command} ne 'sendKeysToElement';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"text"} and $args->{"command"} ne "sendKeysToElement" 5 3 8
Condition66%2/3
$args->{"text"} and $args->{"command"} ne "sendKeysToElement"
!ll&&!rl&&r
305
MC/DC50%1/2
$args->{"text"} and $args->{"command"} ne "sendKeysToElement"
$args->{"text"} $args->{"command"} ne "sendKeysToElement"
Truth table66%2/3
A and B
A= $args->{"text"}B= $args->{"command"} ne "sendKeysToElement"
ABresult
0X0
100
111
198 $data->{payload}->{handle} = $args->{window_handle}
199 8 ▶ if grep { $args->{command} eq $_ }
Branch: 1 branch
BranchTrueFalseTotal
if grep {$args->{"command"} eq $_;} "fullscreenWindow", "minimizeWindow", "maximizeWindow" 8 0 8
200 qw{fullscreenWindow minimizeWindow maximizeWindow};
201 8 return $data;
202 }
203  
204 2 ▶sub parse_response {
Subroutine parse_response: called CC=7 SCAR=21.1
Pod undocumented
205 2 my ( $self, undef, $resp ) = @_;
206  
207 2 ▶ if ( ref($resp) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $resp eq "HASH") 2 0 2
208 2 ▶ if ( $resp->{cmd_status} && $resp->{cmd_status} eq 'OK' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{"cmd_status"} and $resp->{"cmd_status"} eq "OK") 1 1 2
Condition66%2/3
$resp->{"cmd_status"} and $resp->{"cmd_status"} eq "OK"
!ll&&!rl&&r
011
MC/DC50%1/2
$resp->{"cmd_status"} and $resp->{"cmd_status"} eq "OK"
$resp->{"cmd_status"} $resp->{"cmd_status"} eq "OK"
Truth table66%2/3
A and B
A= $resp->{"cmd_status"}B= $resp->{"cmd_status"} eq "OK"
ABresult
0X0
100
111
209 1 return $resp->{cmd_return};
210 }
211 1 my $msg = "Error while executing command";
212 1 ▶ if ( ref $resp->{cmd_return} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $resp->{'cmd_return'} eq 'HASH') { } 1 0 1
213 $msg .= ": $resp->{cmd_return}{error}"
214 1 ▶ if $resp->{cmd_return}{error};
Branch: 1 branch
BranchTrueFalseTotal
if $resp->{"cmd_return"}{"error"} 1 0 1
215 $msg .= ": $resp->{cmd_return}{message}"
216 1 ▶ if $resp->{cmd_return}{message};
Branch: 1 branch
BranchTrueFalseTotal
if $resp->{"cmd_return"}{"message"} 1 0 1
217 }
218 else {
219 0 $msg .= ": $resp->{cmd_return}";
220 }
221 1 croak $msg;
222 }
223  
224 0 return $resp;
225 }
226  
227 #Utility
228  
229 0 ▶sub get_spec_differences {
Subroutine get_spec_differences: not called CC=5 SCAR=34.0
Pod undocumented
230 0 my $v2_spec = Selenium::Remote::Commands->new()->get_cmds();
231 0 my $v3_spec = Selenium::Remote::Spec->new()->get_cmds();
232  
233 0 foreach my $key ( keys(%$v2_spec) ) {
234 print "v2 $key NOT present in v3 spec!!!\n"
235 0 ▶ unless any { $_ eq $key } keys(%$v3_spec);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $key; } , keys %$v3_spec) 0 0 0
236 }
237 0 foreach my $key ( keys(%$v3_spec) ) {
238 print "v3 $key NOT present in v2 spec!!!\n"
239 0 ▶ unless any { $_ eq $key } keys(%$v2_spec);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $key; } , keys %$v2_spec) 0 0 0
240 }
241 }
242  
243 28 1;
244  
245 __END__
« Selenium/CanStartBinary/FindBinary.pm ↑ Summary Selenium/CanStartBinary.pm »