blib/lib/Selenium/UserAgent.pm

filter: Statementstmt 91.5% 97 / 106 Branchbran 72.2% 13 / 18 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 91.6% 22 / 24 Podpod 100% 1 / 1 totaltotal 87.3% 138 / 158 CC 12 SCAR 11.2
CC
12
Cov
88%
_get_size_for
12.0
_desired_options
6.1
__ANON__
2.0
CRAP 3.1
↑ Summary
1 package Selenium::UserAgent;
2 2 $Selenium::UserAgent::VERSION = '0.11';
3 # ABSTRACT: Emulate mobile devices by setting user agents when using webdriver
4 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Cwd qw/abs_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use List::Util 1.33 qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Selenium::Firefox::Profile;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 has browserName => (
13 is => 'rw',
14 required => 1,
15 71 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
16 71 my $browser = $_[0];
17  
18 71 ▶ croak 'Only chrome and firefox are supported.'
Branch: 1 branch
BranchTrueFalseTotal
unless $browser =~ /chrome|firefox/ 1 70 71
19 unless $browser =~ /chrome|firefox/;
20 70 return lc($browser)
21 }
22 2 );
23  
24  
25 has agent => (
26 is => 'rw',
27 required => 1,
28 72 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
29 72 my $agent = $_[0];
30  
31 72 my @valid = qw/
32 iphone4
33 iphone5
34 iphone6
35 iphone6plus
36 ipad_mini
37 ipad
38 galaxy_s3
39 galaxy_s4
40 galaxy_s5
41 galaxy_note3
42 nexus4
43 nexus9
44 nexus10
45 /;
46  
47 72 my $updated_agent = _convert_deprecated_agent( $agent );
48  
49 72 ▶ if (any { $_ eq $updated_agent } @valid) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $updated_agent; } , @valid)) { } 71 1 72
50 71 return $updated_agent;
51 }
52 else {
53 1 croak 'invalid agent: "' . $agent . '"';
54 }
55 }
56 2 );
57  
58 72 ▶sub _convert_deprecated_agent {
Subroutine _convert_deprecated_agent: called CC=2 SCAR=6.9
59 72 my ($agent) = @_;
60  
61 72 my %deprecated = (
62 iphone => 'iphone4',
63 ipad_seven => 'ipad',
64 android_phone => 'nexus4',
65 android_tablet => 'nexus10'
66 );
67  
68 72 ▶ if ( exists $deprecated{ $agent }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $deprecated{$agent}) { } 19 53 72
69 # Attempt to return the updated agent key as of v0.06 that will be able to
70 # pass the coercion
71 19 return $deprecated{ $agent };
72 }
73 else {
74 53 return $agent;
75 }
76 }
77  
78  
79 has orientation => (
80 is => 'rw',
81 70 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
82 70 ▶ croak 'Invalid orientation; please choose "portrait" or "landscape'
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /portrait|landscape/ 1 69 70
83 unless $_[0] =~ /portrait|landscape/;
84 69 return $_[0];
85 },
86 2 default => 'portrait'
87 );
88  
89 has _firefox_options => (
90 is => 'ro',
91 lazy => 1,
92 35 ▶ builder => sub {
Subroutine _build__firefox_options: called CC=1 SCAR=0.0
93 35 my ($self) = @_;
94  
95 35 my $dim = $self->_get_size;
96  
97 35 my $profile = Selenium::Firefox::Profile->new;
98 35 $profile->set_preference(
99 'general.useragent.override' => $self->_get_user_agent
100 );
101  
102 return {
103 35 firefox_profile => $profile
104 };
105 }
106 2 );
107  
108 has _chrome_options => (
109 is => 'ro',
110 lazy => 1,
111 0 ▶ builder => sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine _build__chrome_options: called CC=1 SCAR=1.2
112 0 my ($self) = @_;
113  
114 0 my $size = $self->_get_size;
115 0 my $window_size = $size->{width} . ',' . $size->{height};
116  
117 return {
118 0 chromeOptions => {
119 args => [
120 'user-agent=' . $self->_get_user_agent,
121 ],
122 mobileEmulation => {
123 deviceName => $self->_agent_to_chrome
124 }
125 }
126 }
127 }
128 2 );
129  
130 has _specs => (
131 is => 'ro',
132 3 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build__specs: called CC=1 SCAR=0.0
133 3 my $devices_file = abs_path(__FILE__);
134 3 $devices_file =~ s/UserAgent\.pm$/devices.json/;
135  
136 3 my $devices;
137 {
138 3 local $/ = undef;
139 3 open (my $fh, "<", $devices_file);
140 3 $devices = from_json(<$fh>);
141 3 close ($fh);
142 }
143  
144 3 return $devices;
145 }
146 2 );
147  
148  
149 69 ▶sub caps {
Subroutine caps: called CC=1 SCAR=0.0
Pod documented
150 69 my ($self, %args) = @_;
151  
152 69 my $options = $self->_desired_options(%args);
153  
154 return {
155 # inner_window_size => $self->_get_size_for('caps'),
156 69 desired_capabilities => {
157 browserName => $self->browserName,
158 %$options
159 }
160 };
161 }
162  
163 69 ▶sub _desired_options {
Subroutine _desired_options: called CC=6 SCAR=18.1
164 69 my ($self, %args) = @_;
165  
166 69 my $options;
167 69 ▶ if ($self->_is_chrome) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_chrome) { } 34 35 69
elsif ($self->_is_firefox) { } 35 0 35
168 34 $options = $self->_chrome_options;
169 }
170 elsif ($self->_is_firefox) {
171 35 $options = $self->_firefox_options;
172  
173 35 ▶ unless (%args && exists $args{unencoded} && $args{unencoded}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%args and exists $args{"unencoded"} and $args{"unencoded"}) 34 1 35
Condition66%4/6
%args and exists $args{"unencoded"} and $args{"unencoded"}
!ll&&!rl&&r
3401
%args and exists $args{"unencoded"}
!ll&&!rl&&r
3401
MC/DC33%1/3
%args and exists $args{"unencoded"} and $args{"unencoded"}
%args exists $args{"unencoded"} $args{"unencoded"}
Truth table50%2/4
A and B and C
A= %argsB= exists $args{"unencoded"}C= $args{"unencoded"}
ABCresult
0XX0
10X0
1100
1111
174 34 $options->{firefox_profile} = $options->{firefox_profile}->_encode;
175 }
176 }
177  
178 69 return $options;
179 }
180  
181 69 ▶sub _get_user_agent {
Subroutine _get_user_agent: called CC=1 SCAR=0.0
182 69 my ($self) = @_;
183  
184 69 my $specs = $self->_specs;
185 69 my $agent = $self->agent;
186  
187 69 return $specs->{$agent}->{user_agent};
188 }
189  
190 69 ▶sub _get_size {
Subroutine _get_size: called CC=1 SCAR=0.0
191 69 my ($self) = @_;
192  
193 69 my $specs = $self->_specs;
194 69 my $agent = $self->agent;
195 69 my $orientation = $self->orientation;
196  
197 69 my $size = $specs->{$agent}->{$orientation};
198 69 $size->{pixel_ratio} = $specs->{$agent}->{pixel_ratio};
199  
200 69 return $size;
201 }
202  
203 0 ▶sub _get_size_for {
Subroutine _get_size_for: not called CC=3 SCAR=24.8
204 0 my ($self, $format) = @_;
205 0 my $dim = $self->_get_size;
206  
207 0 ▶ if ($format eq 'caps') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'caps') { } 0 0 0
elsif ($format eq 'chrome') { } 0 0 0
208 0 return [ $dim->{height}, $dim->{width} ];
209 }
210 elsif ($format eq 'chrome') {
211 0 return $dim->{width} . ',' . $dim->{height};
212 }
213 }
214  
215 35 ▶sub _is_firefox {
Subroutine _is_firefox: called CC=1 SCAR=0.0
216 35 return shift->browserName =~ /firefox/i
217 }
218  
219 69 ▶sub _is_chrome {
Subroutine _is_chrome: called CC=1 SCAR=0.0
220 69 return shift->browserName =~ /chrome/i
221 }
222  
223 34 ▶sub _agent_to_chrome {
Subroutine _agent_to_chrome: called CC=1 SCAR=0.0
224 34 my ($self) = @_;
225  
226 34 my $map = {
227 iphone4 => 'iPhone 4',
228 iphone5 => 'iPhone 5',
229 iphone6 => 'iPhone 6',
230 iphone6plus => 'iPhone 6 Plus',
231 ipad_mini => 'iPad Mini',
232 ipad => 'iPad',
233 galaxy_s3 => 'Galaxy S III',
234 galaxy_s5 => 'Galaxy S5',
235 galaxy_note3 => 'Galaxy Note 3',
236 nexus4 => 'Nexus 4',
237 nexus9 => 'Nexus 10',
238 nexus10 => 'Nexus 10',
239 iphone => 'iPhone 4',
240 ipad_seven => 'iPad',
241 android_phone => 'Nexus 4',
242 android_tablet => 'Nexus 10'
243 };
244  
245 34 return $map->{$self->agent};
246 }
247  
248 2 1;
249  
250 __END__
↑ Summary