Firefox-Profile.t 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. BEGIN {
  11. unless (use_ok('Selenium::Remote::Driver::Firefox::Profile')) {
  12. BAIL_OUT ("Couldn't load Firefox Profile");
  13. exit;
  14. }
  15. if (defined $ENV{'WD_MOCKING_RECORD'} && ($ENV{'WD_MOCKING_RECORD'}==1))
  16. {
  17. use t::lib::MockSeleniumWebDriver;
  18. my $p = Net::Ping->new("tcp", 2);
  19. $p->port_number(4444);
  20. unless ($p->ping('localhost')) {
  21. plan skip_all => "Selenium server is not running on localhost:4444";
  22. exit;
  23. }
  24. warn "\n\nRecording...\n\n";
  25. }
  26. }
  27. my $record = (defined $ENV{'WD_MOCKING_RECORD'} && ($ENV{'WD_MOCKING_RECORD'}==1))?1:0;
  28. my $os = $^O;
  29. if ($os =~ m/(aix|freebsd|openbsd|sunos|solaris)/)
  30. {
  31. $os = 'linux';
  32. }
  33. my $mock_file = "firefox-profile-mock-$os.json";
  34. if (!$record && !(-e "t/mock-recordings/$mock_file"))
  35. {
  36. plan skip_all => "Mocking of tests is not been enabled for this platform";
  37. }
  38. t::lib::MockSeleniumWebDriver::register($record,"t/mock-recordings/$mock_file");
  39. # Start our local http server
  40. if ($^O eq 'MSWin32' && $record)
  41. {
  42. system("start \"TEMP_HTTP_SERVER\" /MIN perl t/http-server.pl");
  43. } elsif ($record)
  44. {
  45. system("perl t/http-server.pl > /dev/null &");
  46. }
  47. CUSTOM_EXTENSION_LOADED: {
  48. my $profile = Selenium::Remote::Driver::Firefox::Profile->new();
  49. $profile->set_preferences(
  50. "browser.startup.homepage" => "http://www.google.com"
  51. );
  52. # This extension rewrites any page url that matches *.com to a
  53. # single <h1>. The following javascript is in redisplay.xpi's
  54. # resources/gempesaw/lib/main.js:
  55. # var pageMod = require("sdk/page-mod");
  56. # pageMod.PageMod({
  57. # include: "*.com",
  58. # contentScript: 'document.body.innerHTML = ' +
  59. # ' "<h1>Page matches ruleset</h1>";'
  60. # });
  61. $profile->add_extension('t/www/redisplay.xpi');
  62. my $driver = Selenium::Remote::Driver->new(
  63. extra_capabilities => {
  64. firefox_profile => $profile
  65. }
  66. );
  67. ok(defined $driver, "made a driver without dying");
  68. # the initial automatic homepage load isn't blocking, so we need
  69. # to wait until the page is loaded (when we can find elements)
  70. $driver->set_implicit_wait_timeout(30000);
  71. $driver->find_element("h1", "tag_name");
  72. cmp_ok($driver->get_title, '=~', qr/google/i,
  73. "profile loaded and preference respected!");
  74. $driver->get("http://www.google.com");
  75. cmp_ok($driver->get_text("body", "tag_name"), "=~", qr/ruleset/,
  76. "custom profile with extension loaded");
  77. }
  78. PREFERENCES_FORMATTING: {
  79. my $profile = Selenium::Remote::Driver::Firefox::Profile->new();
  80. my $prefs = {
  81. 'string' => "howdy, there",
  82. 'integer' => 12345,
  83. 'true' => JSON::true,
  84. 'false' => JSON::false,
  85. 'string.like.integer' => '"12345"',
  86. };
  87. my %expected = map {
  88. my $q = $_ eq 'string' ? '"' : '';
  89. $_ => $q . $prefs->{$_} . $q
  90. } keys %$prefs;
  91. $profile->set_preference(%$prefs);
  92. foreach (keys %$prefs) {
  93. cmp_ok($profile->get_preference($_), "eq", $expected{$_},
  94. "$_ preference is formatted properly");
  95. }
  96. my $encoded = $profile->_encode();
  97. my $fh = File::Temp->new();
  98. print $fh decode_base64($encoded);
  99. close $fh;
  100. my $zip = Archive::Extract->new(
  101. archive => $fh->filename,
  102. type => "zip"
  103. );
  104. my $tempdir = File::Temp->newdir();
  105. my $ok = $zip->extract( to => $tempdir );
  106. my $outdir = $zip->extract_path;
  107. my $filename = $tempdir . "/user.js";
  108. open ($fh, "<", $filename);
  109. my (@file) = <$fh>;
  110. close ($fh);
  111. my $userjs = join('', @file);
  112. foreach (keys %expected) {
  113. cmp_ok($userjs, "=~", qr/$expected{$_}\);/,
  114. "$_ preference is formatted properly after packing and unpacking");
  115. }
  116. }
  117. CROAKING: {
  118. my $profile = Selenium::Remote::Driver::Firefox::Profile->new();
  119. {
  120. eval {
  121. $profile->add_extension('00-load.t');
  122. };
  123. ok($@ =~ /xpi format/i, "caught invalid extension filetype");
  124. }
  125. {
  126. eval {
  127. $profile->add_extension('t/www/invalid-extension.xpi');
  128. my $test = $profile->_encode;
  129. };
  130. ok($@ =~ /install\.rdf/i, "caught invalid extension structure");
  131. }
  132. {
  133. eval {
  134. my $croakingDriver = Selenium::Remote::Driver->new(
  135. extra_capabilities => {
  136. firefox_profile => 'clearly invalid!'
  137. }
  138. );
  139. };
  140. ok ($@ =~ /coercion.*failed/, "caught invalid extension in driver constructor");
  141. }
  142. }
  143. # Kill our HTTP Server
  144. if ($^O eq 'MSWin32' && $record)
  145. {
  146. system("taskkill /FI \"WINDOWTITLE eq TEMP_HTTP_SERVER\"");
  147. }
  148. elsif ($record)
  149. {
  150. `ps aux | grep http-server\.pl | grep perl | awk '{print \$2}' | xargs kill`;
  151. }
  152. done_testing;