1
0

CanStartBinary.pm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.
  37. In the following documentation, C<required> refers to when you're
  38. consuming the role, not the C<required> when you're instantiating a
  39. class that has already consumed the role.
  40. =attr binary
  41. Required: Specify the path to the executable in question, or the name
  42. of the executable for us to find via L<File::Which/which>.
  43. =cut
  44. requires 'binary';
  45. =attr binary_port
  46. Required: Specify a default port that for the webdriver binary to try
  47. to bind to. If that port is unavailable, we'll probe above that port
  48. until we find a valid one.
  49. =cut
  50. requires 'binary_port';
  51. has 'binary_mode' => (
  52. is => 'lazy',
  53. init_arg => undef,
  54. builder => 1,
  55. predicate => 1
  56. );
  57. has 'try_binary' => (
  58. is => 'lazy',
  59. default => sub { 0 },
  60. trigger => sub {
  61. my ($self) = @_;
  62. $self->binary_mode if $self->try_binary;
  63. }
  64. );
  65. sub BUILDARGS {
  66. # There's a bit of finagling to do to since we can't ensure the
  67. # attribute instantiation order. To decide whether we're going into
  68. # binary mode, we need the remote_server_addr and port. But, they're
  69. # both lazy and only instantiated immediately before S:R:D's
  70. # remote_conn attribute. Once remote_conn is set, we can't change it,
  71. # so we need the following order:
  72. #
  73. # parent: remote_server_addr, port
  74. # role: binary_mode (aka _build_binary_mode)
  75. # parent: remote_conn
  76. #
  77. # Since we can't force an order, we introduced try_binary which gets
  78. # decided during BUILDARGS to tip us off as to whether we should try
  79. # binary mode or not.
  80. my ( $class, %args ) = @_;
  81. if ( ! exists $args{remote_server_addr} && ! exists $args{port} ) {
  82. $args{try_binary} = 1;
  83. # Windows may throw a fit about invalid pointers if we try to
  84. # connect to localhost instead of 127.1
  85. $args{remote_server_addr} = '127.0.0.1';
  86. }
  87. return { %args };
  88. }
  89. sub _build_binary_mode {
  90. my ($self) = @_;
  91. my $port = $self->start_binary_on_port;
  92. $self->port($port);
  93. return 1;
  94. }
  95. sub probe_port {
  96. my ($port) = @_;
  97. return IO::Socket::INET->new(
  98. PeerAddr => '127.0.0.1',
  99. PeerPort => $port,
  100. Timeout => 3
  101. );
  102. }
  103. sub start_binary_on_port {
  104. my ($self) = @_;
  105. my $executable = $self->_find_executable;
  106. my $port = _find_open_port_above($self->binary_port);
  107. if (ref($self) eq 'Selenium::Firefox') {
  108. setup_firefox_binary_env($port);
  109. }
  110. my $command = $self->_construct_command($executable, $port);
  111. system($command);
  112. my $success = wait_until { probe_port($port) } timeout => 10;
  113. if ($success) {
  114. return $port;
  115. }
  116. else {
  117. die 'Unable to connect to the ' . $executable . ' binary on port ' . $port;
  118. }
  119. }
  120. sub shutdown_binary {
  121. my ($self) = @_;
  122. if ($self->has_binary_mode && $self->binary_mode) {
  123. my $port = $self->port;
  124. my $ua = $self->ua;
  125. $ua->get('127.0.0.1:' . $port . '/wd/hub/shutdown');
  126. }
  127. }
  128. sub _find_executable {
  129. my ($self) = @_;
  130. # If the user specified the full path to the binary, we don't have
  131. # any work to do.
  132. if ($self->has_binary) {
  133. if (-x abs_path($self->binary)) {
  134. return abs_path($self->binary);
  135. }
  136. else {
  137. die 'The binary at ' . $self->binary . ' is not executable. Choose the correct file or chmod +x it as needed.';
  138. }
  139. }
  140. my $binary = $self->binary;
  141. if ($binary eq 'firefox') {
  142. return firefox_path();
  143. }
  144. else {
  145. my $executable = which($binary);
  146. if (not defined $executable) {
  147. warn qq(Unable to find the $binary binary in your \$PATH. We'll try falling back to standard Remote Driver);
  148. }
  149. else {
  150. return $executable;
  151. }
  152. }
  153. }
  154. sub _construct_command {
  155. my ($self, $executable, $port) = @_;
  156. # Handle spaces in executable path names
  157. $executable = '"' . $executable . '"';
  158. my %args;
  159. if ($executable =~ /chromedriver(\.exe)?"$/i) {
  160. %args = (
  161. port => $port,
  162. 'url-base' => 'wd/hub'
  163. );
  164. }
  165. elsif ($executable =~ /phantomjs(\.exe)?"$/i) {
  166. %args = (
  167. webdriver => '127.0.0.1:' . $port
  168. );
  169. }
  170. elsif ($executable =~ /firefox(-bin|\.exe)"$/i) {
  171. $executable .= ' -no-remote ';
  172. }
  173. my @args = map { '--' . $_ . '=' . $args{$_} } keys %args;
  174. # Handle Windows vs Unix discrepancies for invoking shell commands
  175. my ($prefix, $suffix) = ($self->_command_prefix(), $self->_command_suffix());
  176. return join(' ', ($prefix, $executable, @args, $suffix) );
  177. }
  178. sub _command_prefix {
  179. my ($self) = @_;
  180. if ($^O eq 'MSWin32') {
  181. my $title = ref($self) . ':' . $self->binary_port;
  182. return 'start "' . $title . '" /MAX '
  183. }
  184. else {
  185. return '';
  186. }
  187. }
  188. sub _command_suffix {
  189. # TODO: allow users to specify whether & where they want driver
  190. # output to go
  191. if ($^O eq 'MSWin32') {
  192. return ' > /nul 2>&1 ';
  193. }
  194. else {
  195. return ' > /dev/null 2>&1 &';
  196. }
  197. }
  198. sub _find_open_port_above {
  199. my ($port) = @_;
  200. my $free_port = wait_until {
  201. if ( probe_port($port) ) {
  202. $port++;
  203. return 0;
  204. }
  205. else {
  206. return $port;
  207. }
  208. };
  209. return $free_port;
  210. }
  211. 1;