Firefox.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package Selenium::Firefox;
  2. # ABSTRACT: Use FirefoxDriver without a Selenium server
  3. use Moo;
  4. use Carp;
  5. use Selenium::Firefox::Binary qw/firefox_path/;
  6. use Selenium::CanStartBinary::FindBinary qw/coerce_simple_binary coerce_firefox_binary/;
  7. extends 'Selenium::Remote::Driver';
  8. =head1 SYNOPSIS
  9. # These two are the same, and will only work with Firefox 48+
  10. my $driver = Selenium::Firefox->new;
  11. my $driver = Selenium::Firefox->new( marionette_enabled => 1 );
  12. ...
  13. $driver->shutdown_binary;
  14. # For Firefox 47 and older, disable marionette:
  15. my $driver = Selenium::Firefox->new( marionette_enabled => 0 );
  16. $driver->shutdown_binary;
  17. =head1 DESCRIPTION
  18. B<Breaking Changes:> There are breaking changes in v1.0+ of this
  19. module if you're using it to start FF47; please see L</"BREAKING
  20. CHANGES">. You can ignore this if you're using v1.0+ of this module to
  21. start FF48.
  22. This class allows you to use the FirefoxDriver without needing the JRE
  23. or a selenium server running. Unlike starting up an instance of
  24. S::R::D, do not pass the C<remote_server_addr> and C<port> arguments,
  25. and we will search for the Firefox executable in your C<$PATH>. We'll
  26. try to start the binary, connect to it, and shut it down at the end of
  27. the test.
  28. If the Firefox application is not found in the expected places, we'll
  29. fall back to the default L<Selenium::Remote::Driver> behavior of
  30. assuming defaults of 127.0.0.1:4444 after waiting a few seconds.
  31. If you specify a remote server address, or a port, our assumption is
  32. that you are doing standard S::R::D behavior and we will not attempt
  33. any binary startup.
  34. If you're curious whether your Selenium::Firefox instance is using a
  35. separate Firefox binary, or through the selenium server, you can check
  36. the value of the C<binary_mode> attr after instantiation.
  37. =head1 BREAKING CHANGES
  38. In version v1.0+ and newer, the default behavior is to enable
  39. marionette & geckodriver mode. This means that an existing script that
  40. works with v0.2701 and Firefox v47 will require modification if you
  41. upgrade Selenium::Firefox to v1.0+. That is,
  42. # v0.2701 of Selenium::Firefox works with FF47 like such; this will not
  43. # work for FF47 after upgrade:
  44. my $fx47_old = Selenium::Firefox->new;
  45. ...
  46. $fx47_old->shutdown_binary;
  47. # v1.0 of Selenium::Firefox works with FF47 like this
  48. my $fx47_new = Selenium::Firefox->new( marionette_enabled => 0);
  49. ...
  50. $fx47_new->shutdown_binary;
  51. We default to assuming FF48 and geckodriver mode because all
  52. forthcoming versions of the Firefox browser will be using the
  53. geckodriver architecture, and also because that's consistent with the
  54. rest of the driver setups, which all have separate driver binaries
  55. apart from the browser itself. This means that:
  56. # v0.2701 of Selenium::Firefox cannot start up FF48 at all
  57. # v1.0+ of Selenium::Firefox works with FF48+ like this:
  58. my $fx48 = Selenium::Firefox->new;
  59. As with the other drivers, Selenium::Firefox in marionette/geckodriver
  60. mode requires a C<geckodriver> executable in the path or provided
  61. during startup, and it will also attempt to find the path to your
  62. Firefox browser. During testing, we found that it was necessary for us
  63. to pass the Firefox browser file path to the C<geckodriver> executable
  64. during start up, or else C<geckodriver> would have trouble finding
  65. Firefox.
  66. =cut
  67. has '+browser_name' => (
  68. is => 'ro',
  69. default => sub { 'firefox' }
  70. );
  71. =attr binary
  72. Optional: specify the path to the C<geckodriver> binary - this is NOT
  73. the path to the Firefox browser. To specify the path to your Firefox
  74. browser binary, see the L</firefox_binary> attr.
  75. For Firefox 48 and greater, this is the path to your C<geckodriver>
  76. executable. If you don't specify anything, we'll search for
  77. C<geckodriver> in your C<$PATH>.
  78. For Firefox 47 and older, this attribute does not apply, because the
  79. older FF browsers do not use the separate driver binary startup.
  80. =cut
  81. has 'binary' => (
  82. is => 'lazy',
  83. coerce => \&coerce_simple_binary,
  84. default => sub { 'geckodriver' },
  85. predicate => 1
  86. );
  87. =attr binary_port
  88. Optional: specify the port that we should bind to. If you don't
  89. specify anything, we'll default to the driver's default port. Since
  90. there's no a priori guarantee that this will be an open port, this is
  91. _not_ necessarily the port that we end up using - if the port here is
  92. already bound, we'll search above it until we find an open one.
  93. See L<Selenium::CanStartBinary/port> for more details, and
  94. L<Selenium::Remote::Driver/port> after instantiation to see what the
  95. actual port turned out to be.
  96. =cut
  97. has 'binary_port' => (
  98. is => 'lazy',
  99. default => sub { 9090 }
  100. );
  101. =attr firefox_profile
  102. Optional: Pass in an instance of L<Selenium::Firefox::Profile>
  103. pre-configured as you please. The preferences you specify will be
  104. merged with the ones necessary for setting up webdriver, and as a
  105. result some options may be overwritten or ignored.
  106. my $profile = Selenium::Firefox::Profile->new;
  107. my $firefox = Selenium::Firefox->new(
  108. firefox_profile => $profile
  109. );
  110. =cut
  111. has '_binary_args' => (
  112. is => 'lazy',
  113. builder => sub {
  114. my ($self) = @_;
  115. if ( $self->marionette_enabled ) {
  116. my $args = ' --port ' . $self->port
  117. . ' --marionette-port ' . $self->marionette_binary_port
  118. . ' --binary "' . $self->firefox_binary . '"';
  119. return $args;
  120. }
  121. else {
  122. return ' -no-remote';
  123. }
  124. }
  125. );
  126. has '+wd_context_prefix' => (
  127. is => 'ro',
  128. default => sub {
  129. my ($self) = @_;
  130. if ($self->marionette_enabled) {
  131. return '';
  132. }
  133. else {
  134. return '/hub';
  135. }
  136. }
  137. );
  138. =attr marionette_binary_port
  139. Optional: specify the port that we should bind marionette to. If you don't
  140. specify anything, we'll default to the marionette's default port. Since
  141. there's no a priori guarantee that this will be an open port, this is
  142. _not_ necessarily the port that we end up using - if the port here is
  143. already bound, we'll search above it until we find an open one.
  144. Selenium::Firefox->new(
  145. marionette_enabled => 1,
  146. marionette_binary_port => 12345,
  147. );
  148. Attempting to specify a C<marionette_binary_port> in conjunction with
  149. setting C<marionette_enabled> does not make sense and will most likely
  150. not do anything useful.
  151. =cut
  152. has 'marionette_binary_port' => (
  153. is => 'lazy',
  154. default => sub { 2828 }
  155. );
  156. =attr marionette_enabled
  157. Optional: specify whether
  158. L<marionette|https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette>
  159. should be enabled or not. By default, marionette is enabled, which
  160. assumes you are running with Firefox 48 or newer. To use this module to
  161. start Firefox 47 or older, you must pass C<< marionette_enabled => 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. has '_execute_script_suffix' => (
  189. is => 'lazy',
  190. default => 'Gecko'
  191. );
  192. =head2 get_context
  193. Description:
  194. Firefox extension: Retrieve browser's scope (chrome or content).
  195. Chrome is a privileged scope where you can access things like the
  196. Firefox UI itself. Content scope is where things like webpages live.
  197. Output:
  198. STRING - context {CHROME|CONTENT}
  199. Usage:
  200. print $firefox_driver->get_context();
  201. =cut
  202. sub get_context {
  203. my $self = shift;
  204. if ( $self->_is_old_ff ) {
  205. return 0;
  206. }
  207. my $res = { 'command' => 'getContext' };
  208. return $self->_execute_command($res);
  209. }
  210. =head2 set_context
  211. Description:
  212. Firefox extension: Set browser's scope (chrome or content).
  213. Chrome is a privileged scope where you can access things like the
  214. Firefox UI itself. Content scope is where things like webpages live.
  215. Input:
  216. Required:
  217. <STRING> - context {CHROME|CONTENT}
  218. Usage:
  219. $firefox_driver->set_context( $context );
  220. Output:
  221. BOOLEAN - success or failure
  222. =cut
  223. sub set_context {
  224. my ( $self, $context ) = @_;
  225. if ( $self->_is_old_ff ) {
  226. return 0;
  227. }
  228. if ( not defined $context ) {
  229. croak "Expecting context";
  230. }
  231. if ( $context !~ m/chrome|content/i ) {
  232. croak "Expecting context value: chrome or content";
  233. }
  234. my $res = { 'command' => 'setContext' };
  235. return $self->_execute_command( $res, { context => $context } );
  236. }
  237. with 'Selenium::CanStartBinary';
  238. =attr custom_args
  239. Optional: specify any additional command line arguments you'd like
  240. invoked during the binary startup. See
  241. L<Selenium::CanStartBinary/custom_args> for more information.
  242. For Firefox 48 and newer, these arguments will be passed to
  243. geckodriver during start up.
  244. For Firefox 47 and older, these arguments will be passed to the
  245. Firefox browser during start up.
  246. =attr startup_timeout
  247. Optional: specify how long to wait for the binary to start itself and
  248. listen on its port. The default duration is arbitrarily 10 seconds. It
  249. accepts an integer number of seconds to wait: the following will wait
  250. up to 20 seconds:
  251. Selenium::Firefox->new( startup_timeout => 20 );
  252. See L<Selenium::CanStartBinary/startup_timeout> for more information.
  253. =method shutdown_binary
  254. Call this method instead of L<Selenium::Remote::Driver/quit> to ensure
  255. that the binary executable is also closed, instead of simply closing
  256. the browser itself. If the browser is still around, it will call
  257. C<quit> for you. After that, it will try to shutdown the browser
  258. binary by making a GET to /shutdown and on Windows, it will attempt to
  259. do a C<taskkill> on the binary CMD window.
  260. $self->shutdown_binary;
  261. It doesn't take any arguments, and it doesn't return anything.
  262. We do our best to call this when the C<$driver> option goes out of
  263. scope, but if that happens during global destruction, there's nothing
  264. we can do.
  265. =attr fixed_ports
  266. Optional: Throw instead of searching for additional ports; see
  267. L<Selenium::CanStartBinary/fixed_ports> for more info.
  268. =cut
  269. 1;