CanStartBinary.pm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package Selenium::CanStartBinary;
  2. # ABSTRACT: Teach a WebDriver how to start its own binary aka no JRE!
  3. use File::Spec;
  4. use Selenium::CanStartBinary::ProbePort qw/find_open_port_above probe_port/;
  5. use Selenium::Firefox::Binary qw/setup_firefox_binary_env/;
  6. use Selenium::Waiter qw/wait_until/;
  7. use Selenium::Firefox::Profile;
  8. use Moo::Role;
  9. =head1 NAME
  10. CanStartBinary - Role that a Selenium::Remote::Driver can consume to start a binary
  11. =head1 SYNOPSIS
  12. package ChromeDriver {
  13. use Moo;
  14. with 'Selenium::CanStartBinary';
  15. extends 'Selenium::Remote::Driver';
  16. has 'binary' => ( is => 'ro', default => 'chromedriver' );
  17. has 'binary_port' => ( is => 'ro', default => 9515 );
  18. 1
  19. };
  20. my $chrome_via_binary = ChromeDriver->new;
  21. =head1 DESCRIPTION
  22. This role takes care of the details for starting up a Webdriver
  23. instance. It does not do any downloading or installation of any sort -
  24. you're still responsible for obtaining and installing the necessary
  25. binaries into your C<$PATH> for this role to find.
  26. The role determines whether or not it should try to do its own magic
  27. based on whether or not the consuming class is instantiated with a
  28. C<remote_server_addr> and/or C<port>. If they're missing, we assume
  29. the user wants to use the Webdrivers directly and act
  30. accordingly. We'll go find the proper associated binary (or you can
  31. specify it with L</binary_path>), figure out what arguments it wants,
  32. set up any necessary environments, and start 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 port
  51. The role will attempt to determine the proper port for us. Consuming
  52. roles should set a default port in L</binary_port> at which we will
  53. begin searching for an open port.
  54. Note that if we cannot locate a suitable L</binary>, port will be set
  55. to 4444 so we can attempt to look for a Selenium server at
  56. C<127.0.0.1:4444>.
  57. =cut
  58. has 'port' => (
  59. is => 'lazy',
  60. builder => sub {
  61. my ($self) = @_;
  62. if ($self->binary) {
  63. return find_open_port_above($self->binary_port);
  64. }
  65. else {
  66. return '4444'
  67. }
  68. }
  69. );
  70. =attr binary_mode
  71. Mostly intended for internal use, its builder coordinates all the side
  72. effects of interacting with the binary: locating the executable,
  73. finding an open port, setting up the environment, shelling out to
  74. start the binary, and ensuring that the webdriver is listening on the
  75. correct port.
  76. If all of the above steps pass, it will return truthy after
  77. instantiation. If any of them fail, it should return falsy and the
  78. class should attempt normal L<Selenium::Remote::Driver> behavior.
  79. =cut
  80. has 'binary_mode' => (
  81. is => 'lazy',
  82. init_arg => undef,
  83. builder => 1,
  84. predicate => 1
  85. );
  86. has 'try_binary' => (
  87. is => 'lazy',
  88. default => sub { 0 },
  89. trigger => sub {
  90. my ($self) = @_;
  91. $self->binary_mode if $self->try_binary;
  92. }
  93. );
  94. =attr window_title
  95. Intended for internal use: this will build us a unique title for the
  96. background binary process of the Webdriver. Then, when we're cleaning
  97. up, we know what the window title is that we're going to C<taskkill>.
  98. =cut
  99. has 'window_title' => (
  100. is => 'lazy',
  101. init_arg => undef,
  102. builder => sub {
  103. my ($self) = @_;
  104. my (undef, undef, $file) = File::Spec->splitpath( $self->binary );
  105. my $port = $self->port;
  106. return $file . ':' . $port;
  107. }
  108. );
  109. use constant IS_WIN => $^O eq 'MSWin32';
  110. sub BUILDARGS {
  111. # There's a bit of finagling to do to since we can't ensure the
  112. # attribute instantiation order. To decide whether we're going into
  113. # binary mode, we need the remote_server_addr and port. But, they're
  114. # both lazy and only instantiated immediately before S:R:D's
  115. # remote_conn attribute. Once remote_conn is set, we can't change it,
  116. # so we need the following order:
  117. #
  118. # parent: remote_server_addr, port
  119. # role: binary_mode (aka _build_binary_mode)
  120. # parent: remote_conn
  121. #
  122. # Since we can't force an order, we introduced try_binary which gets
  123. # decided during BUILDARGS to tip us off as to whether we should try
  124. # binary mode or not.
  125. my ( $class, %args ) = @_;
  126. if ( ! exists $args{remote_server_addr} && ! exists $args{port} ) {
  127. $args{try_binary} = 1;
  128. # Windows may throw a fit about invalid pointers if we try to
  129. # connect to localhost instead of 127.1
  130. $args{remote_server_addr} = '127.0.0.1';
  131. }
  132. return { %args };
  133. }
  134. sub _build_binary_mode {
  135. my ($self) = @_;
  136. my $executable = $self->binary;
  137. return unless $executable;
  138. my $port = $self->port;
  139. return unless $port != 4444;
  140. if (ref($self) eq 'Selenium::Firefox') {
  141. setup_firefox_binary_env($port);
  142. }
  143. my $command = $self->_construct_command($executable, $port);
  144. system($command);
  145. my $success = wait_until { probe_port($port) } timeout => 10;
  146. if ($success) {
  147. return 1;
  148. }
  149. else {
  150. die 'Unable to connect to the ' . $executable . ' binary on port ' . $port;
  151. }
  152. }
  153. sub shutdown_binary {
  154. my ($self) = @_;
  155. if ($self->has_binary_mode && $self->binary_mode) {
  156. my $port = $self->port;
  157. my $ua = $self->ua;
  158. $ua->get('127.0.0.1:' . $port . '/wd/hub/shutdown');
  159. # close the additional command windows on windows
  160. if (IS_WIN) {
  161. $self->shutdown_windows_binary;
  162. }
  163. }
  164. }
  165. sub shutdown_windows_binary {
  166. my ($self) = @_;
  167. my $kill = 'taskkill /FI "WINDOWTITLE eq ' . $self->window_title . '"';
  168. system($kill);
  169. }
  170. before DEMOLISH => sub {
  171. my ($self) = @_;
  172. $self->shutdown_binary;
  173. };
  174. sub DEMOLISH { };
  175. sub _construct_command {
  176. my ($self, $executable, $port) = @_;
  177. # Handle spaces in executable path names
  178. $executable = '"' . $executable . '"';
  179. my %args;
  180. if ($executable =~ /chromedriver(\.exe)?"$/i) {
  181. %args = (
  182. port => $port,
  183. 'url-base' => 'wd/hub'
  184. );
  185. }
  186. elsif ($executable =~ /phantomjs(\.exe)?"$/i) {
  187. %args = (
  188. webdriver => '127.0.0.1:' . $port
  189. );
  190. }
  191. elsif ($executable =~ /firefox(-bin|\.exe)"$/i) {
  192. $executable .= ' -no-remote ';
  193. }
  194. my @args = map { '--' . $_ . '=' . $args{$_} } keys %args;
  195. # Handle Windows vs Unix discrepancies for invoking shell commands
  196. my ($prefix, $suffix) = ($self->_command_prefix(), $self->_command_suffix());
  197. return join(' ', ($prefix, $executable, @args, $suffix) );
  198. }
  199. sub _command_prefix {
  200. my ($self) = @_;
  201. if ($^O eq 'MSWin32') {
  202. my $title = ref($self) . ':' . $self->binary_port;
  203. return 'start "' . $title . '" /MAX '
  204. }
  205. else {
  206. return '';
  207. }
  208. }
  209. sub _command_suffix {
  210. # TODO: allow users to specify whether & where they want driver
  211. # output to go
  212. if ($^O eq 'MSWin32') {
  213. return ' > /nul 2>&1 ';
  214. }
  215. else {
  216. return ' > /dev/null 2>&1 &';
  217. }
  218. }
  219. =head1 SEE ALSO
  220. Selenium::Chrome
  221. Selenium::Firefox
  222. Selenium::PhantomJS
  223. =cut
  224. 1;