1
0

Profile.pm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package Selenium::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. either use JSON::true/JSON::false, or 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 ( blessed($value) ) {
  67. $self->set_boolean_preference($_, $value );
  68. next;
  69. }
  70. elsif ($value =~ /^(['"]).*\1$/ or looks_like_number($value)) {
  71. # plain integers: 0, 1, 32768, or integers wrapped in strings:
  72. # "0", "1", "20140204". in either case, there's nothing for us
  73. # to do.
  74. $clean_value = $value;
  75. }
  76. else {
  77. # otherwise it's hopefully a string that we'll need to
  78. # quote on our own
  79. $clean_value = '"' . $value . '"';
  80. }
  81. $self->{user_prefs}->{$_} = $clean_value;
  82. }
  83. }
  84. =method set_boolean_preference
  85. Set preferences that require boolean values of 'true' or 'false'. You
  86. can set multiple preferences at once. For string or integer
  87. preferences, use C<set_preference()>.
  88. $profile->set_boolean_preference("false.pref" => 0);
  89. # user_pref("false.pref", false);
  90. $profile->set_boolean_preference("true.pref" => 1);
  91. # user_pref("true.pref", true);
  92. =cut
  93. sub set_boolean_preference {
  94. my ($self, %prefs) = @_;
  95. foreach (keys %prefs) {
  96. my $value = $prefs{$_};
  97. $self->{user_prefs}->{$_} = $value ? 'true' : 'false';
  98. }
  99. }
  100. =method get_preference
  101. Retrieve the computed value of a preference. Strings will be double
  102. quoted and boolean values will be single quoted as "true" or "false"
  103. accordingly.
  104. $profile->set_boolean_preference("true.pref" => 1);
  105. print $profile->get_preference("true.pref") # true
  106. $profile->set_preference("string.pref" => "an extra set of quotes");
  107. print $profile->get_preference("string.pref") # "an extra set of quotes"
  108. =cut
  109. sub get_preference {
  110. my ($self, $pref) = @_;
  111. return $self->{user_prefs}->{$pref};
  112. }
  113. =method add_extension
  114. Add an existing C<.xpi> to the profile by providing its path. This
  115. only works with packaged C<.xpi> files, not plain/un-packed extension
  116. directories.
  117. $profile->add_extension('t/www/redisplay.xpi');
  118. =cut
  119. sub add_extension {
  120. my ($self, $xpi) = @_;
  121. croak 'File not found: ' . $xpi unless -e $xpi;
  122. my $xpi_abs_path = abs_path($xpi);
  123. croak '$xpi_abs_path: extensions must be in .xpi format' unless $xpi_abs_path =~ /\.xpi$/;
  124. push (@{$self->{extensions}}, $xpi_abs_path);
  125. }
  126. sub _encode {
  127. my $self = shift;
  128. # The remote webdriver accepts the Firefox profile as a base64
  129. # encoded zip file
  130. $self->_layout_on_disk();
  131. my $zip = Archive::Zip->new();
  132. my $dir_member = $zip->addTree( $self->{profile_dir} );
  133. my $string = "";
  134. open (my $fh, ">", \$string);
  135. binmode($fh);
  136. unless ( $zip->writeToFileHandle($fh) == AZ_OK ) {
  137. die 'write error';
  138. }
  139. return encode_base64($string);
  140. }
  141. sub _layout_on_disk {
  142. my $self = shift;
  143. $self->_write_preferences();
  144. $self->_install_extensions();
  145. return $self->{profile_dir};
  146. }
  147. sub _write_preferences {
  148. my $self = shift;
  149. my $userjs = $self->{profile_dir} . "/user.js";
  150. open (my $fh, ">>", $userjs)
  151. or die "Cannot open $userjs for writing preferences: $!";
  152. foreach (keys %{$self->{user_prefs}}) {
  153. print $fh 'user_pref("' . $_ . '", ' . $self->get_preference($_) . ');' . "\n";
  154. }
  155. close ($fh);
  156. }
  157. sub _install_extensions {
  158. my $self = shift;
  159. my $extension_dir = $self->{profile_dir} . "/extensions/";
  160. mkdir $extension_dir unless -d $extension_dir;
  161. # TODO: handle extensions that need to be unpacked
  162. foreach (@{$self->{extensions}}) {
  163. # For Firefox to recognize the extension, we have to put the
  164. # .xpi in the /extensions/ folder and change the filename to
  165. # its id, which is found in the install.rdf in the root of the
  166. # zip.
  167. my $ae = Archive::Extract->new( archive => $_,
  168. type => "zip");
  169. my $tempDir = File::Temp->newdir();
  170. $ae->extract( to => $tempDir );
  171. my $install = $ae->extract_path();
  172. $install .= '/install.rdf';
  173. open (my $fh, "<", $install)
  174. or croak "No install.rdf inside $_: $!";
  175. my (@file) = <$fh>;
  176. close ($fh);
  177. my @name = grep { chomp; $_ =~ /<em:id>[^{]/ } @file;
  178. $name[0] =~ s/.*<em:id>(.*)<\/em:id>.*/$1/;
  179. my $xpi_dest = $extension_dir . $name[0] . ".xpi";
  180. copy($_, $xpi_dest)
  181. or croak "Error copying $_ to $xpi_dest : $!";
  182. }
  183. }
  184. 1;
  185. __END__
  186. =head1 SEE ALSO
  187. http://kb.mozillazine.org/About:config_entries
  188. https://developer.mozilla.org/en-US/docs/Mozilla/Preferences/A_brief_guide_to_Mozilla_preferences