Firefox.pm 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package Selenium::Firefox;
  2. # ABSTRACT: Use FirefoxDriver without a Selenium server
  3. use Moo;
  4. use Selenium::Firefox::Binary qw/firefox_path/;
  5. use Selenium::CanStartBinary::FindBinary qw/coerce_simple_binary coerce_firefox_binary/;
  6. extends 'Selenium::Remote::Driver';
  7. =head1 SYNOPSIS
  8. # These two are the same, and will only work with Firefox 48+
  9. my $driver = Selenium::Firefox->new;
  10. my $driver = Selenium::Firefox->new( marionette_enabled => 1 );
  11. ...
  12. $driver->shutdown_binary;
  13. # For Firefox 47 and older, disable marionette:
  14. my $driver = Selenium::Firefox->new( marionette_enabled => 0 );
  15. $driver->shutdown_binary;
  16. =head1 DESCRIPTION
  17. B<Breaking Changes:> There are breaking changes in v1.0+ of this
  18. module if you're using it to start FF47; please see L</"BREAKING
  19. CHANGES">. You can ignore this if you're using v1.0+ of this module to
  20. start FF48.
  21. This class allows you to use the FirefoxDriver without needing the JRE
  22. or a selenium server running. Unlike starting up an instance of
  23. S::R::D, do not pass the C<remote_server_addr> and C<port> arguments,
  24. and we will search for the Firefox executable in your C<$PATH>. We'll
  25. try to start the binary, connect to it, and shut it down at the end of
  26. the test.
  27. If the Firefox application is not found in the expected places, we'll
  28. fall back to the default L<Selenium::Remote::Driver> behavior of
  29. assuming defaults of 127.0.0.1:4444 after waiting a few seconds.
  30. If you specify a remote server address, or a port, our assumption is
  31. that you are doing standard S::R::D behavior and we will not attempt
  32. any binary startup.
  33. If you're curious whether your Selenium::Firefox instance is using a
  34. separate Firefox binary, or through the selenium server, you can check
  35. the value of the C<binary_mode> attr after instantiation.
  36. =head1 BREAKING CHANGES
  37. In version v1.0+ and newer, the default behavior is to enable
  38. marionette & geckodriver mode. This means that an existing script that
  39. works with v0.2701 and Firefox v47 will require modification if you
  40. upgrade Selenium::Firefox to v1.0+. That is,
  41. # v0.2701 of Selenium::Firefox works with FF47 like such; this will not
  42. # work for FF47 after upgrade:
  43. my $fx47_old = Selenium::Firefox->new;
  44. ...
  45. $fx47_old->shutdown_binary;
  46. # v1.0 of Selenium::Firefox works with FF47 like this
  47. my $fx47_new = Selenium::Firefox->new( marionette_enabled => 0);
  48. ...
  49. $fx47_new->shutdown_binary;
  50. We default to assuming FF48 and geckodriver mode because all
  51. forthcoming versions of the Firefox browser will be using the
  52. geckodriver architecture, and also because that's consistent with the
  53. rest of the driver setups, which all have separate driver binaries
  54. apart from the browser itself. This means that:
  55. # v0.2701 of Selenium::Firefox cannot start up FF48 at all
  56. # v1.0+ of Selenium::Firefox works with FF48+ like this:
  57. my $fx48 = Selenium::Firefox->new;
  58. As with the other drivers, Selenium::Firefox in marionette/geckodriver
  59. mode requires a C<geckodriver> executable in the path or provided
  60. during startup, and it will also attempt to find the path to your
  61. Firefox browser. During testing, we found that it was necessary for us
  62. to pass the Firefox browser file path to the C<geckodriver> executable
  63. during start up, or else C<geckodriver> would have trouble finding
  64. Firefox.
  65. =cut
  66. has '+browser_name' => (
  67. is => 'ro',
  68. default => sub { 'firefox' }
  69. );
  70. =attr binary
  71. Optional: specify the path to the C<geckodriver> binary - this is NOT
  72. the path to the Firefox browser. To specify the path to your Firefox
  73. browser binary, see the L</firefox_binary> attr.
  74. For Firefox 48 and greater, this is the path to your C<geckodriver>
  75. executable. If you don't specify anything, we'll search for
  76. C<geckodriver> in your C<$PATH>.
  77. For Firefox 47 and older, this attribute does not apply, because the
  78. older FF browsers do not use the separate driver binary startup.
  79. =cut
  80. has 'binary' => (
  81. is => 'lazy',
  82. coerce => \&coerce_simple_binary,
  83. default => sub { 'geckodriver' },
  84. predicate => 1
  85. );
  86. =attr binary_port
  87. Optional: specify the port that we should bind to. If you don't
  88. specify anything, we'll default to the driver's default port. Since
  89. there's no a priori guarantee that this will be an open port, this is
  90. _not_ necessarily the port that we end up using - if the port here is
  91. already bound, we'll search above it until we find an open one.
  92. See L<Selenium::CanStartBinary/port> for more details, and
  93. L<Selenium::Remote::Driver/port> after instantiation to see what the
  94. actual port turned out to be.
  95. =cut
  96. has 'binary_port' => (
  97. is => 'lazy',
  98. default => sub { 9090 }
  99. );
  100. =attr firefox_profile
  101. Optional: Pass in an instance of L<Selenium::Firefox::Profile>
  102. pre-configured as you please. The preferences you specify will be
  103. merged with the ones necessary for setting up webdriver, and as a
  104. result some options may be overwritten or ignored.
  105. my $profile = Selenium::Firefox::Profile->new;
  106. my $firefox = Selenium::Firefox->new(
  107. firefox_profile => $profile
  108. );
  109. =cut
  110. has '_binary_args' => (
  111. is => 'lazy',
  112. builder => sub {
  113. my ($self) = @_;
  114. if ( $self->marionette_enabled ) {
  115. my $args = ' --port ' . $self->port
  116. . ' --marionette-port ' . $self->marionette_binary_port
  117. . ' --binary "' . $self->firefox_binary . '"';
  118. return $args;
  119. }
  120. else {
  121. return ' -no-remote';
  122. }
  123. }
  124. );
  125. has '+wd_context_prefix' => (
  126. is => 'ro',
  127. default => sub {
  128. my ($self) = @_;
  129. if ($self->marionette_enabled) {
  130. return '';
  131. }
  132. else {
  133. return '/hub';
  134. }
  135. }
  136. );
  137. =attr marionette_binary_port
  138. Optional: specify the port that we should bind marionette to. If you don't
  139. specify anything, we'll default to the marionette's default port. Since
  140. there's no a priori guarantee that this will be an open port, this is
  141. _not_ necessarily the port that we end up using - if the port here is
  142. already bound, we'll search above it until we find an open one.
  143. Selenium::Firefox->new(
  144. marionette_enabled => 1,
  145. marionette_binary_port => 12345,
  146. );
  147. Attempting to specify a C<marionette_binary_port> in conjunction with
  148. setting C<marionette_enabled> does not make sense and will most likely
  149. not do anything useful.
  150. =cut
  151. has 'marionette_binary_port' => (
  152. is => 'lazy',
  153. default => sub { 2828 }
  154. );
  155. =attr marionette_enabled
  156. Optional: specify whether
  157. L<marionette|https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette>
  158. should be enabled or not. By default, marionette is enabled, which
  159. assumes you are running with Firefox 48 or newer. To use this module
  160. to start Firefox 47 or older, you must pass C<<<marionette_enabled =>
  161. 0>>>.
  162. my $ff48 = Selenium::Firefox->new( marionette_enabled => 1 ); # defaults to 1
  163. my $ff47 = Selenium::Firefox->new( marionette_enabled => 0 );
  164. =cut
  165. has 'marionette_enabled' => (
  166. is => 'lazy',
  167. default => 1
  168. );
  169. =attr firefox_binary
  170. Optional: specify the path to the Firefox browser executable. Although
  171. we will attempt to locate this in your C<$PATH>, you may specify it
  172. explicitly here. Note that path here must point to a file that exists
  173. and is executable, or we will croak.
  174. For Firefox 48 and newer, this will be passed to C<geckodriver> such
  175. that it will attempt to start up the Firefox at the specified path. If
  176. you do not specify anything, we will look for the Firefox browser on
  177. our own in the normal places, but if the browser cannot be found,
  178. we'll probably C<die> during instantiation.
  179. For Firefox 47 and older, this browser path should be the file that we
  180. directly start up.
  181. =cut
  182. has 'firefox_binary' => (
  183. is => 'lazy',
  184. coerce => \&coerce_firefox_binary,
  185. predicate => 1,
  186. builder => 'firefox_path'
  187. );
  188. with 'Selenium::CanStartBinary';
  189. =attr custom_args
  190. Optional: specify any additional command line arguments you'd like
  191. invoked during the binary startup. See
  192. L<Selenium::CanStartBinary/custom_args> for more information.
  193. For Firefox 48 and newer, these arguments will be passed to
  194. geckodriver during start up.
  195. For Firefox 47 and older, these arguments will be passed to the
  196. Firefox browser during start up.
  197. =attr startup_timeout
  198. Optional: specify how long to wait for the binary to start itself and
  199. listen on its port. The default duration is arbitrarily 10 seconds. It
  200. accepts an integer number of seconds to wait: the following will wait
  201. up to 20 seconds:
  202. Selenium::Firefox->new( startup_timeout => 20 );
  203. See L<Selenium::CanStartBinary/startup_timeout> for more information.
  204. =method shutdown_binary
  205. Call this method instead of L<Selenium::Remote::Driver/quit> to ensure
  206. that the binary executable is also closed, instead of simply closing
  207. the browser itself. If the browser is still around, it will call
  208. C<quit> for you. After that, it will try to shutdown the browser
  209. binary by making a GET to /shutdown and on Windows, it will attempt to
  210. do a C<taskkill> on the binary CMD window.
  211. $self->shutdown_binary;
  212. It doesn't take any arguments, and it doesn't return anything.
  213. We do our best to call this when the C<$driver> option goes out of
  214. scope, but if that happens during global destruction, there's nothing
  215. we can do.
  216. =cut
  217. 1;