Firefox-Profile.t 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #! /usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Selenium::Remote::Driver;
  5. use Test::More;
  6. use MIME::Base64 qw/decode_base64/;
  7. use Archive::Extract;
  8. use File::Temp;
  9. use JSON;
  10. use Selenium::Remote::Driver::Firefox::Profile;
  11. BEGIN {
  12. if (defined $ENV{'WD_MOCKING_RECORD'} && ($ENV{'WD_MOCKING_RECORD'}==1)) {
  13. use t::lib::MockSeleniumWebDriver;
  14. my $p = Net::Ping->new("tcp", 2);
  15. $p->port_number(4444);
  16. unless ($p->ping('localhost')) {
  17. plan skip_all => "Selenium server is not running on localhost:4444";
  18. exit;
  19. }
  20. warn "\n\nRecording...\n\n";
  21. }
  22. }
  23. my $record = (defined $ENV{'WD_MOCKING_RECORD'} && ($ENV{'WD_MOCKING_RECORD'}==1))?1:0;
  24. my $os = $^O;
  25. if ($os =~ m/(aix|freebsd|openbsd|sunos|solaris)/) {
  26. $os = 'linux';
  27. }
  28. my $mock_file = "firefox-profile-mock-$os.json";
  29. if (!$record && !(-e "t/mock-recordings/$mock_file")) {
  30. plan skip_all => "Mocking of tests is not been enabled for this platform";
  31. }
  32. t::lib::MockSeleniumWebDriver::register($record,"t/mock-recordings/$mock_file");
  33. CUSTOM_EXTENSION_LOADED: {
  34. my $profile = Selenium::Remote::Driver::Firefox::Profile->new;
  35. my $website = 'http://localhost:63636';
  36. my $mock_encoded_profile = "t/www/encoded_profile.b64";
  37. my $encoded;
  38. # Set this to true to re-encode the profile. This should not need
  39. # to happen often.
  40. my $create_new_profile = 0;
  41. if ($record && $create_new_profile) {
  42. $profile->set_preference(
  43. 'browser.startup.homepage' => $website
  44. );
  45. # This extension rewrites any page url to a single <h1>. The
  46. # following javascript is in redisplay.xpi's
  47. # resources/gempesaw/lib/main.js:
  48. # var pageMod = require("sdk/page-mod");
  49. # pageMod.PageMod({
  50. # include: "*",
  51. # contentScript: 'document.body.innerHTML = ' +
  52. # ' "<h1>Page matches ruleset</h1>";'
  53. # });
  54. $profile->add_extension('t/www/redisplay.xpi');
  55. $encoded = $profile->_encode;
  56. open (my $fh, ">", $mock_encoded_profile);
  57. print $fh $encoded;
  58. close ($fh);
  59. }
  60. else {
  61. open (my $fh, "<", $mock_encoded_profile);
  62. $encoded = do {local $/ = undef; <$fh>};
  63. close ($fh);
  64. }
  65. my $driver = Selenium::Remote::Driver->new(extra_capabilities => {
  66. firefox_profile => $encoded
  67. });
  68. ok(defined $driver, "made a driver without dying");
  69. # the initial automatic homepage load found in the preference
  70. # 'browser.startup.homepage' isn't blocking, so we need to wait
  71. # until the page is loaded (when we can find elements)
  72. $driver->set_implicit_wait_timeout(30000);
  73. $driver->find_element("h1", "tag_name");
  74. cmp_ok($driver->get_current_url, '=~', qr/localhost/i,
  75. "profile loaded and preference respected!");
  76. $driver->get($website . '/index.html');
  77. cmp_ok($driver->get_text("body", "tag_name"), "=~", qr/ruleset/,
  78. "custom profile with extension loaded");
  79. }
  80. PREFERENCES: {
  81. my $profile = Selenium::Remote::Driver::Firefox::Profile->new();
  82. # We're keeping the expected values together as we accumulate them
  83. # so we can validate them all at the end in the pack_and_unpack
  84. # section
  85. my $expected = {};
  86. STRINGS_AND_INTEGERS: {
  87. my $prefs = {
  88. 'string' => "howdy, there",
  89. 'integer' => 12345,
  90. 'string.like.integer' => '"12345"',
  91. };
  92. foreach (keys %$prefs) {
  93. my $q = $_ eq 'string' ? '"' : '';
  94. $expected->{$_} = $q . $prefs->{$_} . $q;
  95. }
  96. $profile->set_preference(%$prefs);
  97. foreach (keys %$prefs) {
  98. cmp_ok($profile->get_preference($_), "eq", $expected->{$_},
  99. "$_ preference is formatted properly");
  100. }
  101. }
  102. BOOLEANS: {
  103. my $prefs = {
  104. 'boolean.true' => 1,
  105. 'boolean.false' => 0,
  106. };
  107. foreach (keys %$prefs) {
  108. $expected->{$_} = $prefs->{$_} ? 'true' : 'false';
  109. }
  110. $profile->set_boolean_preference(%$prefs);
  111. foreach (keys %$prefs) {
  112. cmp_ok($profile->get_preference($_), "eq", $expected->{$_},
  113. "$_ pref is formatted correctly");
  114. }
  115. }
  116. PACK_AND_UNPACK: {
  117. my $encoded = $profile->_encode();
  118. my $fh = File::Temp->new();
  119. print $fh decode_base64($encoded);
  120. close $fh;
  121. my $zip = Archive::Extract->new(
  122. archive => $fh->filename,
  123. type => "zip"
  124. );
  125. my $tempdir = File::Temp->newdir();
  126. my $ok = $zip->extract( to => $tempdir );
  127. my $outdir = $zip->extract_path;
  128. my $filename = $tempdir . "/user.js";
  129. open ($fh, "<", $filename);
  130. my (@file) = <$fh>;
  131. close ($fh);
  132. my $userjs = join('', @file);
  133. foreach (keys %$expected) {
  134. my $value = $expected->{$_};
  135. cmp_ok($userjs, "=~", qr/$value\);/,
  136. "$_ preference is formatted properly after packing and unpacking");
  137. }
  138. }
  139. }
  140. CROAKING: {
  141. my $profile = Selenium::Remote::Driver::Firefox::Profile->new;
  142. {
  143. eval {
  144. $profile->add_extension('00-load.t');
  145. };
  146. ok($@ =~ /xpi format/i, "caught invalid extension filetype");
  147. }
  148. {
  149. eval {
  150. $profile->add_extension('t/www/invalid-extension.xpi');
  151. my $test = $profile->_encode;
  152. };
  153. ok($@ =~ /install\.rdf/i, "caught invalid extension structure");
  154. }
  155. {
  156. eval {
  157. my $croakingDriver = Selenium::Remote::Driver->new(
  158. firefox_profile => 'clearly invalid!'
  159. );
  160. };
  161. ok ($@ =~ /coercion.*failed/, "caught invalid extension in driver constructor");
  162. }
  163. }
  164. done_testing;