1
0

Firefox.pm 11 KB

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