1
0

Firefox.pm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package Selenium::Firefox;
  2. # ABSTRACT: A convenience package for creating a Firefox instance
  3. use Selenium::Binary qw/_find_open_port_above _probe_port/;
  4. use Selenium::Firefox::Binary qw/firefox_path/;
  5. use Selenium::Firefox::Profile;
  6. use Selenium::Waiter qw/wait_until/;
  7. use Moo;
  8. use namespace::clean;
  9. extends 'Selenium::Remote::Driver';
  10. =head1 SYNOPSIS
  11. my $driver = Selenium::Firefox->new;
  12. =cut
  13. use constant FIREFOX_PORT => 9090;
  14. has '+browser_name' => (
  15. is => 'ro',
  16. default => sub { 'firefox' }
  17. );
  18. # By shadowing the parent's port, we can set it in _build_binary_mode properly
  19. has '+port' => (
  20. is => 'lazy',
  21. default => sub { 4444 }
  22. );
  23. has '+firefox_profile' => (
  24. is => 'ro',
  25. lazy => 1,
  26. coerce => sub {
  27. my ($profile) = @_;
  28. die unless $profile->isa('Selenium::Firefox::Profile');
  29. my $port = _find_open_port_above(FIREFOX_PORT);
  30. $profile->add_webdriver($port);
  31. return $profile;
  32. },
  33. default => sub { Selenium::Firefox::Profile->new }
  34. );
  35. has 'binary_mode' => (
  36. is => 'ro',
  37. init_arg => undef,
  38. builder => sub {
  39. my ($self) = @_;
  40. my $profile = Selenium::Firefox::Profile->new;
  41. my $port = _find_open_port_above(FIREFOX_PORT);
  42. $profile->add_webdriver($port);
  43. $ENV{'XRE_PROFILE_PATH'} = $profile->_layout_on_disk;
  44. $ENV{'MOZ_NO_REMOTE'} = '1'; # able to launch multiple instances
  45. $ENV{'MOZ_CRASHREPORTER_DISABLE'} = '1'; # disable breakpad
  46. $ENV{'NO_EM_RESTART'} = '1'; # prevent the binary from detaching from the console.log
  47. my $binary = firefox_path();
  48. system( $binary . ' -no-remote > /dev/null 2>&1 & ');
  49. my $success = wait_until { _probe_port($port) } timeout => 10;
  50. if ($success) {
  51. $self->port($port);
  52. return 1
  53. }
  54. else {
  55. return 0;
  56. }
  57. }
  58. );
  59. 1;