CanStartBinary.pm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package Selenium::CanStartBinary;
  2. # ABSTRACT: Teach a WebDriver how to start its own binary aka no JRE!
  3. use IO::Socket::INET;
  4. use Selenium::Waiter qw/wait_until/;
  5. use Selenium::Firefox::Binary qw/firefox_path setup_firefox_binary_env/;
  6. use Selenium::Firefox::Profile;
  7. use Moo::Role;
  8. =head1 NAME
  9. CanStartBinary - Role that a Selenium::Remote::Driver can consume to start a binary
  10. =head1 SYNOPSIS
  11. package ChromeDriver {
  12. use Moo;
  13. with 'Selenium::CanStartBinary';
  14. extends 'Selenium::Remote::Driver';
  15. has 'binary' => ( is => 'ro', default => 'chromedriver' );
  16. has 'binary_port' => ( is => 'ro', default => 9515 );
  17. 1
  18. };
  19. my $chrome_via_binary = ChromeDriver->new;
  20. =head1 DESCRIPTION
  21. This role takes care of the details for starting up a Webdriver
  22. instance. It does not do any downloading or installation of any sort -
  23. you're still responsible for obtaining and installing the necessary
  24. binaries into your C<$PATH> for this role to find.
  25. The role determines whether or not it should try to do its own magic
  26. based on whether or not the consuming class is instantiated with a
  27. C<remote_server_addr> and/or C<port>. If they're missing, we assume
  28. the user wants to use the Webdrivers directly and act
  29. accordingly. We'll go find the proper associated binary (or you can
  30. specify it with L</binary_path>), figure out what arguments it wants,
  31. set up any necessary environments, and start up the binary.
  32. There's a number of TODOs left over - namely Windows support is
  33. severely lacking, and we're pretty naive when we attempt to locate the
  34. executables on our own.
  35. In the following documentation, C<required> refers to when you're
  36. consuming the role, not the C<required> when you're instantiating a
  37. class that has already consumed the role.
  38. =attr binary
  39. Required: Specify the path to the executable in question, or the name
  40. of the executable for us to find via L<File::Which/which>.
  41. =cut
  42. requires 'binary';
  43. =attr binary_port
  44. Required: Specify a default port that for the webdriver binary to try
  45. to bind to. If that port is unavailable, we'll probe above that port
  46. until we find a valid one.
  47. =cut
  48. requires 'binary_port';
  49. has 'binary_mode' => (
  50. is => 'lazy',
  51. init_arg => undef,
  52. builder => 1,
  53. predicate => 1
  54. );
  55. has 'try_binary' => (
  56. is => 'lazy',
  57. default => sub { 0 },
  58. trigger => sub {
  59. my ($self) = @_;
  60. $self->binary_mode if $self->try_binary;
  61. }
  62. );
  63. sub BUILDARGS {
  64. # There's a bit of finagling to do to since we can't ensure the
  65. # attribute instantiation order. To decide whether we're going into
  66. # binary mode, we need the remote_server_addr and port. But, they're
  67. # both lazy and only instantiated immediately before S:R:D's
  68. # remote_conn attribute. Once remote_conn is set, we can't change it,
  69. # so we need the following order:
  70. #
  71. # parent: remote_server_addr, port
  72. # role: binary_mode (aka _build_binary_mode)
  73. # parent: remote_conn
  74. #
  75. # Since we can't force an order, we introduced try_binary which gets
  76. # decided during BUILDARGS to tip us off as to whether we should try
  77. # binary mode or not.
  78. my ( $class, %args ) = @_;
  79. if ( ! exists $args{remote_server_addr} && ! exists $args{port} ) {
  80. $args{try_binary} = 1;
  81. # Windows may throw a fit about invalid pointers if we try to
  82. # connect to localhost instead of 127.1
  83. $args{remote_server_addr} = '127.0.0.1';
  84. }
  85. return { %args };
  86. }
  87. sub _build_binary_mode {
  88. my ($self) = @_;
  89. my $port = $self->start_binary_on_port;
  90. $self->port($port);
  91. return 1;
  92. }
  93. sub probe_port {
  94. my ($port) = @_;
  95. return IO::Socket::INET->new(
  96. PeerAddr => '127.0.0.1',
  97. PeerPort => $port,
  98. Timeout => 3
  99. );
  100. }
  101. sub start_binary_on_port {
  102. my ($self) = @_;
  103. my $executable = $self->binary;
  104. my $port = _find_open_port_above($self->binary_port);
  105. if (ref($self) eq 'Selenium::Firefox') {
  106. setup_firefox_binary_env($port);
  107. }
  108. my $command = $self->_construct_command($executable, $port);
  109. system($command);
  110. my $success = wait_until { probe_port($port) } timeout => 10;
  111. if ($success) {
  112. return $port;
  113. }
  114. else {
  115. die 'Unable to connect to the ' . $executable . ' binary on port ' . $port;
  116. }
  117. }
  118. sub shutdown_binary {
  119. my ($self) = @_;
  120. if ($self->has_binary_mode && $self->binary_mode) {
  121. my $port = $self->port;
  122. my $ua = $self->ua;
  123. $ua->get('127.0.0.1:' . $port . '/wd/hub/shutdown');
  124. }
  125. }
  126. my ($self) = @_;
  127. }
  128. sub _construct_command {
  129. my ($self, $executable, $port) = @_;
  130. # Handle spaces in executable path names
  131. $executable = '"' . $executable . '"';
  132. my %args;
  133. if ($executable =~ /chromedriver(\.exe)?"$/i) {
  134. %args = (
  135. port => $port,
  136. 'url-base' => 'wd/hub'
  137. );
  138. }
  139. elsif ($executable =~ /phantomjs(\.exe)?"$/i) {
  140. %args = (
  141. webdriver => '127.0.0.1:' . $port
  142. );
  143. }
  144. elsif ($executable =~ /firefox(-bin|\.exe)"$/i) {
  145. $executable .= ' -no-remote ';
  146. }
  147. my @args = map { '--' . $_ . '=' . $args{$_} } keys %args;
  148. # Handle Windows vs Unix discrepancies for invoking shell commands
  149. my ($prefix, $suffix) = ($self->_command_prefix(), $self->_command_suffix());
  150. return join(' ', ($prefix, $executable, @args, $suffix) );
  151. }
  152. sub _command_prefix {
  153. my ($self) = @_;
  154. if ($^O eq 'MSWin32') {
  155. my $title = ref($self) . ':' . $self->binary_port;
  156. return 'start "' . $title . '" /MAX '
  157. }
  158. else {
  159. return '';
  160. }
  161. }
  162. sub _command_suffix {
  163. # TODO: allow users to specify whether & where they want driver
  164. # output to go
  165. if ($^O eq 'MSWin32') {
  166. return ' > /nul 2>&1 ';
  167. }
  168. else {
  169. return ' > /dev/null 2>&1 &';
  170. }
  171. }
  172. sub _find_open_port_above {
  173. my ($port) = @_;
  174. my $free_port = wait_until {
  175. if ( probe_port($port) ) {
  176. $port++;
  177. return 0;
  178. }
  179. else {
  180. return $port;
  181. }
  182. };
  183. return $free_port;
  184. }
  185. 1;