1
0

Firefox-Profile.t 4.8 KB

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