| 1 | package Selenium::Firefox::Profile; | ||||||||||||||||||||||||||||
| 2 | 17 | $Selenium::Firefox::Profile::VERSION = '1.50'; | |||||||||||||||||||||||||||
| 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 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 6 | my $class = shift; | |||||||||||||||||||||||||||
| 25 | 6 | my %args = @_; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 6 | my $profile_dir; | |||||||||||||||||||||||||||
| 28 | 6 | ▶ | if ( $args{profile_dir} && -d $args{profile_dir} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{"profile_dir"} and -d $args{"profile_dir"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 6 | return $self; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 5 | ▶ | sub set_preference { | ||||||||||||||||||||||||||
|
Subroutine
set_preference: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 5 | my ( $self, %prefs ) = @_; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 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 | 3 | ▶ | sub set_boolean_preference { | ||||||||||||||||||||||||||
|
Subroutine
set_boolean_preference: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 3 | my ( $self, %prefs ) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | foreach ( keys %prefs ) { | |||||||||||||||||||||||||||
| 81 | 4 | my $value = $prefs{$_}; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 4 | ▶ | $self->{user_prefs}->{$_} = $value ? 'true' : 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 15 | ▶ | sub get_preference { | ||||||||||||||||||||||||||
|
Subroutine
get_preference: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 15 | my ( $self, $pref ) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 15 | return $self->{user_prefs}->{$pref}; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 4 | ▶ | sub add_extension { | ||||||||||||||||||||||||||
|
Subroutine
add_extension: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 4 | my ( $self, $xpi ) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 4 | ▶ | croak 'File not found: ' . $xpi unless -e $xpi; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 3 | my $xpi_abs_path = abs_path($xpi); | |||||||||||||||||||||||||||
| 100 | 3 | ▶ | croak '$xpi_abs_path: extensions must be in .xpi format' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | unless $xpi_abs_path =~ /\.xpi$/; | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | push( @{ $self->{extensions} }, $xpi_abs_path ); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | sub add_webdriver { | ||||||||||||||||||||||||||
|
Subroutine
add_webdriver: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 1 | my ( $self, $port, $is_marionette ) = @_; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 122 | 1 | $self->_add_webdriver_xpi; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | return $self; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1 | ▶ | sub _add_webdriver_xpi { | ||||||||||||||||||||||||||
|
Subroutine
_add_webdriver_xpi: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 131 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub add_marionette { | ||||||||||||||||||||||||||
|
Subroutine
add_marionette: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 0 | my ( $self, $port ) = @_; | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | return if !$port; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | $self->set_preference( 'marionette.defaultPrefs.port', $port ); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 2 | ▶ | sub _encode { | ||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 147 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 160 | 0 | die 'write error'; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 1 | return encode_base64( $string, '' ); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 3 | ▶ | sub _layout_on_disk { | ||||||||||||||||||||||||||
|
Subroutine
_layout_on_disk: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 167 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 3 | $self->_write_preferences(); | |||||||||||||||||||||||||||
| 170 | 3 | $self->_install_extensions(); | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 2 | return $self->{profile_dir}; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 3 | ▶ | sub _write_preferences { | ||||||||||||||||||||||||||
|
Subroutine
_write_preferences: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 176 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 3 | my $userjs = $self->{profile_dir} . "/user.js"; | |||||||||||||||||||||||||||
| 179 | 3 | ▶ | open( my $fh, ">>", $userjs ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 3 | ▶ | sub _install_extensions { | ||||||||||||||||||||||||||
|
Subroutine
_install_extensions: called CC=4 SCAR=17.9
| |||||||||||||||||||||||||||||
| 191 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 192 | 3 | my $extension_dir = $self->{profile_dir} . "/extensions/"; | |||||||||||||||||||||||||||
| 193 | 3 | ▶ | mkdir $extension_dir unless -d $extension_dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 209 | or croak "Error copying $_ to $xpi_dest : $!"; | ||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 1 | ▶ | sub _extract_install_rdf { | ||||||||||||||||||||||||||
|
Subroutine
_extract_install_rdf: called CC=5 SCAR=24.2
| |||||||||||||||||||||||||||||
| 214 | 1 | my ( $self, $xpi ) = @_; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 1 | ▶ | my $unzipped = IO::Uncompress::Unzip->new($xpi) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | or die "Cannot unzip $xpi: $UnzipError"; | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 1 | my $install_rdf = ''; | |||||||||||||||||||||||||||
| 220 | 1 | ▶ | while ( $unzipped->nextStream ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 0 | my $filename = $unzipped->getHeaderInfo->{Name}; | |||||||||||||||||||||||||||
| 222 | 0 | ▶ | if ( $filename eq 'install.rdf' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 0 | my $buffer; | |||||||||||||||||||||||||||
| 224 | 0 | ▶ | while ( ( my $status = $unzipped->read($buffer) ) > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||