Selenium/Firefox/Profile.pm

filter: Statementstmt 89.0% 114 / 128 Branchbran 55.8% 19 / 34 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 96.1% 25 / 26 Podpod 100% 7 / 7 totaltotal 84.3% 173 / 205 SCAR 33.1
CC
24
Cov
82%
_extract_install_rdf
11.3
add_marionette
6.0
_install_extensions
6.0
CRAP 27.3
« Selenium/CanStartBinary/FindBinary.pm ↑ Summary Selenium/Remote/Mock/RemoteConnection.pm »
1 package Selenium::Firefox::Profile;
2 17 $Selenium::Firefox::Profile::VERSION = '1.49';
3 # ABSTRACT: Use custom profiles with Selenium::Remote::Driver
4 # TODO: convert this to Moo!
5  
6 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 ▶use Archive::Zip qw( :ERROR_CODES );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use File::Copy qw(copy);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use IO::Uncompress::Unzip 2.030 qw($UnzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22  
23 sub new {
24 6 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
25 6 my %args = @_;
26  
27 6 my $profile_dir;
28 6 ▶ if ( $args{profile_dir} && -d $args{profile_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'profile_dir'} and -d $args{'profile_dir'}) { } 1 5 6
Condition66%2/3
$args{"profile_dir"} and -d $args{"profile_dir"}
!ll&&!rl&&r
501
MC/DC50%1/2
$args{"profile_dir"} and -d $args{"profile_dir"}
$args{"profile_dir"} -d $args{"profile_dir"}
Truth table66%2/3
A and B
A= $args{"profile_dir"}B= -d $args{"profile_dir"}
ABresult
0X0
100
111
29 1 $profile_dir = $args{profile_dir};
30 }
31 else {
32 5 $profile_dir = File::Temp->newdir();
33 }
34  
35 # TODO: accept user prefs, boolean prefs, and extensions in
36 # constructor
37 6 my $self = {
38 profile_dir => $profile_dir,
39 user_prefs => {},
40 extensions => []
41 };
42 6 ▶ bless $self, $class or die "Can't bless $class: $!";
Branch: 1 branch
BranchTrueFalseTotal
bless $self, $class or die "Can't bless ${class}: $!" 0 6 6
43  
44 6 return $self;
45 }
46  
47  
48 sub set_preference {
49 5 ▶ my ( $self, %prefs ) = @_;
Subroutine set_preference: called CC=5 SCAR=16.1
Pod documented
50  
51 5 foreach ( keys %prefs ) {
52 9 my $value = $prefs{$_};
53 9 my $clean_value = '';
54  
55 9 ▶ if ( JSON::is_bool($value) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (&JSON::is_bool($value)) { } 2 7 9
elsif ($value =~ /^(['"]).*\1$/ or &looks_like_number($value)) { } 3 4 7
56 2 $self->set_boolean_preference( $_, $value );
57 2 next;
58 }
59 ▶ elsif ( $value =~ /^(['"]).*\1$/ or looks_like_number($value) ) {
Condition100%3/3
$value =~ /^(['"]).*\1$/ or &looks_like_number($value)
l!l&&r!l&&!r
214
MC/DC100%2/2
$value =~ /^(['"]).*\1$/ or &looks_like_number($value)
$value =~ /^(['"]).*\1$/ &looks_like_number($value)
Truth table100%3/3
A or B
A= $value =~ /^(['"]).*\1$/B= &looks_like_number($value)
ABresult
1X1
011
000
60  
61 # plain integers: 0, 1, 32768, or integers wrapped in strings:
62 # "0", "1", "20140204". in either case, there's nothing for us
63 # to do.
64 3 $clean_value = $value;
65 }
66 else {
67 # otherwise it's hopefully a string that we'll need to
68 # quote on our own
69 4 $clean_value = '"' . $value . '"';
70 }
71  
72 7 $self->{user_prefs}->{$_} = $clean_value;
73 }
74 }
75  
76  
77 sub set_boolean_preference {
78 3 ▶ my ( $self, %prefs ) = @_;
Subroutine set_boolean_preference: called CC=3 SCAR=11.0
Pod documented
79  
80 3 foreach ( keys %prefs ) {
81 4 my $value = $prefs{$_};
82  
83 4 ▶ $self->{user_prefs}->{$_} = $value ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 2 4
84 }
85 }
86  
87  
88 sub get_preference {
89 15 ▶ my ( $self, $pref ) = @_;
Subroutine get_preference: called CC=1 SCAR=0.0
Pod documented
90  
91 15 return $self->{user_prefs}->{$pref};
92 }
93  
94  
95 sub add_extension {
96 4 ▶ my ( $self, $xpi ) = @_;
Subroutine add_extension: called CC=3 SCAR=11.0
Pod documented
97  
98 4 ▶ croak 'File not found: ' . $xpi unless -e $xpi;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $xpi 1 3 4
99 3 my $xpi_abs_path = abs_path($xpi);
100 3 ▶ croak '$xpi_abs_path: extensions must be in .xpi format'
Branch: 1 branch
BranchTrueFalseTotal
unless $xpi_abs_path =~ /\.xpi$/ 1 2 3
101 unless $xpi_abs_path =~ /\.xpi$/;
102  
103 2 push( @{ $self->{extensions} }, $xpi_abs_path );
104 }
105  
106  
107 sub add_webdriver {
108 1 ▶ my ( $self, $port, $is_marionette ) = @_;
Subroutine add_webdriver: called CC=2 SCAR=7.0
Pod documented
109  
110 1 my $current_user_prefs = $self->{user_prefs};
111  
112 $self->set_preference(
113 # having the user prefs here allows them to overwrite the
114 # mutable loaded prefs
115 1 %{$current_user_prefs},
116  
117 # but the frozen ones cannot be overwritten
118 'webdriver_firefox_port' => $port
119 );
120  
121 1 ▶ if ( !$is_marionette ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_marionette) 1 0 1
122 1 $self->_add_webdriver_xpi;
123 }
124  
125 1 return $self;
126 }
127  
128  
129  
130 sub _add_webdriver_xpi {
131 1 ▶ my ($self) = @_;
Subroutine _add_webdriver_xpi: called CC=1 SCAR=0.0
132  
133 1 my $this_dir = dirname( abs_path(__FILE__) );
134 1 my $webdriver_extension = $this_dir . '/webdriver.xpi';
135  
136 1 $self->add_extension($webdriver_extension);
137 }
138  
139  
140 sub add_marionette {
141 0 ▶ my ( $self, $port ) = @_;
Subroutine add_marionette: not called CC=2 SCAR=17.9
Pod documented
142 0 ▶ return if !$port;
Branch: 1 branch
BranchTrueFalseTotal
unless $port 0 0 0
143 0 $self->set_preference( 'marionette.defaultPrefs.port', $port );
144 }
145  
146 sub _encode {
147 2 ▶ my $self = shift;
Subroutine _encode: called CC=2 SCAR=7.0
148  
149 # The remote webdriver accepts the Firefox profile as a base64
150 # encoded zip file
151 2 $self->_layout_on_disk();
152  
153 1 my $zip = Archive::Zip->new();
154 1 $zip->addTree( $self->{profile_dir} );
155  
156 1 my $string = "";
157 1 open( my $fh, ">", \$string );
158 1 binmode($fh);
159 1 ▶ unless ( $zip->writeToFileHandle($fh) == AZ_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($zip->writeToFileHandle($fh) == 0) 0 1 1
160 0 die 'write error';
161 }
162  
163 1 return encode_base64( $string, '' );
164 }
165  
166 sub _layout_on_disk {
167 3 ▶ my $self = shift;
Subroutine _layout_on_disk: called CC=1 SCAR=0.0
168  
169 3 $self->_write_preferences();
170 3 $self->_install_extensions();
171  
172 2 return $self->{profile_dir};
173 }
174  
175 sub _write_preferences {
176 3 ▶ my $self = shift;
Subroutine _write_preferences: called CC=3 SCAR=11.0
177  
178 3 my $userjs = $self->{profile_dir} . "/user.js";
179 3 ▶ open( my $fh, ">>", $userjs )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $userjs or die "Cannot open $userjs for writing preferences: $!" 0 3 3
180 or die "Cannot open $userjs for writing preferences: $!";
181  
182 3 foreach ( keys %{ $self->{user_prefs} } ) {
183 7 print $fh 'user_pref("'
184 . $_ . '", '
185 . $self->get_preference($_) . ');' . "\n";
186 }
187 3 close($fh);
188 }
189  
190 sub _install_extensions {
191 3 ▶ my $self = shift;
Subroutine _install_extensions: called CC=4 SCAR=17.9
192 3 my $extension_dir = $self->{profile_dir} . "/extensions/";
193 3 ▶ mkdir $extension_dir unless -d $extension_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $extension_dir 3 0 3
194  
195 # TODO: handle extensions that need to be unpacked
196 3 foreach my $xpi ( @{ $self->{extensions} } ) {
197  
198 # For Firefox to recognize the extension, we have to put the
199 # .xpi in the /extensions/ folder and change the filename to
200 # its id, which is found in the install.rdf in the root of the
201 # zip.
202  
203 1 my $rdf_string = $self->_extract_install_rdf($xpi);
204 0 my $rdf = XMLin($rdf_string);
205 0 my $name = $rdf->{Description}->{'em:id'};
206  
207 0 my $xpi_dest = $extension_dir . $name . ".xpi";
208 0 ▶ copy( $xpi, $xpi_dest )
Branch: 1 branch
BranchTrueFalseTotal
copy($xpi, $xpi_dest) or croak("Error copying $_ to $xpi_dest : $!") 0 0 0
209 or croak "Error copying $_ to $xpi_dest : $!";
210 }
211 }
212  
213 sub _extract_install_rdf {
214 1 ▶ my ( $self, $xpi ) = @_;
Subroutine _extract_install_rdf: called CC=5 SCAR=24.2
215  
216 1 ▶ my $unzipped = IO::Uncompress::Unzip->new($xpi)
Branch: 1 branch
BranchTrueFalseTotal
my $unzipped = "IO::Uncompress::Unzip"->new($xpi) or die "Cannot unzip ${xpi}: $Selenium::Firefox::Profile::UnzipError" 0 1 1
217 or die "Cannot unzip $xpi: $UnzipError";
218  
219 1 my $install_rdf = '';
220 1 ▶ while ( $unzipped->nextStream ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unzipped->nextStream) 0 1 1
221 0 my $filename = $unzipped->getHeaderInfo->{Name};
222 0 ▶ if ( $filename eq 'install.rdf' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq "install.rdf") 0 0 0
223 0 my $buffer;
224 0 ▶ while ( ( my $status = $unzipped->read($buffer) ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $status = $unzipped->read($buffer)) > 0) 0 0 0
225 0 $install_rdf .= $buffer;
226 }
227 0 return $install_rdf;
228 }
229 }
230  
231 croak
232 1 'Invalid Firefox extension: could not find install.rdf in the .XPI at: '
233 . $xpi;
234 }
235  
236 17 1;
237  
238 __END__
239  
240 =pod
241  
242 =encoding UTF-8
243  
244 =head1 NAME
245  
246 Selenium::Firefox::Profile - Use custom profiles with Selenium::Remote::Driver
247  
248 =head1 VERSION
249  
250 version 1.49
251  
252 =head1 DESCRIPTION
253  
254 You can use this module to create a custom Firefox Profile for your
255 Selenium tests. Currently, you can set browser preferences and add
256 extensions to the profile before passing it in the constructor for a
257 new L<Selenium::Remote::Driver> or L<Selenium::Firefox>.
258  
259 =head1 SYNPOSIS
260  
261 use Selenium::Remote::Driver;
262 use Selenium::Firefox::Profile;
263  
264 my $profile = Selenium::Firefox::Profile->new;
265 $profile->set_preference(
266 'browser.startup.homepage' => 'http://www.google.com',
267 'browser.cache.disk.capacity' => 358400
268 );
269  
270 $profile->set_boolean_preference(
271 'browser.shell.checkDefaultBrowser' => 0
272 );
273  
274 $profile->add_extension('t/www/redisplay.xpi');
275  
276 my $driver = Selenium::Remote::Driver->new(
277 'firefox_profile' => $profile
278 );
279  
280 $driver->get('http://www.google.com');
281 print $driver->get_title();
282  
283 =head1 CONSTRUCTOR
284  
285 =head2 new (%args)
286  
287 profile_dir - <string> directory to look for the firefox profile. Defaults to a Tempdir.
288  
289 =head1 METHODS
290  
291 =head2 set_preference
292  
293 Set string and integer preferences on the profile object. You can set
294 multiple preferences at once. If you need to set a boolean preference,
295 either use JSON::true/JSON::false, or see C<set_boolean_preference()>.
296  
297 $profile->set_preference("quoted.integer.pref" => '"20140314220517"');
298 # user_pref("quoted.integer.pref", "20140314220517");
299  
300 $profile->set_preference("plain.integer.pref" => 9005);
301 # user_pref("plain.integer.pref", 9005);
302  
303 $profile->set_preference("string.pref" => "sample string value");
304 # user_pref("string.pref", "sample string value");
305  
306 =head2 set_boolean_preference
307  
308 Set preferences that require boolean values of 'true' or 'false'. You
309 can set multiple preferences at once. For string or integer
310 preferences, use C<set_preference()>.
311  
312 $profile->set_boolean_preference("false.pref" => 0);
313 # user_pref("false.pref", false);
314  
315 $profile->set_boolean_preference("true.pref" => 1);
316 # user_pref("true.pref", true);
317  
318 =head2 get_preference
319  
320 Retrieve the computed value of a preference. Strings will be double
321 quoted and boolean values will be single quoted as "true" or "false"
322 accordingly.
323  
324 $profile->set_boolean_preference("true.pref" => 1);
325 print $profile->get_preference("true.pref") # true
326  
327 $profile->set_preference("string.pref" => "an extra set of quotes");
328 print $profile->get_preference("string.pref") # "an extra set of quotes"
329  
330 =head2 add_extension
331  
332 Add an existing C<.xpi> to the profile by providing its path. This
333 only works with packaged C<.xpi> files, not plain/un-packed extension
334 directories.
335  
336 $profile->add_extension('t/www/redisplay.xpi');
337  
338 =head2 add_webdriver
339  
340 Primarily for internal use, we set the appropriate firefox preferences
341 for a new geckodriver session.
342  
343 =head2 add_webdriver_xpi
344  
345 Primarily for internal use. This adds the fxgoogle .xpi that is used
346 for webdriver communication in FF47 and older. For FF48 and newer, the
347 old method using an extension to orchestrate the webdriver
348 communication with the Firefox browser has been obsoleted by the
349 introduction of C<geckodriver>.
350  
351 =head2 add_marionette
352  
353 Primarily for internal use, configure Marionette to the
354 current Firefox profile.
355  
356 =head1 SEE ALSO
357  
358 http://kb.mozillazine.org/About:config_entries
359 https://developer.mozilla.org/en-US/docs/Mozilla/Preferences/A_brief_guide_to_Mozilla_preferences
360  
361 =head1 AUTHORS
362  
363 Current Maintainers:
364  
365 =over 4
366  
367 =item *
368  
369 George S. Baugh <george@troglodyne.net>
370  
371 =back
372  
373 Previous maintainers:
374  
375 =over 4
376  
377 =item *
378  
379 Daniel Gempesaw <gempesaw@gmail.com>
380  
381 =item *
382  
383 Emmanuel Peroumalnaïk <peroumalnaik.emmanuel@gmail.com>
384  
385 =item *
386  
387 Luke Closs <cpan@5thplane.com>
388  
389 =item *
390  
391 Mark Stosberg <mark@stosberg.com>
392  
393 =back
394  
395 Original authors:
396  
397 =over 4
398  
399 =item *
400  
401 Aditya Ivaturi <ivaturi@gmail.com>
402  
403 =back
404  
405 =head1 COPYRIGHT AND LICENSE
406  
407 Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child
408  
409 Copyright (c) 2014-2017 Daniel Gempesaw
410  
411 Copyright (c) 2018-2021 George S. Baugh
412  
413 Licensed under the Apache License, Version 2.0 (the "License");
414 you may not use this file except in compliance with the License.
415 You may obtain a copy of the License at
416  
417 http://www.apache.org/licenses/LICENSE-2.0
418  
419 Unless required by applicable law or agreed to in writing, software
420 distributed under the License is distributed on an "AS IS" BASIS,
421 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
422 See the License for the specific language governing permissions and
423 limitations under the License.
424  
425 =cut
« Selenium/CanStartBinary/FindBinary.pm ↑ Summary Selenium/Remote/Mock/RemoteConnection.pm »