Firefox-Profile.t 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. $profile->set_preference(
  37. 'browser.startup.homepage' => $website
  38. );
  39. # This extension rewrites any page url to a single <h1>. The
  40. # following javascript is in redisplay.xpi's
  41. # resources/gempesaw/lib/main.js:
  42. # var pageMod = require("sdk/page-mod");
  43. # pageMod.PageMod({
  44. # include: "*",
  45. # contentScript: 'document.body.innerHTML = ' +
  46. # ' "<h1>Page matches ruleset</h1>";'
  47. # });
  48. $profile->add_extension('t/www/redisplay.xpi');
  49. my $driver = Selenium::Remote::Driver->new(
  50. firefox_profile => $profile
  51. );
  52. ok(defined $driver, "made a driver without dying");
  53. # the initial automatic homepage load found in the preference
  54. # 'browser.startup.homepage' isn't blocking, so we need to wait
  55. # until the page is loaded (when we can find elements)
  56. $driver->set_implicit_wait_timeout(30000);
  57. $driver->find_element("h1", "tag_name");
  58. cmp_ok($driver->get_current_url, '=~', qr/localhost/i,
  59. "profile loaded and preference respected!");
  60. $driver->get($website . '/index.html');
  61. cmp_ok($driver->get_text("body", "tag_name"), "=~", qr/ruleset/,
  62. "custom profile with extension loaded");
  63. }
  64. PREFERENCES: {
  65. my $profile = Selenium::Remote::Driver::Firefox::Profile->new();
  66. # We're keeping the expected values together as we accumulate them
  67. # so we can validate them all at the end in the pack_and_unpack
  68. # section
  69. my $expected = {};
  70. STRINGS_AND_INTEGERS: {
  71. my $prefs = {
  72. 'string' => "howdy, there",
  73. 'integer' => 12345,
  74. 'string.like.integer' => '"12345"',
  75. };
  76. foreach (keys %$prefs) {
  77. my $q = $_ eq 'string' ? '"' : '';
  78. $expected->{$_} = $q . $prefs->{$_} . $q;
  79. }
  80. $profile->set_preference(%$prefs);
  81. foreach (keys %$prefs) {
  82. cmp_ok($profile->get_preference($_), "eq", $expected->{$_},
  83. "$_ preference is formatted properly");
  84. }
  85. }
  86. BOOLEANS: {
  87. my $prefs = {
  88. 'boolean.true' => 1,
  89. 'boolean.false' => 0,
  90. };
  91. foreach (keys %$prefs) {
  92. $expected->{$_} = $prefs->{$_} ? 'true' : 'false';
  93. }
  94. $profile->set_boolean_preference(%$prefs);
  95. foreach (keys %$prefs) {
  96. cmp_ok($profile->get_preference($_), "eq", $expected->{$_},
  97. "$_ pref is formatted correctly");
  98. }
  99. }
  100. PACK_AND_UNPACK: {
  101. my $encoded = $profile->_encode();
  102. my $fh = File::Temp->new();
  103. print $fh decode_base64($encoded);
  104. close $fh;
  105. my $zip = Archive::Extract->new(
  106. archive => $fh->filename,
  107. type => "zip"
  108. );
  109. my $tempdir = File::Temp->newdir();
  110. my $ok = $zip->extract( to => $tempdir );
  111. my $outdir = $zip->extract_path;
  112. my $filename = $tempdir . "/user.js";
  113. open ($fh, "<", $filename);
  114. my (@file) = <$fh>;
  115. close ($fh);
  116. my $userjs = join('', @file);
  117. foreach (keys %$expected) {
  118. my $value = $expected->{$_};
  119. cmp_ok($userjs, "=~", qr/$value\);/,
  120. "$_ preference is formatted properly after packing and unpacking");
  121. }
  122. }
  123. }
  124. CROAKING: {
  125. my $profile = Selenium::Remote::Driver::Firefox::Profile->new;
  126. {
  127. eval {
  128. $profile->add_extension('00-load.t');
  129. };
  130. ok($@ =~ /xpi format/i, "caught invalid extension filetype");
  131. }
  132. {
  133. eval {
  134. $profile->add_extension('t/www/invalid-extension.xpi');
  135. my $test = $profile->_encode;
  136. };
  137. ok($@ =~ /install\.rdf/i, "caught invalid extension structure");
  138. }
  139. {
  140. eval {
  141. my $croakingDriver = Selenium::Remote::Driver->new(
  142. firefox_profile => 'clearly invalid!'
  143. );
  144. };
  145. ok ($@ =~ /coercion.*failed/, "caught invalid extension in driver constructor");
  146. }
  147. }
  148. done_testing;