CanStartBinary.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. package Selenium::CanStartBinary;
  2. use strict;
  3. use warnings;
  4. # ABSTRACT: Teach a WebDriver how to start its own binary aka no JRE!
  5. use File::Spec;
  6. use Selenium::CanStartBinary::ProbePort qw/find_open_port_above find_open_port probe_port/;
  7. use Selenium::Firefox::Binary qw/setup_firefox_binary_env/;
  8. use Selenium::Waiter qw/wait_until/;
  9. use Moo::Role;
  10. use constant IS_WIN => $^O eq 'MSWin32';
  11. =for Pod::Coverage *EVERYTHING*
  12. =head1 DESCRIPTION
  13. This role takes care of the details for starting up a Webdriver
  14. instance. It does not do any downloading or installation of any sort -
  15. you're still responsible for obtaining and installing the necessary
  16. binaries into your C<$PATH> for this role to find. You may be
  17. interested in L<Selenium::Chrome>, L<Selenium::Firefox>, or
  18. L<Selenium::PhantomJS> if you're looking for classes that already
  19. consume this role.
  20. The role determines whether or not it should try to do its own magic
  21. based on whether the consuming class is instantiated with a
  22. C<remote_server_addr> and/or C<port>.
  23. # We'll start up the Chrome binary for you
  24. my $chrome_via_binary = Selenium::Chrome->new;
  25. # Look for a selenium server running on 4444.
  26. my $chrome_via_server = Selenium::Chrome->new( port => 4444 );
  27. If they're missing, we assume the user wants to use a webdriver
  28. directly and act accordingly. We handle finding the proper associated
  29. binary (or you can specify it with L</binary>), figuring out what
  30. arguments it wants, setting up any necessary environments, and
  31. starting 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. =attr _binary_args
  50. Required: Specify the arguments that the particular binary needs in
  51. order to start up correctly. In particular, you may need to tell the
  52. binary about the proper port when we start it up, or that it should
  53. use a particular prefix to match up with the behavior of the Remote
  54. Driver server.
  55. If your binary doesn't need any arguments, just have the default be an
  56. empty string.
  57. =cut
  58. requires '_binary_args';
  59. =attr port
  60. The role will attempt to determine the proper port for us. Consuming
  61. roles should set a default port in L</binary_port> at which we will
  62. begin searching for an open port.
  63. Note that if we cannot locate a suitable L</binary>, port will be set
  64. to 4444 so we can attempt to look for a Selenium server at
  65. C<127.0.0.1:4444>.
  66. =cut
  67. has '_real_binary' => (
  68. is => 'lazy',
  69. builder => sub {
  70. my ($self) = @_;
  71. if ($self->_is_old_ff) {
  72. return $self->firefox_binary;
  73. }
  74. else {
  75. return $self->binary;
  76. }
  77. }
  78. );
  79. has '_is_old_ff' => (
  80. is => 'lazy',
  81. builder => sub {
  82. my ($self) = @_;
  83. return $self->isa('Selenium::Firefox') && !$self->marionette_enabled;
  84. }
  85. );
  86. has '+port' => (
  87. is => 'lazy',
  88. builder => sub {
  89. my ($self) = @_;
  90. if ($self->_real_binary) {
  91. if ($self->fixed_ports) {
  92. return find_open_port($self->binary_port);
  93. }
  94. else {
  95. return find_open_port_above($self->binary_port);
  96. }
  97. }
  98. else {
  99. return 4444
  100. }
  101. }
  102. );
  103. =attr fixed_ports
  104. Optional: By default, if binary_port and marionette_port are not free
  105. a higher free port is probed and acquired if possible, until a free one
  106. if found or a timeout is exceeded.
  107. my $driver1 = Selenium::Chrome->new;
  108. my $driver2 = Selenium::Chrome->new( port => 1234 );
  109. The default behavior can be overridden. In this case, only the default
  110. or given binary_port and marionette_port are probed, without probing
  111. higher ports. This ensures that either the default or given port will be
  112. assigned, or no port will be assigned at all.
  113. my $driver1 = Selenium::Chrome->new( fixed_ports => 1 );
  114. my $driver2 = Selenium::Chrome->new( port => 1234, fixed_ports => 1);
  115. =cut
  116. has 'fixed_ports' => (
  117. is => 'lazy',
  118. default => sub { 0 }
  119. );
  120. =attr custom_args
  121. Optional: If you want to pass additional options to the binary when it
  122. starts up, you can add that here. For example, if your binary accepts
  123. an argument on the command line like C<--log-path=/path/to/log>, and
  124. you'd like to specify that the binary uses that option, you could do:
  125. my $chrome = Selenium::Chrome->new(
  126. custom_args => '--log-path=/path/to/log'
  127. );
  128. To specify multiple arguments, just include them all in the string.
  129. =cut
  130. has custom_args => (
  131. is => 'lazy',
  132. predicate => 1,
  133. default => sub { '' }
  134. );
  135. has 'marionette_port' => (
  136. is => 'lazy',
  137. builder => sub {
  138. my ($self) = @_;
  139. if ($self->_is_old_ff) {
  140. return 0;
  141. }
  142. else {
  143. if ($self->fixed_ports) {
  144. return find_open_port($self->marionette_binary_port);
  145. }
  146. else {
  147. return find_open_port_above($self->marionette_binary_port);
  148. }
  149. }
  150. }
  151. );
  152. =attr startup_timeout
  153. Optional: you can modify how long we will wait for the binary to start
  154. up. By default, we will start the binary and check the intended
  155. destination port for 10 seconds before giving up. If the machine
  156. you're using to run your browsers is slower or smaller, you may need
  157. to increase this timeout.
  158. The following:
  159. my $f = Selenium::Firefox->new(
  160. startup_timeout => 60
  161. );
  162. will wait up to 60 seconds for the firefox binary to respond on the
  163. proper port. To use this constructor option, you should specify a time
  164. in seconds as an integer, and it will be passed to the arguments
  165. section of a L<Selenium::Waiter/wait_until> subroutine call.
  166. =cut
  167. has startup_timeout => (
  168. is => 'lazy',
  169. default => sub { 10 }
  170. );
  171. =attr binary_mode
  172. Mostly intended for internal use, its builder coordinates all the side
  173. effects of interacting with the binary: locating the executable,
  174. finding an open port, setting up the environment, shelling out to
  175. start the binary, and ensuring that the webdriver is listening on the
  176. correct port.
  177. If all of the above steps pass, it will return truthy after
  178. instantiation. If any of them fail, it should return falsy and the
  179. class should attempt normal L<Selenium::Remote::Driver> behavior.
  180. =cut
  181. has 'binary_mode' => (
  182. is => 'lazy',
  183. init_arg => undef,
  184. builder => 1,
  185. predicate => 1
  186. );
  187. has 'try_binary' => (
  188. is => 'lazy',
  189. default => sub { 0 },
  190. trigger => sub {
  191. my ($self) = @_;
  192. $self->binary_mode if $self->try_binary;
  193. }
  194. );
  195. =attr window_title
  196. Intended for internal use: this will build us a unique title for the
  197. background binary process of the Webdriver. Then, when we're cleaning
  198. up, we know what the window title is that we're going to C<taskkill>.
  199. =cut
  200. has 'window_title' => (
  201. is => 'lazy',
  202. init_arg => undef,
  203. builder => sub {
  204. my ($self) = @_;
  205. my (undef, undef, $file) = File::Spec->splitpath( $self->_real_binary );
  206. my $port = $self->port;
  207. return $file . ':' . $port;
  208. }
  209. );
  210. =attr command
  211. Intended for internal use: this read-only attribute is built by us,
  212. but it can be useful after instantiation to see exactly what command
  213. was run to start the webdriver server.
  214. my $f = Selenium::Firefox->new;
  215. say $f->_command;
  216. =cut
  217. has '_command' => (
  218. is => 'lazy',
  219. init_arg => undef,
  220. builder => sub {
  221. my ($self) = @_;
  222. return $self->_construct_command;
  223. }
  224. );
  225. =attr logfile
  226. Normally we log what occurs in the driver to /dev/null (or /nul on windows).
  227. Setting this will redirect it to the provided file.
  228. =cut
  229. has 'logfile' => (
  230. is => 'lazy',
  231. default => sub {
  232. return '/nul' if IS_WIN;
  233. return '/dev/null';
  234. }
  235. );
  236. sub BUILDARGS {
  237. # There's a bit of finagling to do to since we can't ensure the
  238. # attribute instantiation order. To decide whether we're going into
  239. # binary mode, we need the remote_server_addr and port. But, they're
  240. # both lazy and only instantiated immediately before S:R:D's
  241. # remote_conn attribute. Once remote_conn is set, we can't change it,
  242. # so we need the following order:
  243. #
  244. # parent: remote_server_addr, port
  245. # role: binary_mode (aka _build_binary_mode)
  246. # parent: remote_conn
  247. #
  248. # Since we can't force an order, we introduced try_binary which gets
  249. # decided during BUILDARGS to tip us off as to whether we should try
  250. # binary mode or not.
  251. my ( undef, %args ) = @_;
  252. if ( ! exists $args{remote_server_addr} && ! exists $args{port} ) {
  253. $args{try_binary} = 1;
  254. # Windows may throw a fit about invalid pointers if we try to
  255. # connect to localhost instead of 127.1
  256. $args{remote_server_addr} = '127.0.0.1';
  257. }
  258. else {
  259. $args{try_binary} = 0;
  260. $args{binary_mode} = 0;
  261. }
  262. return { %args };
  263. }
  264. sub _build_binary_mode {
  265. my ($self) = @_;
  266. # We don't know what to do without a binary driver to start up
  267. return unless $self->_real_binary;
  268. # Either the user asked for 4444, or we couldn't find an open port
  269. my $port = $self->port + 0;
  270. return if $port == 4444;
  271. if( $self->fixed_ports && $port == 0 ){
  272. die 'port ' . $self->binary_port . ' is not free and have requested fixed ports';
  273. }
  274. $self->_handle_firefox_setup($port);
  275. system($self->_command);
  276. my $success = wait_until { probe_port($port) } timeout => $self->startup_timeout;
  277. if ($success) {
  278. return 1;
  279. }
  280. else {
  281. die 'Unable to connect to the ' . $self->_real_binary . ' binary on port ' . $port;
  282. }
  283. }
  284. sub _handle_firefox_setup {
  285. my ($self, $port) = @_;
  286. # This is a no-op for other browsers
  287. return unless $self->isa('Selenium::Firefox');
  288. my $user_profile = $self->has_firefox_profile
  289. ? $self->firefox_profile
  290. : 0;
  291. my $profile = setup_firefox_binary_env(
  292. $port,
  293. $self->marionette_port,
  294. $user_profile
  295. );
  296. if ($self->_is_old_ff) {
  297. # For non-geckodriver/non-marionette, we want to get rid of
  298. # the profile so that we don't accidentally zip it and encode
  299. # it down the line while Firefox is trying to read from it.
  300. $self->clear_firefox_profile if $self->has_firefox_profile;
  301. }
  302. else {
  303. # For geckodriver/marionette, we keep the enhanced profile around because
  304. # we need to send it to geckodriver as a zipped b64-encoded
  305. # directory.
  306. $self->firefox_profile($profile);
  307. }
  308. }
  309. sub shutdown_binary {
  310. my ($self) = @_;
  311. if ( $self->auto_close && defined $self->session_id ) {
  312. $self->quit();
  313. }
  314. if ($self->has_binary_mode && $self->binary_mode) {
  315. # Tell the binary itself to shutdown
  316. my $port = $self->port;
  317. my $ua = $self->ua;
  318. $ua->get('http://127.0.0.1:' . $port . '/wd/hub/shutdown');
  319. # Close the orphaned command windows on windows
  320. $self->shutdown_windows_binary;
  321. $self->shutdown_unix_binary;
  322. }
  323. }
  324. sub shutdown_unix_binary {
  325. my ($self) = @_;
  326. if (!IS_WIN) {
  327. my $cmd = "lsof -t -i :".$self->port();
  328. my ( $pid ) = grep { $_ && $_ ne $$ } split /\s+/, scalar `$cmd`;
  329. if ($pid) {
  330. print "Killing Driver PID $pid listening on port ".$self->port."...\n";
  331. eval { kill 'KILL', $pid };
  332. warn "Could not kill driver process! you may have to clean up manually." if $@;
  333. }
  334. }
  335. }
  336. sub shutdown_windows_binary {
  337. my ($self) = @_;
  338. if (IS_WIN) {
  339. if ($self->_is_old_ff) {
  340. # FIXME: Blech, handle a race condition that kills the
  341. # driver before it's finished cleaning up its sessions. In
  342. # particular, when the perl process ends, it wants to
  343. # clean up the temp directory it created for the Firefox
  344. # profile. But, if the Firefox process is still running,
  345. # it will have a lock on the temp profile directory, and
  346. # perl will get upset. This "solution" is _very_ bad.
  347. sleep(2);
  348. # Firefox doesn't have a Driver/Session architecture - the
  349. # only thing running is Firefox itself, so there's no
  350. # other task to kill.
  351. return;
  352. }
  353. system('taskkill /FI "WINDOWTITLE eq ' . $self->window_title . '" > nul 2>&1');
  354. }
  355. }
  356. sub DEMOLISH {
  357. my ($self, $in_gd) = @_;
  358. # if we're in global destruction, all bets are off.
  359. return if $in_gd;
  360. $self->shutdown_binary;
  361. }
  362. sub _construct_command {
  363. my ($self) = @_;
  364. my $executable = $self->_real_binary;
  365. # Executable path names may have spaces
  366. $executable = '"' . $executable . '"';
  367. # The different binaries take different arguments for proper setup
  368. $executable .= $self->_binary_args;
  369. if ($self->has_custom_args) {
  370. $executable .= ' ' . $self->custom_args;
  371. }
  372. # Handle Windows vs Unix discrepancies for invoking shell commands
  373. my ($prefix, $suffix) = ($self->_cmd_prefix, $self->_cmd_suffix);
  374. return join(' ', ($prefix, $executable, $suffix) );
  375. }
  376. sub _cmd_prefix {
  377. my ($self) = @_;
  378. my $prefix = '';
  379. if (IS_WIN) {
  380. $prefix = 'start "' . $self->window_title . '"';
  381. if ($self->_is_old_ff) {
  382. # For older versions of Firefox that run without
  383. # marionette, the command we're running actually starts up
  384. # the browser itself, so we don't want to minimize it.
  385. return $prefix;
  386. }
  387. else {
  388. # If we're firefox with marionette, or any other browser,
  389. # the command we're running is the driver, and we don't
  390. # need want the command window in the foreground.
  391. return $prefix . ' /MIN ';
  392. }
  393. }
  394. return $prefix;
  395. }
  396. sub _cmd_suffix {
  397. my ($self) = @_;
  398. return " > ".$self->logfile." 2>&1 " if IS_WIN;
  399. return " > ".$self->logfile." 2>&1 &";
  400. }
  401. =head1 SEE ALSO
  402. Selenium::Chrome
  403. Selenium::Firefox
  404. Selenium::PhantomJS
  405. =cut
  406. 1;