1
0

CanStartBinary.pm 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 Moo::Role;
  8. =head1 SYNOPSIS
  9. package My::Selenium::Chrome {
  10. use Moo;
  11. extends 'Selenium::Remote::Driver';
  12. has 'binary' => ( is => 'ro', default => 'chromedriver' );
  13. has 'binary_port' => ( is => 'ro', default => 9515 );
  14. with 'Selenium::CanStartBinary';
  15. 1
  16. };
  17. my $chrome_via_binary = My::Selenium::Chrome->new;
  18. my $chrome_with_path = My::Selenium::Chrome->new(
  19. binary => './chromedriver'
  20. );
  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. You may be
  26. interested in L<Selenium::Chrome>, L<Selenium::Firefox>, or
  27. L<Selenium::PhantomJS> if you're looking for classes that already
  28. consume this role.
  29. The role determines whether or not it should try to do its own magic
  30. based on whether the consuming class is instantiated with a
  31. C<remote_server_addr> and/or C<port>.
  32. # We'll start up the Chrome binary for you
  33. my $chrome_via_binary = Selenium::Chrome->new;
  34. # Look for a selenium server running on 4444.
  35. my $chrome_via_server = Selenium::Chrome->new( port => 4444 );
  36. If they're missing, we assume the user wants to use a webdriver
  37. directly and act accordingly. We handle finding the proper associated
  38. binary (or you can specify it with L</binary>), figuring out what
  39. arguments it wants, setting up any necessary environments, and
  40. starting up the binary.
  41. There's a number of TODOs left over - namely Windows support is
  42. severely lacking, and we're pretty naive when we attempt to locate the
  43. executables on our own.
  44. In the following documentation, C<required> refers to when you're
  45. consuming the role, not the C<required> when you're instantiating a
  46. class that has already consumed the role.
  47. =attr binary
  48. Required: Specify the path to the executable in question, or the name
  49. of the executable for us to find via L<File::Which/which>.
  50. =cut
  51. requires 'binary';
  52. =attr binary_port
  53. Required: Specify a default port that for the webdriver binary to try
  54. to bind to. If that port is unavailable, we'll probe above that port
  55. until we find a valid one.
  56. =cut
  57. requires 'binary_port';
  58. =attr port
  59. The role will attempt to determine the proper port for us. Consuming
  60. roles should set a default port in L</binary_port> at which we will
  61. begin searching for an open port.
  62. Note that if we cannot locate a suitable L</binary>, port will be set
  63. to 4444 so we can attempt to look for a Selenium server at
  64. C<127.0.0.1:4444>.
  65. =cut
  66. has '+port' => (
  67. is => 'lazy',
  68. builder => sub {
  69. my ($self) = @_;
  70. if ($self->binary) {
  71. return find_open_port_above($self->binary_port);
  72. }
  73. else {
  74. return '4444'
  75. }
  76. }
  77. );
  78. =attr binary_mode
  79. Mostly intended for internal use, its builder coordinates all the side
  80. effects of interacting with the binary: locating the executable,
  81. finding an open port, setting up the environment, shelling out to
  82. start the binary, and ensuring that the webdriver is listening on the
  83. correct port.
  84. If all of the above steps pass, it will return truthy after
  85. instantiation. If any of them fail, it should return falsy and the
  86. class should attempt normal L<Selenium::Remote::Driver> behavior.
  87. =cut
  88. has 'binary_mode' => (
  89. is => 'lazy',
  90. init_arg => undef,
  91. builder => 1,
  92. predicate => 1
  93. );
  94. has 'try_binary' => (
  95. is => 'lazy',
  96. default => sub { 0 },
  97. trigger => sub {
  98. my ($self) = @_;
  99. $self->binary_mode if $self->try_binary;
  100. }
  101. );
  102. =attr window_title
  103. Intended for internal use: this will build us a unique title for the
  104. background binary process of the Webdriver. Then, when we're cleaning
  105. up, we know what the window title is that we're going to C<taskkill>.
  106. =cut
  107. has 'window_title' => (
  108. is => 'lazy',
  109. init_arg => undef,
  110. builder => sub {
  111. my ($self) = @_;
  112. my (undef, undef, $file) = File::Spec->splitpath( $self->binary );
  113. my $port = $self->port;
  114. return $file . ':' . $port;
  115. }
  116. );
  117. use constant IS_WIN => $^O eq 'MSWin32';
  118. sub BUILDARGS {
  119. # There's a bit of finagling to do to since we can't ensure the
  120. # attribute instantiation order. To decide whether we're going into
  121. # binary mode, we need the remote_server_addr and port. But, they're
  122. # both lazy and only instantiated immediately before S:R:D's
  123. # remote_conn attribute. Once remote_conn is set, we can't change it,
  124. # so we need the following order:
  125. #
  126. # parent: remote_server_addr, port
  127. # role: binary_mode (aka _build_binary_mode)
  128. # parent: remote_conn
  129. #
  130. # Since we can't force an order, we introduced try_binary which gets
  131. # decided during BUILDARGS to tip us off as to whether we should try
  132. # binary mode or not.
  133. my ( $class, %args ) = @_;
  134. if ( ! exists $args{remote_server_addr} && ! exists $args{port} ) {
  135. $args{try_binary} = 1;
  136. # Windows may throw a fit about invalid pointers if we try to
  137. # connect to localhost instead of 127.1
  138. $args{remote_server_addr} = '127.0.0.1';
  139. }
  140. return { %args };
  141. }
  142. sub _build_binary_mode {
  143. my ($self) = @_;
  144. my $executable = $self->binary;
  145. return unless $executable;
  146. my $port = $self->port;
  147. return unless $port != 4444;
  148. if ($self->isa('Selenium::Firefox')) {
  149. setup_firefox_binary_env($port);
  150. }
  151. my $command = $self->_construct_command($executable, $port);
  152. system($command);
  153. my $success = wait_until { probe_port($port) } timeout => 10;
  154. if ($success) {
  155. return 1;
  156. }
  157. else {
  158. die 'Unable to connect to the ' . $executable . ' binary on port ' . $port;
  159. }
  160. }
  161. sub shutdown_binary {
  162. my ($self) = @_;
  163. # TODO: Allow user to keep browser open after test
  164. $self->quit;
  165. if ($self->has_binary_mode && $self->binary_mode) {
  166. my $port = $self->port;
  167. my $ua = $self->ua;
  168. $ua->get('127.0.0.1:' . $port . '/wd/hub/shutdown');
  169. # Close the additional command windows on windows
  170. if (IS_WIN) {
  171. # Blech, handle a race condition that kills the driver
  172. # before it's finished cleaning up its sessions
  173. sleep(1);
  174. $self->shutdown_windows_binary;
  175. }
  176. }
  177. }
  178. sub shutdown_windows_binary {
  179. my ($self) = @_;
  180. # Firefox doesn't have a Driver/Session architecture - the only
  181. # thing running is Firefox itself, so there's no other task to
  182. # kill.
  183. return if $self->isa('Selenium::Firefox');
  184. my $kill = 'taskkill /FI "WINDOWTITLE eq ' . $self->window_title . '"';
  185. system($kill);
  186. }
  187. before DEMOLISH => sub {
  188. my ($self) = @_;
  189. $self->shutdown_binary;
  190. };
  191. sub DEMOLISH { };
  192. sub _construct_command {
  193. my ($self, $executable, $port) = @_;
  194. # Handle spaces in executable path names
  195. $executable = '"' . $executable . '"';
  196. my %args;
  197. if ($executable =~ /chromedriver(\.exe)?"$/i) {
  198. %args = (
  199. port => $port,
  200. 'url-base' => 'wd/hub'
  201. );
  202. }
  203. elsif ($executable =~ /phantomjs(\.exe)?"$/i) {
  204. %args = (
  205. webdriver => '127.0.0.1:' . $port
  206. );
  207. }
  208. elsif ($executable =~ /firefox(-bin|\.exe)"$/i) {
  209. $executable .= ' -no-remote ';
  210. }
  211. my @args = map { '--' . $_ . '=' . $args{$_} } keys %args;
  212. # Handle Windows vs Unix discrepancies for invoking shell commands
  213. my ($prefix, $suffix) = ($self->_cmd_prefix, $self->_cmd_suffix);
  214. return join(' ', ($prefix, $executable, @args, $suffix) );
  215. }
  216. sub _cmd_prefix {
  217. my ($self) = @_;
  218. if (IS_WIN) {
  219. my $prefix = 'start "' . $self->window_title;
  220. # Let's minimize the command windows for the drivers that have
  221. # separate binaries - but let's not minimize the Firefox
  222. # window itself.
  223. if (! $self->isa('Selenium::Firefox')) {
  224. $prefix .= '" /MIN ';
  225. }
  226. return $prefix;
  227. }
  228. else {
  229. return '';
  230. }
  231. }
  232. sub _cmd_suffix {
  233. # TODO: allow users to specify whether & where they want driver
  234. # output to go
  235. if (IS_WIN) {
  236. return ' > /nul 2>&1 ';
  237. }
  238. else {
  239. return ' > /dev/null 2>&1 &';
  240. }
  241. }
  242. =head1 SEE ALSO
  243. Selenium::Chrome
  244. Selenium::Firefox
  245. Selenium::PhantomJS
  246. =cut
  247. 1;