1
0

Firefox-Profile.t 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_FORMATTING: {
  65. my $profile = Selenium::Remote::Driver::Firefox::Profile->new();
  66. my $prefs = {
  67. 'string' => "howdy, there",
  68. 'integer' => 12345,
  69. 'true' => JSON::true,
  70. 'false' => JSON::false,
  71. 'string.like.integer' => '"12345"',
  72. };
  73. my %expected = map {
  74. my $q = $_ eq 'string' ? '"' : '';
  75. $_ => $q . $prefs->{$_} . $q
  76. } keys %$prefs;
  77. $profile->set_preference(%$prefs);
  78. foreach (keys %$prefs) {
  79. cmp_ok($profile->get_preference($_), "eq", $expected{$_},
  80. "$_ preference is formatted properly");
  81. }
  82. my $encoded = $profile->_encode();
  83. my $fh = File::Temp->new();
  84. print $fh decode_base64($encoded);
  85. close $fh;
  86. my $zip = Archive::Extract->new(
  87. archive => $fh->filename,
  88. type => "zip"
  89. );
  90. my $tempdir = File::Temp->newdir();
  91. my $ok = $zip->extract( to => $tempdir );
  92. my $outdir = $zip->extract_path;
  93. my $filename = $tempdir . "/user.js";
  94. open ($fh, "<", $filename);
  95. my (@file) = <$fh>;
  96. close ($fh);
  97. my $userjs = join('', @file);
  98. foreach (keys %expected) {
  99. cmp_ok($userjs, "=~", qr/$expected{$_}\);/,
  100. "$_ preference is formatted properly after packing and unpacking");
  101. }
  102. }
  103. CROAKING: {
  104. my $profile = Selenium::Remote::Driver::Firefox::Profile->new;
  105. {
  106. eval {
  107. $profile->add_extension('00-load.t');
  108. };
  109. ok($@ =~ /xpi format/i, "caught invalid extension filetype");
  110. }
  111. {
  112. eval {
  113. $profile->add_extension('t/www/invalid-extension.xpi');
  114. my $test = $profile->_encode;
  115. };
  116. ok($@ =~ /install\.rdf/i, "caught invalid extension structure");
  117. }
  118. {
  119. eval {
  120. my $croakingDriver = Selenium::Remote::Driver->new(
  121. firefox_profile => 'clearly invalid!'
  122. );
  123. };
  124. ok ($@ =~ /coercion.*failed/, "caught invalid extension in driver constructor");
  125. }
  126. }
  127. done_testing;