Firefox.pm 11 KB

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