1
0

Firefox.pm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package Selenium::Firefox;
  2. # ABSTRACT: Use FirefoxDriver without a Selenium server
  3. use Moo;
  4. use Selenium::CanStartBinary::FindBinary qw/coerce_simple_binary/;
  5. extends 'Selenium::Remote::Driver';
  6. =head1 SYNOPSIS
  7. my $driver = Selenium::Firefox->new;
  8. my $driver = Selenium::Firefox->new( marionette_enabled => 1 );
  9. =head1 DESCRIPTION
  10. This class allows you to use the FirefoxDriver without needing the JRE
  11. or a selenium server running. When you refrain from passing the
  12. C<remote_server_addr> and C<port> arguments, we will search for the
  13. Firefox executable in your $PATH. We'll try to start the binary
  14. connect to it, shutting it down at the end of the test.
  15. If the Firefox application is not found in the expected places, we'll
  16. fall back to the default L<Selenium::Remote::Driver> behavior of
  17. assuming defaults of 127.0.0.1:4444 after waiting a few seconds.
  18. If you specify a remote server address, or a port, we'll assume you
  19. know what you're doing and take no additional behavior.
  20. If you're curious whether your Selenium::Firefox instance is using a
  21. separate Firefox binary, or through the selenium server, you can check
  22. the C<binary_mode> attr after instantiation.
  23. =cut
  24. has '+browser_name' => (
  25. is => 'ro',
  26. default => sub { 'firefox' }
  27. );
  28. =attr binary
  29. Optional: specify the path to your binary. If you don't specify
  30. anything, we'll try to find it on our own in the default installation
  31. paths for Firefox. If your Firefox is elsewhere, we probably won't be
  32. able to find it, so you may be well served by specifying it yourself.
  33. =cut
  34. has 'binary' => (
  35. is => 'lazy',
  36. coerce => \&coerce_simple_binary,
  37. default => sub { 'geckodriver' },
  38. predicate => 1
  39. );
  40. =attr binary_port
  41. Optional: specify the port that we should bind to. If you don't
  42. specify anything, we'll default to the driver's default port. Since
  43. there's no a priori guarantee that this will be an open port, this is
  44. _not_ necessarily the port that we end up using - if the port here is
  45. already bound, we'll search above it until we find an open one.
  46. See L<Selenium::CanStartBinary/port> for more details, and
  47. L<Selenium::Remote::Driver/port> after instantiation to see what the
  48. actual port turned out to be.
  49. =cut
  50. has 'binary_port' => (
  51. is => 'lazy',
  52. default => sub { 9090 }
  53. );
  54. has '_binary_args' => (
  55. is => 'lazy',
  56. builder => sub {
  57. my ($self) = @_;
  58. my $args = ' --log trace --port ' . $self->port;
  59. if( $self->marionette_enabled ) {
  60. $args .= ' --marionette-port ' . $self->marionette_binary_port;
  61. }
  62. return $args;
  63. }
  64. );
  65. has '+wd_context_prefix' => (
  66. is => 'ro',
  67. default => sub { '' }
  68. );
  69. =attr marionette_binary_port
  70. Optional: specify the port that we should bind marionette to. If you don't
  71. specify anything, we'll default to the marionette's default port. Since
  72. there's no a priori guarantee that this will be an open port, this is
  73. _not_ necessarily the port that we end up using - if the port here is
  74. already bound, we'll search above it until we find an open one.
  75. See L<Selenium::CanStartBinary/port> for more details, and
  76. L<Selenium::Remote::Driver/port> after instantiation to see what the
  77. actual port turned out to be.
  78. Selenium::Firefox->new(
  79. marionette_enabled => 1,
  80. marionette_binary_port => 12345,
  81. );
  82. =cut
  83. has 'marionette_binary_port' => (
  84. is => 'lazy',
  85. default => sub { 2828 }
  86. );
  87. =attr marionette_enabled
  88. Optional: specify whether L<marionette|https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette>
  89. should be enabled or not. If you enable the marionette_enabled flag,
  90. Firefox is launched with marionette server listening to
  91. C<marionette_binary_port>.
  92. The firefox binary must have been built with this funtionality and it's
  93. available in L<all recent Firefox binaries|https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/Builds>.
  94. Note: L<Selenium::Remote::Driver> does not yet provide a marionette
  95. client. It's up to the user to use a client or a marionette-to-webdriver
  96. proxy to communicate with the marionette server.
  97. Selenium::Firefox->new( marionette_enabled => 1 );
  98. and Firefox will have 2 ports open. One for webdriver and one
  99. for marionette:
  100. netstat -tlp | grep firefox
  101. tcp 0 0 localhost:9090 *:* LISTEN 23456/firefox
  102. tcp 0 0 localhost:2828 *:* LISTEN 23456/firefox
  103. =cut
  104. has 'marionette_enabled' => (
  105. is => 'lazy',
  106. default => 1
  107. );
  108. with 'Selenium::CanStartBinary';
  109. =attr custom_args
  110. Optional: specify any additional command line arguments you'd like
  111. invoked during the binary startup. See
  112. L<Selenium::CanStartBinary/custom_args> for more information.
  113. =attr startup_timeout
  114. Optional: specify how long to wait for the binary to start itself and
  115. listen on its port. The default duration is arbitrarily 10 seconds. It
  116. accepts an integer number of seconds to wait: the following will wait
  117. up to 20 seconds:
  118. Selenium::Firefox->new( startup_timeout => 20 );
  119. See L<Selenium::CanStartBinary/startup_timeout> for more information.
  120. =cut
  121. 1;