CanStartBinary.pm 6.6 KB

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