File Coverage

blib/lib/Selenium/Remote/Driver/UserAgent.pm
Criterion Covered Total %
statement 65 65 100.0
branch 7 10 70.0
condition 4 6 66.6
subroutine 17 17 100.0
pod 1 1 100.0
total 94 99 94.9


line stmt bran cond sub pod time code
1 22     22   4877229 use strict;
  22         44  
  22         757  
2 22     22   109 use warnings;
  22         22  
  22         1062  
3             package Selenium::Remote::Driver::UserAgent;
4             $Selenium::Remote::Driver::UserAgent::VERSION = '0.0301';
5             # ABSTRACT: (DEPRECATED) Use Selenium::UserAgent instead
6 22     22   660 use Moo;
  22         12679  
  22         258  
7 22     22   6696 use JSON;
  22         11966  
  22         151  
8 22     22   2825 use Cwd qw/abs_path/;
  22         23  
  22         1120  
9 22     22   109 use Carp qw/croak/;
  22         1  
  22         861  
10 22     22   13370 use Selenium::Remote::Driver::Firefox::Profile;
  22         3424956  
  22         15653  
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   9246 my ($self) = @_;
55              
56 31         620 my $dim = $self->_get_size;
57              
58 31         577 my $profile = Selenium::Remote::Driver::Firefox::Profile->new;
59 31         21424 $profile->set_preference(
60             'general.useragent.override' => $self->_get_user_agent
61             );
62              
63             return {
64 31         1153 firefox_profile => $profile
65             };
66             }
67             );
68              
69             has _chrome_options => (
70             is => 'ro',
71             lazy => 1,
72             builder => sub {
73 10     10   9146 my ($self) = @_;
74              
75 10         160 my $size = $self->_get_size_for('chrome');
76              
77             return {
78 10         42 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   11077007 my $devices_file = abs_path(__FILE__);
95 44         560 $devices_file =~ s/UserAgent\.pm$/devices.json/;
96              
97 44         94 my $devices;
98             {
99 44         109 local $/ = undef;
  44         310  
100 44         1927 open (my $fh, "<", $devices_file);
101 44         1458 $devices = from_json(<$fh>);
102 44         5092 close ($fh);
103             }
104              
105 44         1457 return $devices;
106             }
107             );
108              
109              
110             sub caps {
111 41     41 1 783 my ($self, %args) = @_;
112              
113 41         335 my $options = $self->_desired_options(%args);
114              
115             return {
116 41         9521 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   83 my ($self, %args) = @_;
126              
127 41         56 my $options;
128 41 100       105 if ($self->_is_chrome) {
    50          
129 10         423 $options = $self->_chrome_options;
130             }
131             elsif ($self->_is_firefox) {
132 31         509 $options = $self->_firefox_options;
133              
134 31 50 66     334 unless (%args && exists $args{unencoded} && $args{unencoded}) {
      66        
135 10         130 $options->{firefox_profile} = $options->{firefox_profile}->_encode;
136             }
137             }
138              
139 41         106968 return $options;
140             }
141              
142             sub _get_user_agent {
143 41     41   84 my ($self) = @_;
144              
145 41         104 my $specs = $self->_specs;
146 41         1137 my $agent = $self->agent;
147              
148 41         661 return $specs->{$agent}->{user_agent};
149             }
150              
151             sub _get_size {
152 82     82   159 my ($self) = @_;
153              
154 82         435 my $specs = $self->_specs;
155 82         1918 my $agent = $self->agent;
156 82         11542 my $orientation = $self->orientation;
157              
158 82         9387 return $specs->{$agent}->{$orientation};
159             }
160              
161             sub _get_size_for {
162 51     51   232 my ($self, $format) = @_;
163 51         202 my $dim = $self->_get_size;
164              
165 51 100       305 if ($format eq 'caps') {
    50          
166 41         864 return [ $dim->{height}, $dim->{width} ];
167             }
168             elsif ($format eq 'chrome') {
169 10         85 return $dim->{width} . ',' . $dim->{height};
170             }
171             }
172              
173             sub _is_firefox {
174 31     31   9815 return shift->browserName =~ /firefox/i
175             }
176              
177             sub _is_chrome {
178 41     41   674 return shift->browserName =~ /chrome/i
179             }
180              
181             1;
182              
183             __END__