1
0

Firefox.pm 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package Selenium::Firefox;
  2. # ABSTRACT: A convenience package for creating a Firefox instance
  3. use Selenium::Binary qw/start_binary_on_port/;
  4. use Try::Tiny;
  5. use Moo;
  6. use namespace::clean;
  7. extends 'Selenium::Remote::Driver';
  8. =head1 SYNOPSIS
  9. my $driver = Selenium::Firefox->new;
  10. =cut
  11. use constant FIREFOX_PORT => 9090;
  12. has '+browser_name' => (
  13. is => 'ro',
  14. default => sub { 'firefox' }
  15. );
  16. # By shadowing the parent's port, we can set it in _build_binary_mode properly
  17. has '+port' => (
  18. is => 'lazy',
  19. default => sub { 4444 }
  20. );
  21. has '+firefox_profile' => (
  22. is => 'ro',
  23. lazy => 1,
  24. coerce => sub {
  25. my ($profile) = @_;
  26. die unless $profile->isa('Selenium::Firefox::Profile');
  27. my $port = _find_open_port_above(FIREFOX_PORT);
  28. $profile->add_webdriver($port);
  29. return $profile;
  30. },
  31. default => sub { Selenium::Firefox::Profile->new }
  32. );
  33. has 'binary_mode' => (
  34. is => 'ro',
  35. init_arg => undef,
  36. builder => 1
  37. );
  38. sub _build_binary_mode {
  39. my ($self) = @_;
  40. if (! $self->has_remote_server_addr && ! $self->has_port) {
  41. try {
  42. my $port = start_binary_on_port('firefox', FIREFOX_PORT);
  43. $self->port($port);
  44. return 1;
  45. }
  46. catch {
  47. warn $_;
  48. return 0;
  49. }
  50. }
  51. else {
  52. return 0;
  53. }
  54. }
  55. 1;