CanStartBinary.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. package Selenium::CanStartBinary;
  2. use strict;
  3. use warnings;
  4. # ABSTRACT: Teach a WebDriver how to start its own binary aka no JRE!
  5. use File::Spec;
  6. use Selenium::CanStartBinary::ProbePort
  7. qw/find_open_port_above find_open_port probe_port/;
  8. use Selenium::Firefox::Binary qw/setup_firefox_binary_env/;
  9. use Selenium::Waiter qw/wait_until/;
  10. use Moo::Role;
  11. use constant IS_WIN => $^O eq 'MSWin32';
  12. =for Pod::Coverage *EVERYTHING*
  13. =head1 DESCRIPTION
  14. This role takes care of the details for starting up a Webdriver
  15. instance. It does not do any downloading or installation of any sort -
  16. you're still responsible for obtaining and installing the necessary
  17. binaries into your C<$PATH> for this role to find. You may be
  18. interested in L<Selenium::Chrome>, L<Selenium::Firefox>, or
  19. L<Selenium::PhantomJS> if you're looking for classes that already
  20. consume this role.
  21. The role determines whether or not it should try to do its own magic
  22. based on whether the consuming class is instantiated with a
  23. C<remote_server_addr> and/or C<port>.
  24. # We'll start up the Chrome binary for you
  25. my $chrome_via_binary = Selenium::Chrome->new;
  26. # Look for a selenium server running on 4444.
  27. my $chrome_via_server = Selenium::Chrome->new( port => 4444 );
  28. If they're missing, we assume the user wants to use a webdriver
  29. directly and act accordingly. We handle finding the proper associated
  30. binary (or you can specify it with L</binary>), figuring out what
  31. arguments it wants, setting up any necessary environments, and
  32. starting up the binary.
  33. There's a number of TODOs left over - namely Windows support is
  34. severely lacking, and we're pretty naive when we attempt to locate the
  35. executables on our own.
  36. In the following documentation, C<required> refers to when you're
  37. consuming the role, not the C<required> when you're instantiating a
  38. class that has already consumed the role.
  39. =attr binary
  40. Required: Specify the path to the executable in question, or the name
  41. of the executable for us to find via L<File::Which/which>.
  42. =cut
  43. requires 'binary';
  44. =attr binary_port
  45. Required: Specify a default port that for the webdriver binary to try
  46. to bind to. If that port is unavailable, we'll probe above that port
  47. until we find a valid one.
  48. =cut
  49. requires 'binary_port';
  50. =attr _binary_args
  51. Required: Specify the arguments that the particular binary needs in
  52. order to start up correctly. In particular, you may need to tell the
  53. binary about the proper port when we start it up, or that it should
  54. use a particular prefix to match up with the behavior of the Remote
  55. Driver server.
  56. If your binary doesn't need any arguments, just have the default be an
  57. empty string.
  58. =cut
  59. requires '_binary_args';
  60. =attr port
  61. The role will attempt to determine the proper port for us. Consuming
  62. roles should set a default port in L</binary_port> at which we will
  63. begin searching for an open port.
  64. Note that if we cannot locate a suitable L</binary>, port will be set
  65. to 4444 so we can attempt to look for a Selenium server at
  66. C<127.0.0.1:4444>.
  67. =cut
  68. has '_real_binary' => (
  69. is => 'lazy',
  70. builder => sub {
  71. my ($self) = @_;
  72. if ( $self->_is_old_ff ) {
  73. return $self->firefox_binary;
  74. }
  75. else {
  76. return $self->binary;
  77. }
  78. }
  79. );
  80. has '_is_old_ff' => (
  81. is => 'lazy',
  82. builder => sub {
  83. my ($self) = @_;
  84. return $self->isa('Selenium::Firefox') && !$self->marionette_enabled;
  85. }
  86. );
  87. has '+port' => (
  88. is => 'lazy',
  89. builder => sub {
  90. my ($self) = @_;
  91. if ( $self->_real_binary ) {
  92. if ( $self->fixed_ports ) {
  93. return find_open_port( $self->binary_port );
  94. }
  95. else {
  96. return find_open_port_above( $self->binary_port );
  97. }
  98. }
  99. else {
  100. return 4444;
  101. }
  102. }
  103. );
  104. =attr fixed_ports
  105. Optional: By default, if binary_port and marionette_port are not free
  106. a higher free port is probed and acquired if possible, until a free one
  107. if found or a timeout is exceeded.
  108. my $driver1 = Selenium::Chrome->new;
  109. my $driver2 = Selenium::Chrome->new( port => 1234 );
  110. The default behavior can be overridden. In this case, only the default
  111. or given binary_port and marionette_port are probed, without probing
  112. higher ports. This ensures that either the default or given port will be
  113. assigned, or no port will be assigned at all.
  114. my $driver1 = Selenium::Chrome->new( fixed_ports => 1 );
  115. my $driver2 = Selenium::Chrome->new( port => 1234, fixed_ports => 1);
  116. =cut
  117. has 'fixed_ports' => (
  118. is => 'lazy',
  119. default => sub { 0 }
  120. );
  121. =attr custom_args
  122. Optional: If you want to pass additional options to the binary when it
  123. starts up, you can add that here. For example, if your binary accepts
  124. an argument on the command line like C<--log-path=/path/to/log>, and
  125. you'd like to specify that the binary uses that option, you could do:
  126. my $chrome = Selenium::Chrome->new(
  127. custom_args => '--log-path=/path/to/log'
  128. );
  129. To specify multiple arguments, just include them all in the string.
  130. =cut
  131. has custom_args => (
  132. is => 'lazy',
  133. predicate => 1,
  134. default => sub { '' }
  135. );
  136. has 'marionette_port' => (
  137. is => 'lazy',
  138. builder => sub {
  139. my ($self) = @_;
  140. if ( $self->_is_old_ff ) {
  141. return 0;
  142. }
  143. else {
  144. if ( $self->fixed_ports ) {
  145. return find_open_port( $self->marionette_binary_port );
  146. }
  147. else {
  148. return find_open_port_above( $self->marionette_binary_port );
  149. }
  150. }
  151. }
  152. );
  153. =attr startup_timeout
  154. Optional: you can modify how long we will wait for the binary to start
  155. up. By default, we will start the binary and check the intended
  156. destination port for 10 seconds before giving up. If the machine
  157. you're using to run your browsers is slower or smaller, you may need
  158. to increase this timeout.
  159. The following:
  160. my $f = Selenium::Firefox->new(
  161. startup_timeout => 60
  162. );
  163. will wait up to 60 seconds for the firefox binary to respond on the
  164. proper port. To use this constructor option, you should specify a time
  165. in seconds as an integer, and it will be passed to the arguments
  166. section of a L<Selenium::Waiter/wait_until> subroutine call.
  167. =cut
  168. has startup_timeout => (
  169. is => 'lazy',
  170. default => sub { 10 }
  171. );
  172. =attr binary_mode
  173. Mostly intended for internal use, its builder coordinates all the side
  174. effects of interacting with the binary: locating the executable,
  175. finding an open port, setting up the environment, shelling out to
  176. start the binary, and ensuring that the webdriver is listening on the
  177. correct port.
  178. If all of the above steps pass, it will return truthy after
  179. instantiation. If any of them fail, it should return falsy and the
  180. class should attempt normal L<Selenium::Remote::Driver> behavior.
  181. =cut
  182. has 'binary_mode' => (
  183. is => 'lazy',
  184. init_arg => undef,
  185. builder => 1,
  186. predicate => 1
  187. );
  188. has 'try_binary' => (
  189. is => 'lazy',
  190. default => sub { 0 },
  191. trigger => sub {
  192. my ($self) = @_;
  193. $self->binary_mode if $self->try_binary;
  194. }
  195. );
  196. =attr window_title
  197. Intended for internal use: this will build us a unique title for the
  198. background binary process of the Webdriver. Then, when we're cleaning
  199. up, we know what the window title is that we're going to C<taskkill>.
  200. =cut
  201. has 'window_title' => (
  202. is => 'lazy',
  203. init_arg => undef,
  204. builder => sub {
  205. my ($self) = @_;
  206. my ( undef, undef, $file ) =
  207. File::Spec->splitpath( $self->_real_binary );
  208. my $port = $self->port;
  209. return $file . ':' . $port;
  210. }
  211. );
  212. =attr command
  213. Intended for internal use: this read-only attribute is built by us,
  214. but it can be useful after instantiation to see exactly what command
  215. was run to start the webdriver server.
  216. my $f = Selenium::Firefox->new;
  217. say $f->_command;
  218. =cut
  219. has '_command' => (
  220. is => 'lazy',
  221. init_arg => undef,
  222. builder => sub {
  223. my ($self) = @_;
  224. return $self->_construct_command;
  225. }
  226. );
  227. =attr logfile
  228. Normally we log what occurs in the driver to /dev/null (or /nul on windows).
  229. Setting this will redirect it to the provided file.
  230. =cut
  231. has 'logfile' => (
  232. is => 'lazy',
  233. default => sub {
  234. return '/nul' if IS_WIN;
  235. return '/dev/null';
  236. }
  237. );
  238. sub BUILDARGS {
  239. # There's a bit of finagling to do to since we can't ensure the
  240. # attribute instantiation order. To decide whether we're going into
  241. # binary mode, we need the remote_server_addr and port. But, they're
  242. # both lazy and only instantiated immediately before S:R:D's
  243. # remote_conn attribute. Once remote_conn is set, we can't change it,
  244. # so we need the following order:
  245. #
  246. # parent: remote_server_addr, port
  247. # role: binary_mode (aka _build_binary_mode)
  248. # parent: remote_conn
  249. #
  250. # Since we can't force an order, we introduced try_binary which gets
  251. # decided during BUILDARGS to tip us off as to whether we should try
  252. # binary mode or not.
  253. my ( undef, %args ) = @_;
  254. if ( !exists $args{remote_server_addr} && !exists $args{port} ) {
  255. $args{try_binary} = 1;
  256. # Windows may throw a fit about invalid pointers if we try to
  257. # connect to localhost instead of 127.1
  258. $args{remote_server_addr} = '127.0.0.1';
  259. }
  260. else {
  261. $args{try_binary} = 0;
  262. $args{binary_mode} = 0;
  263. }
  264. return {%args};
  265. }
  266. sub _build_binary_mode {
  267. my ($self) = @_;
  268. # We don't know what to do without a binary driver to start up
  269. return unless $self->_real_binary;
  270. # Either the user asked for 4444, or we couldn't find an open port
  271. my $port = $self->port + 0;
  272. return if $port == 4444;
  273. if ( $self->fixed_ports && $port == 0 ) {
  274. die 'port '
  275. . $self->binary_port
  276. . ' is not free and have requested fixed ports';
  277. }
  278. $self->_handle_firefox_setup($port);
  279. system( $self->_command );
  280. my $success =
  281. wait_until { probe_port($port) } timeout => $self->startup_timeout;
  282. if ($success) {
  283. return 1;
  284. }
  285. else {
  286. die 'Unable to connect to the '
  287. . $self->_real_binary
  288. . ' binary on port '
  289. . $port;
  290. }
  291. }
  292. sub _handle_firefox_setup {
  293. my ( $self, $port ) = @_;
  294. # This is a no-op for other browsers
  295. return unless $self->isa('Selenium::Firefox');
  296. my $user_profile =
  297. $self->has_firefox_profile
  298. ? $self->firefox_profile
  299. : 0;
  300. my $profile =
  301. setup_firefox_binary_env( $port, $self->marionette_port, $user_profile );
  302. if ( $self->_is_old_ff ) {
  303. # For non-geckodriver/non-marionette, we want to get rid of
  304. # the profile so that we don't accidentally zip it and encode
  305. # it down the line while Firefox is trying to read from it.
  306. $self->clear_firefox_profile if $self->has_firefox_profile;
  307. }
  308. else {
  309. # For geckodriver/marionette, we keep the enhanced profile around because
  310. # we need to send it to geckodriver as a zipped b64-encoded
  311. # directory.
  312. $self->firefox_profile($profile);
  313. }
  314. }
  315. sub shutdown_binary {
  316. my ($self) = @_;
  317. return unless $self->auto_close();
  318. if ( defined $self->session_id ) {
  319. $self->quit();
  320. }
  321. if ( $self->has_binary_mode && $self->binary_mode ) {
  322. # Tell the binary itself to shutdown
  323. my $port = $self->port;
  324. my $ua = $self->ua;
  325. $ua->get( 'http://127.0.0.1:' . $port . '/wd/hub/shutdown' );
  326. # Close the orphaned command windows on windows
  327. $self->shutdown_windows_binary;
  328. $self->shutdown_unix_binary;
  329. }
  330. }
  331. sub shutdown_unix_binary {
  332. my ($self) = @_;
  333. if (!IS_WIN) {
  334. my $cmd = "lsof -t -i :".$self->port();
  335. my ( $pid ) = grep { $_ && $_ ne $$ } split( /\s+/, scalar `$cmd` );
  336. if ($pid) {
  337. print "Killing Driver PID $pid listening on port "
  338. . $self->port . "...\n";
  339. eval { kill 'KILL', $pid };
  340. warn
  341. "Could not kill driver process! you may have to clean up manually."
  342. if $@;
  343. }
  344. }
  345. }
  346. sub shutdown_windows_binary {
  347. my ($self) = @_;
  348. if (IS_WIN) {
  349. if ( $self->_is_old_ff ) {
  350. # FIXME: Blech, handle a race condition that kills the
  351. # driver before it's finished cleaning up its sessions. In
  352. # particular, when the perl process ends, it wants to
  353. # clean up the temp directory it created for the Firefox
  354. # profile. But, if the Firefox process is still running,
  355. # it will have a lock on the temp profile directory, and
  356. # perl will get upset. This "solution" is _very_ bad.
  357. sleep(2);
  358. # Firefox doesn't have a Driver/Session architecture - the
  359. # only thing running is Firefox itself, so there's no
  360. # other task to kill.
  361. return;
  362. }
  363. system( 'taskkill /FI "WINDOWTITLE eq '
  364. . $self->window_title
  365. . '" > nul 2>&1' );
  366. }
  367. }
  368. sub DEMOLISH {
  369. my ( $self, $in_gd ) = @_;
  370. # if we're in global destruction, all bets are off.
  371. return if $in_gd;
  372. $self->shutdown_binary;
  373. }
  374. sub _construct_command {
  375. my ($self) = @_;
  376. my $executable = $self->_real_binary;
  377. # Executable path names may have spaces
  378. $executable = '"' . $executable . '"';
  379. # The different binaries take different arguments for proper setup
  380. $executable .= $self->_binary_args;
  381. if ( $self->has_custom_args ) {
  382. $executable .= ' ' . $self->custom_args;
  383. }
  384. # Handle Windows vs Unix discrepancies for invoking shell commands
  385. my ( $prefix, $suffix ) = ( $self->_cmd_prefix, $self->_cmd_suffix );
  386. return join( ' ', ( $prefix, $executable, $suffix ) );
  387. }
  388. sub _cmd_prefix {
  389. my ($self) = @_;
  390. my $prefix = '';
  391. if (IS_WIN) {
  392. $prefix = 'start "' . $self->window_title . '"';
  393. if ( $self->_is_old_ff ) {
  394. # For older versions of Firefox that run without
  395. # marionette, the command we're running actually starts up
  396. # the browser itself, so we don't want to minimize it.
  397. return $prefix;
  398. }
  399. else {
  400. # If we're firefox with marionette, or any other browser,
  401. # the command we're running is the driver, and we don't
  402. # need want the command window in the foreground.
  403. return $prefix . ' /MIN ';
  404. }
  405. }
  406. return $prefix;
  407. }
  408. sub _cmd_suffix {
  409. my ($self) = @_;
  410. return " > " . $self->logfile . " 2>&1 " if IS_WIN;
  411. return " > " . $self->logfile . " 2>&1 &";
  412. }
  413. =head1 SEE ALSO
  414. Selenium::Chrome
  415. Selenium::Firefox
  416. Selenium::PhantomJS
  417. =cut
  418. 1;