line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
22
|
|
|
22
|
|
7488458
|
use strict; |
|
22
|
|
|
|
|
66
|
|
|
22
|
|
|
|
|
798
|
|
2
|
22
|
|
|
22
|
|
131
|
use warnings; |
|
22
|
|
|
|
|
23
|
|
|
22
|
|
|
|
|
1337
|
|
3
|
|
|
|
|
|
|
package Selenium::Remote::Driver::UserAgent; |
4
|
|
|
|
|
|
|
$Selenium::Remote::Driver::UserAgent::VERSION = '0.03'; |
5
|
|
|
|
|
|
|
# ABSTRACT: Emulate mobile devices by setting user agents when using webdriver |
6
|
22
|
|
|
22
|
|
949
|
use Moo; |
|
22
|
|
|
|
|
15898
|
|
|
22
|
|
|
|
|
132
|
|
7
|
22
|
|
|
22
|
|
9503
|
use JSON; |
|
22
|
|
|
|
|
17109
|
|
|
22
|
|
|
|
|
194
|
|
8
|
22
|
|
|
22
|
|
3461
|
use Cwd qw/abs_path/; |
|
22
|
|
|
|
|
44
|
|
|
22
|
|
|
|
|
1288
|
|
9
|
22
|
|
|
22
|
|
110
|
use Carp qw/croak/; |
|
22
|
|
|
|
|
43
|
|
|
22
|
|
|
|
|
1242
|
|
10
|
22
|
|
|
22
|
|
22539
|
use Selenium::Remote::Driver::Firefox::Profile; |
|
22
|
|
|
|
|
5060735
|
|
|
22
|
|
|
|
|
21983
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has browserName => ( |
14
|
|
|
|
|
|
|
is => 'rw', |
15
|
|
|
|
|
|
|
required => 1, |
16
|
|
|
|
|
|
|
coerce => sub { |
17
|
|
|
|
|
|
|
my $browser = $_[0]; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
croak 'Only chrome and firefox are supported.' |
20
|
|
|
|
|
|
|
unless $browser =~ /chrome|firefox/; |
21
|
|
|
|
|
|
|
return lc($browser) |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has agent => ( |
27
|
|
|
|
|
|
|
is => 'rw', |
28
|
|
|
|
|
|
|
required => 1, |
29
|
|
|
|
|
|
|
coerce => sub { |
30
|
|
|
|
|
|
|
my $agent = $_[0]; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my @valid = qw/iphone ipad_seven ipad android_phone android_tablet/; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
croak 'invalid agent' unless grep { $_ eq $agent } @valid; |
35
|
|
|
|
|
|
|
return $agent; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has orientation => ( |
41
|
|
|
|
|
|
|
is => 'rw', |
42
|
|
|
|
|
|
|
coerce => sub { |
43
|
|
|
|
|
|
|
croak 'Invalid orientation; please choose "portrait" or "landscape' |
44
|
|
|
|
|
|
|
unless $_[0] =~ /portrait|landscape/; |
45
|
|
|
|
|
|
|
return $_[0]; |
46
|
|
|
|
|
|
|
}, |
47
|
|
|
|
|
|
|
default => 'portrait' |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
has _firefox_options => ( |
51
|
|
|
|
|
|
|
is => 'ro', |
52
|
|
|
|
|
|
|
lazy => 1, |
53
|
|
|
|
|
|
|
builder => sub { |
54
|
31
|
|
|
31
|
|
11267
|
my ($self) = @_; |
55
|
|
|
|
|
|
|
|
56
|
31
|
|
|
|
|
704
|
my $dim = $self->_get_size; |
57
|
|
|
|
|
|
|
|
58
|
31
|
|
|
|
|
642
|
my $profile = Selenium::Remote::Driver::Firefox::Profile->new; |
59
|
31
|
|
|
|
|
41703
|
$profile->set_preference( |
60
|
|
|
|
|
|
|
'general.useragent.override' => $self->_get_user_agent |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return { |
64
|
31
|
|
|
|
|
1540
|
firefox_profile => $profile |
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
has _chrome_options => ( |
70
|
|
|
|
|
|
|
is => 'ro', |
71
|
|
|
|
|
|
|
lazy => 1, |
72
|
|
|
|
|
|
|
builder => sub { |
73
|
10
|
|
|
10
|
|
22776
|
my ($self) = @_; |
74
|
|
|
|
|
|
|
|
75
|
10
|
|
|
|
|
140
|
my $size = $self->_get_size_for('chrome'); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
return { |
78
|
10
|
|
|
|
|
185
|
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
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
has _specs => ( |
92
|
|
|
|
|
|
|
is => 'ro', |
93
|
|
|
|
|
|
|
builder => sub { |
94
|
44
|
|
|
44
|
|
24834260
|
my $devices_file = abs_path(__FILE__); |
95
|
44
|
|
|
|
|
1202
|
$devices_file =~ s/UserAgent\.pm$/devices.json/; |
96
|
|
|
|
|
|
|
|
97
|
44
|
|
|
|
|
481
|
my $devices; |
98
|
|
|
|
|
|
|
{ |
99
|
44
|
|
|
|
|
214
|
local $/ = undef; |
|
44
|
|
|
|
|
394
|
|
100
|
44
|
|
|
|
|
2996
|
open (my $fh, "<", $devices_file); |
101
|
44
|
|
|
|
|
2660
|
$devices = from_json(<$fh>); |
102
|
44
|
|
|
|
|
7609
|
close ($fh); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
44
|
|
|
|
|
1730
|
return $devices; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub caps { |
111
|
41
|
|
|
41
|
1
|
1194
|
my ($self, %args) = @_; |
112
|
|
|
|
|
|
|
|
113
|
41
|
|
|
|
|
469
|
my $options = $self->_desired_options(%args); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
return { |
116
|
41
|
|
|
|
|
15785
|
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
|
41
|
|
|
41
|
|
138
|
my ($self, %args) = @_; |
126
|
|
|
|
|
|
|
|
127
|
41
|
|
|
|
|
88
|
my $options; |
128
|
41
|
100
|
|
|
|
221
|
if ($self->_is_chrome) { |
|
|
50
|
|
|
|
|
|
129
|
10
|
|
|
|
|
488
|
$options = $self->_chrome_options; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
elsif ($self->_is_firefox) { |
132
|
31
|
|
|
|
|
799
|
$options = $self->_firefox_options; |
133
|
|
|
|
|
|
|
|
134
|
31
|
50
|
66
|
|
|
377
|
unless (%args && exists $args{unencoded} && $args{unencoded}) { |
|
|
|
66
|
|
|
|
|
135
|
10
|
|
|
|
|
451
|
$options->{firefox_profile} = $options->{firefox_profile}->_encode; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
41
|
|
|
|
|
366126
|
return $options; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub _get_user_agent { |
143
|
41
|
|
|
41
|
|
134
|
my ($self) = @_; |
144
|
|
|
|
|
|
|
|
145
|
41
|
|
|
|
|
202
|
my $specs = $self->_specs; |
146
|
41
|
|
|
|
|
1531
|
my $agent = $self->agent; |
147
|
|
|
|
|
|
|
|
148
|
41
|
|
|
|
|
1115
|
return $specs->{$agent}->{user_agent}; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub _get_size { |
152
|
82
|
|
|
82
|
|
255
|
my ($self) = @_; |
153
|
|
|
|
|
|
|
|
154
|
82
|
|
|
|
|
5842
|
my $specs = $self->_specs; |
155
|
82
|
|
|
|
|
3151
|
my $agent = $self->agent; |
156
|
82
|
|
|
|
|
18033
|
my $orientation = $self->orientation; |
157
|
|
|
|
|
|
|
|
158
|
82
|
|
|
|
|
14495
|
return $specs->{$agent}->{$orientation}; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub _get_size_for { |
162
|
51
|
|
|
51
|
|
567
|
my ($self, $format) = @_; |
163
|
51
|
|
|
|
|
371
|
my $dim = $self->_get_size; |
164
|
|
|
|
|
|
|
|
165
|
51
|
100
|
|
|
|
620
|
if ($format eq 'caps') { |
|
|
50
|
|
|
|
|
|
166
|
41
|
|
|
|
|
2158
|
return [ $dim->{height}, $dim->{width} ]; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
elsif ($format eq 'chrome') { |
169
|
10
|
|
|
|
|
284
|
return $dim->{width} . ',' . $dim->{height}; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub _is_firefox { |
174
|
31
|
|
|
31
|
|
15617
|
return shift->browserName =~ /firefox/i |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub _is_chrome { |
178
|
41
|
|
|
41
|
|
968
|
return shift->browserName =~ /chrome/i |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
1; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
__END__ |