Profile.pm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package Selenium::Remote::Driver::Firefox::Profile;
  2. # ABSTRACT: Use custom profiles with Selenium::Remote::Driver
  3. use strict;
  4. use warnings;
  5. use Archive::Zip qw( :ERROR_CODES );
  6. use Archive::Extract;
  7. use Carp qw(croak);
  8. use Cwd qw(abs_path);
  9. use File::Copy qw(copy);
  10. use File::Temp;
  11. use MIME::Base64;
  12. use Scalar::Util qw(looks_like_number);
  13. =head1 DESCRIPTION
  14. You can use this module to create a custom Firefox Profile for your
  15. Selenium tests. Currently, you can set browser preferences and add
  16. extensions to the profile before passing it in the constructor for a
  17. new Selenium::Remote::Driver.
  18. =head1 SYNPOSIS
  19. use Selenium::Remote::Driver;
  20. use Selenium::Remote::Driver::Firefox::Profile;
  21. my $profile = Selenium::Remote::Driver::Firefox::Profile->new;
  22. $profile->set_preference(
  23. 'browser.startup.homepage' => 'http://www.google.com',
  24. 'browser.cache.disk.capacity' => 358400
  25. );
  26. $profile->set_boolean_preference(
  27. 'browser.shell.checkDefaultBrowser' => 0
  28. );
  29. $profile->add_extension('t/www/redisplay.xpi');
  30. my $driver = Selenium::Remote::Driver->new(
  31. 'firefox_profile' => $profile
  32. );
  33. $driver->get('http://www.google.com');
  34. print $driver->get_title();
  35. =cut
  36. sub new {
  37. my $class = shift;
  38. # TODO: add handling for a pre-existing profile folder passed into
  39. # the constructor
  40. # TODO: accept user prefs, boolean prefs, and extensions in
  41. # constructor
  42. my $self = {
  43. profile_dir => File::Temp->newdir(),
  44. user_prefs => {},
  45. extensions => []
  46. };
  47. bless $self, $class or die "Can't bless $class: $!";
  48. return $self;
  49. }
  50. =method set_preference
  51. Set string and integer preferences on the profile object. You can set
  52. multiple preferences at once. If you need to set a boolean preference,
  53. see C<set_boolean_preference()>.
  54. $profile->set_preference("quoted.integer.pref" => '"20140314220517"');
  55. # user_pref("quoted.integer.pref", "20140314220517");
  56. $profile->set_preference("plain.integer.pref" => 9005);
  57. # user_pref("plain.integer.pref", 9005);
  58. $profile->set_preference("string.pref" => "sample string value");
  59. # user_pref("string.pref", "sample string value");
  60. =cut
  61. sub set_preference {
  62. my ($self, %prefs) = @_;
  63. foreach (keys %prefs) {
  64. my $value = $prefs{$_};
  65. my $clean_value = '';
  66. if ($value =~ /^(['"]).*\1$/ or looks_like_number($value)) {
  67. # plain integers: 0, 1, 32768, or integers wrapped in strings:
  68. # "0", "1", "20140204". in either case, there's nothing for us
  69. # to do.
  70. $clean_value = $value;
  71. }
  72. else {
  73. # otherwise it's hopefully a string that we'll need to
  74. # quote on our own
  75. $clean_value = '"' . $value . '"';
  76. }
  77. $self->{user_prefs}->{$_} = $clean_value;
  78. }
  79. }
  80. =method set_boolean_preference
  81. Set preferences that require boolean values of 'true' or 'false'. You
  82. can set multiple preferences at once. For string or integer
  83. preferences, use C<set_preference()>.
  84. $profile->set_boolean_preference("false.pref" => 0);
  85. # user_pref("false.pref", false);
  86. $profile->set_boolean_preference("true.pref" => 1);
  87. # user_pref("true.pref", true);
  88. =cut
  89. sub set_boolean_preference {
  90. my ($self, %prefs) = @_;
  91. foreach (keys %prefs) {
  92. my $value = $prefs{$_};
  93. $self->{user_prefs}->{$_} = $value ? 'true' : 'false';
  94. }
  95. }
  96. =method get_preference
  97. Retrieve the computed value of a preference. Strings will be double
  98. quoted and boolean values will be single quoted as "true" or "false"
  99. accordingly.
  100. $profile->set_boolean_preference("true.pref" => 1);
  101. print $profile->get_preference("true.pref") # true
  102. $profile->set_preference("string.pref" => "an extra set of quotes");
  103. print $profile->get_preference("string.pref") # "an extra set of quotes"
  104. =cut
  105. sub get_preference {
  106. my ($self, $pref) = @_;
  107. return $self->{user_prefs}->{$pref};
  108. }
  109. =method add_extension
  110. Add an existing C<.xpi> to the profile by providing its path. This
  111. only works with packaged C<.xpi> files, not plain/un-packed extension
  112. directories.
  113. $profile->add_extension('t/www/redisplay.xpi');
  114. =cut
  115. sub add_extension {
  116. my ($self, $xpi) = @_;
  117. my $xpi_abs_path = abs_path($xpi);
  118. croak "$xpi_abs_path: extensions must be in .xpi format" unless $xpi_abs_path =~ /\.xpi$/;
  119. push (@{$self->{extensions}}, $xpi_abs_path);
  120. }
  121. sub _encode {
  122. my $self = shift;
  123. # The remote webdriver accepts the Firefox profile as a base64
  124. # encoded zip file
  125. $self->_layout_on_disk();
  126. my $zip = Archive::Zip->new();
  127. my $dir_member = $zip->addTree( $self->{profile_dir} );
  128. my $string = "";
  129. open (my $fh, ">", \$string);
  130. binmode($fh);
  131. unless ( $zip->writeToFileHandle($fh) == AZ_OK ) {
  132. die 'write error';
  133. }
  134. return encode_base64($string);
  135. }
  136. sub _layout_on_disk {
  137. my $self = shift;
  138. $self->_write_preferences();
  139. $self->_install_extensions();
  140. return $self->{profile_dir};
  141. }
  142. sub _write_preferences {
  143. my $self = shift;
  144. my $userjs = $self->{profile_dir} . "/user.js";
  145. open (my $fh, ">>", $userjs)
  146. or die "Cannot open $userjs for writing preferences: $!";
  147. foreach (keys %{$self->{user_prefs}}) {
  148. print $fh 'user_pref("' . $_ . '", ' . $self->get_preference($_) . ');' . "\n";
  149. }
  150. close ($fh);
  151. }
  152. sub _install_extensions {
  153. my $self = shift;
  154. my $extension_dir = $self->{profile_dir} . "/extensions/";
  155. mkdir $extension_dir unless -d $extension_dir;
  156. # TODO: handle extensions that need to be unpacked
  157. foreach (@{$self->{extensions}}) {
  158. # For Firefox to recognize the extension, we have to put the
  159. # .xpi in the /extensions/ folder and change the filename to
  160. # its id, which is found in the install.rdf in the root of the
  161. # zip.
  162. my $ae = Archive::Extract->new( archive => $_,
  163. type => "zip");
  164. my $tempDir = File::Temp->newdir();
  165. $ae->extract( to => $tempDir );
  166. my $install = $ae->extract_path();
  167. $install .= '/install.rdf';
  168. open (my $fh, "<", $install)
  169. or croak "No install.rdf inside $_: $!";
  170. my (@file) = <$fh>;
  171. close ($fh);
  172. my @name = grep { chomp; $_ =~ /<em:id>[^{]/ } @file;
  173. $name[0] =~ s/.*<em:id>(.*)<\/em:id>.*/$1/;
  174. my $xpi_dest = $extension_dir . $name[0] . ".xpi";
  175. copy($_, $xpi_dest)
  176. or croak "Error copying $_ to $xpi_dest : $!";
  177. }
  178. }
  179. 1;
  180. __END__
  181. =head1 SEE ALSO
  182. http://kb.mozillazine.org/About:config_entries
  183. https://developer.mozilla.org/en-US/docs/Mozilla/Preferences/A_brief_guide_to_Mozilla_preferences