blib/lib/Selenium/Remote/Driver/UserAgent.pm

filter: Statementstmt 98.8% 85 / 86 Branchbran 81.2% 13 / 16 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 21 / 21 Podpod 100% 1 / 1 totaltotal 91.7% 122 / 133 SCAR 24.0
CC
11
Cov
93%
_desired_options
7.5
_get_size_for
3.0
__ANON__
2.0
CRAP 11.0
↑ Summary
1 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Selenium::Remote::Driver::UserAgent;
4 7 $Selenium::Remote::Driver::UserAgent::VERSION = '0.03';
5 # ABSTRACT: Emulate mobile devices by setting user agents when using webdriver
6 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Cwd qw/abs_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Selenium::Remote::Driver::Firefox::Profile;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 has browserName => (
14 is => 'rw',
15 required => 1,
16 coerce => sub {
17 13 ▶ my $browser = $_[0];
Subroutine __ANON__: called CC=2 SCAR=6.9
18  
19 13 ▶ croak 'Only chrome and firefox are supported.'
Branch: 1 branch
BranchTrueFalseTotal
unless $browser =~ /chrome|firefox/ 1 12 13
20 unless $browser =~ /chrome|firefox/;
21 12 return lc($browser)
22 }
23 7 );
24  
25  
26 has agent => (
27 is => 'rw',
28 required => 1,
29 coerce => sub {
30 14 ▶ my $agent = $_[0];
Subroutine __ANON__: called CC=2 SCAR=6.9
31  
32 14 my @valid = qw/iphone ipad_seven ipad android_phone android_tablet/;
33  
34 14 ▶ croak 'invalid agent' unless grep { $_ eq $agent } @valid;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $agent;} @valid 1 13 14
35 13 return $agent;
36 }
37 7 );
38  
39  
40 has orientation => (
41 is => 'rw',
42 coerce => sub {
43 12 ▶ croak 'Invalid orientation; please choose "portrait" or "landscape'
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /portrait|landscape/ 1 11 12
44 unless $_[0] =~ /portrait|landscape/;
45 11 return $_[0];
46 },
47 7 default => 'portrait'
48 );
49  
50 has _firefox_options => (
51 is => 'ro',
52 lazy => 1,
53 builder => sub {
54 5 ▶ my ($self) = @_;
Subroutine _build__firefox_options: called CC=1 SCAR=0.0
55  
56 5 my $dim = $self->_get_size;
57  
58 5 my $profile = Selenium::Remote::Driver::Firefox::Profile->new;
59 5 $profile->set_preference(
60 'general.useragent.override' => $self->_get_user_agent
61 );
62  
63 return {
64 5 firefox_profile => $profile
65 };
66 }
67 7 );
68  
69 has _chrome_options => (
70 is => 'ro',
71 lazy => 1,
72 builder => sub {
73 1 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build__chrome_options: called CC=1 SCAR=0.0
74  
75 1 my $size = $self->_get_size_for('chrome');
76  
77 return {
78 1 chromeOptions => {
79 'args' => [
80 'user-agent=' . $self->_get_user_agent,
81 'window-size=' . $size
82 ],
83 'excludeSwitches' => [
84 'ignore-certificate-errors'
85 ]
86 }
87 }
88 }
89 7 );
90  
91 has _specs => (
92 is => 'ro',
93 builder => sub {
94 8 ▶ my $devices_file = abs_path(__FILE__);
Subroutine _build__specs: called CC=1 SCAR=0.0
95 8 $devices_file =~ s/UserAgent\.pm$/devices.json/;
96  
97 8 my $devices;
98 {
99 8 local $/ = undef;
100 8 open (my $fh, "<", $devices_file);
101 8 $devices = from_json(<$fh>);
102 8 close ($fh);
103 }
104  
105 8 return $devices;
106 }
107 7 );
108  
109  
110 sub caps {
111 11 ▶ my ($self, %args) = @_;
Subroutine caps: called CC=1 SCAR=0.0
Pod documented
112  
113 11 my $options = $self->_desired_options(%args);
114  
115 return {
116 11 inner_window_size => $self->_get_size_for('caps'),
117 desired_capabilities => {
118 browserName => $self->browserName,
119 %$options
120 }
121 };
122 }
123  
124 sub _desired_options {
125 11 ▶ my ($self, %args) = @_;
Subroutine _desired_options: called CC=6 SCAR=20.2
126  
127 11 my $options;
128 11 ▶ if ($self->_is_chrome) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_chrome) { } 5 6 11
elsif ($self->_is_firefox) { } 6 0 6
129 5 $options = $self->_chrome_options;
130 }
131 elsif ($self->_is_firefox) {
132 6 $options = $self->_firefox_options;
133  
134 6 ▶ unless (%args && exists $args{unencoded} && $args{unencoded}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%args and exists $args{"unencoded"} and $args{"unencoded"}) 0 6 6
Condition33%2/6
%args and exists $args{"unencoded"} and $args{"unencoded"}
!ll&&!rl&&r
006
%args and exists $args{"unencoded"}
!ll&&!rl&&r
006
MC/DC0%0/3
%args and exists $args{"unencoded"} and $args{"unencoded"}
%args exists $args{"unencoded"} $args{"unencoded"}
Truth table25%1/4
A and B and C
A= %argsB= exists $args{"unencoded"}C= $args{"unencoded"}
ABCresult
0XX0
10X0
1100
1111
135 0 $options->{firefox_profile} = $options->{firefox_profile}->_encode;
136 }
137 }
138  
139 11 return $options;
140 }
141  
142 sub _get_user_agent {
143 11 ▶ my ($self) = @_;
Subroutine _get_user_agent: called CC=1 SCAR=0.0
144  
145 11 my $specs = $self->_specs;
146 11 my $agent = $self->agent;
147  
148 11 return $specs->{$agent}->{user_agent};
149 }
150  
151 sub _get_size {
152 22 ▶ my ($self) = @_;
Subroutine _get_size: called CC=1 SCAR=0.0
153  
154 22 my $specs = $self->_specs;
155 22 my $agent = $self->agent;
156 22 my $orientation = $self->orientation;
157  
158 22 return $specs->{$agent}->{$orientation};
159 }
160  
161 sub _get_size_for {
162 16 ▶ my ($self, $format) = @_;
Subroutine _get_size_for: called CC=3 SCAR=11.0
163 16 my $dim = $self->_get_size;
164  
165 16 ▶ if ($format eq 'caps') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'caps') { } 11 5 16
elsif ($format eq 'chrome') { } 5 0 5
166 11 return [ $dim->{height}, $dim->{width} ];
167 }
168 elsif ($format eq 'chrome') {
169 5 return $dim->{width} . ',' . $dim->{height};
170 }
171 }
172  
173 sub _is_firefox {
174 6 ▶ return shift->browserName =~ /firefox/i
Subroutine _is_firefox: called CC=1 SCAR=0.0
175 }
176  
177 sub _is_chrome {
178 11 ▶ return shift->browserName =~ /chrome/i
Subroutine _is_chrome: called CC=1 SCAR=0.0
179 }
180  
181 7 1;
182  
183 __END__
184  
185 =pod
186  
187 =encoding UTF-8
188  
189 =head1 NAME
190  
191 Selenium::Remote::Driver::UserAgent - Emulate mobile devices by setting user agents when using webdriver
192  
193 =head1 VERSION
194  
195 version 0.03
196  
197 =head1 SYNOPSIS
198  
199 my $dua = Selenium::Remote::Driver::UserAgent->new(
200 browserName => 'chrome',
201 agent => 'iphone'
202 );
203  
204 my $caps = $dua->caps;
205 my $driver = Selenium::Remote::Driver->new_from_caps(%$caps);
206  
207 =head1 DESCRIPTION
208  
209 This package will help you test your websites on mobile devices by
210 convincing your browsers to masquerade as a mobile device. You can
211 start up Firefox or Chrome with the same user agents that your mobile
212 browsers would send, along with the same screen resolution and layout.
213  
214 Although the experience may not be 100% the same as manually testing
215 on an actual mobile device, the advantage of testing this way is that
216 you hardly need any additional infrastructure if you've already got a
217 webdriver testing suite set up.
218  
219 NB: There is a bug in v2.42.2 of the Selenium standalone server for
220 Retina displays, like on MacBook Pros: the scaling for Firefox will be
221 doubled in both the width and height dimensions. You can either use an
222 older version of the standalone server or wait for a new release.
223  
224 =head1 ATTRIBUTES
225  
226 =head2 browserName
227  
228 Required: specify which browser type to use. Currently, we only
229 support C<Chrome> and C<Firefox>.
230  
231 my $dua = Selenium::Remote::Driver::UserAgent->new(
232 browserName => 'chrome',
233 agent => 'ipad'
234 );
235  
236 =head2 agent
237  
238 Required: specify which mobile device type to emulate. Your options
239 are:
240  
241 iphone
242 ipad_seven
243 ipad
244 android_phone
245 android_tablet
246  
247 Usage looks like:
248  
249 my $dua = Selenium::Remote::Driver::UserAgent->new(
250 browserName => 'chrome',
251 agent => 'ipad_seven'
252 );
253  
254 =head2 orientation
255  
256 Optional: specify the orientation of the mobile device. Your options
257 are C<portrait> or C<landscape>; defaults to C<portrait>.
258  
259 =head1 METHODS
260  
261 =head2 caps
262  
263 Call this after initiating the ::UserAgent object to get the
264 capabilities that you should pass to S::R::D's's
265 L<Selenium::Remote::Driver/new_from_caps> function. This function
266 returns a hashref with the following keys:
267  
268 =over 4
269  
270 =item inner_window_size - this will set the window size immediately
271 after browser creation
272  
273 =item desired_capabilities - this will set the browserName and the
274 appropriate options needed
275  
276 =back
277  
278 If you're using Firefox and you'd like to continue editing the Firefox
279 profile before passing it to the Driver, pass in C<unencoded => 1>
280 as the argument to this function.
281  
282 =head1 BUGS
283  
284 Please report any bugs or feature requests on the bugtracker website
285 https://github.com/gempesaw/Selenium-Remote-Driver-UserAgent/issues
286  
287 When submitting a bug or request, please include a test-file or a
288 patch to an existing test-file that illustrates the bug or desired
289 feature.
290  
291 =head1 AUTHOR
292  
293 Daniel Gempesaw <gempesaw@gmail.com>
294  
295 =cut
↑ Summary