Chrome.pm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package Selenium::Chrome;
  2. # ABSTRACT: Use ChromeDriver without a Selenium server
  3. use Moo;
  4. use Selenium::CanStartBinary::FindBinary qw/coerce_simple_binary/;
  5. extends 'Selenium::Remote::Driver';
  6. =head1 SYNOPSIS
  7. my $driver = Selenium::Chrome->new;
  8. # when you're done
  9. $driver->shutdown_binary;
  10. =head1 DESCRIPTION
  11. This class allows you to use the ChromeDriver without needing the JRE
  12. or a selenium server running. When you refrain from passing the
  13. C<remote_server_addr> and C<port> arguments, we will search for the
  14. chromedriver executable binary in your $PATH. We'll try to start the
  15. binary connect to it, shutting it down at the end of the test.
  16. If the chromedriver binary is not found, we'll fall back to the
  17. default L<Selenium::Remote::Driver> behavior of assuming defaults of
  18. 127.0.0.1:4444 after waiting a few seconds.
  19. If you specify a remote server address, or a port, we'll assume you
  20. know what you're doing and take no additional behavior.
  21. If you're curious whether your Selenium::Chrome instance is using a
  22. separate ChromeDriver binary, or through the selenium server, you can
  23. check the C<binary_mode> attr after instantiation.
  24. =cut
  25. has '+browser_name' => (
  26. is => 'ro',
  27. default => sub { 'chrome' }
  28. );
  29. =attr binary
  30. Optional: specify the path to your binary. If you don't specify
  31. anything, we'll try to find it on our own via L<File::Which/which>.
  32. =cut
  33. has 'binary' => (
  34. is => 'lazy',
  35. coerce => \&coerce_simple_binary,
  36. default => sub { 'chromedriver' },
  37. predicate => 1
  38. );
  39. =attr binary_port
  40. Optional: specify the port that we should bind to. If you don't
  41. specify anything, we'll default to the driver's default port. Since
  42. there's no a priori guarantee that this will be an open port, this is
  43. _not_ necessarily the port that we end up using - if the port here is
  44. already bound, we'll search above it until we find an open one.
  45. See L<Selenium::CanStartBinary/port> for more details, and
  46. L<Selenium::Remote::Driver/port> after instantiation to see what the
  47. actual port turned out to be.
  48. =cut
  49. has 'binary_port' => (
  50. is => 'lazy',
  51. default => sub { 9515 }
  52. );
  53. has '_binary_args' => (
  54. is => 'lazy',
  55. builder => sub {
  56. my ($self) = @_;
  57. my $context = $self->wd_context_prefix;
  58. $context =~ s{^/}{};
  59. return ' --port=' . $self->port . ' --url-base=' . $context . ' ';
  60. }
  61. );
  62. with 'Selenium::CanStartBinary';
  63. =attr custom_args
  64. Optional: specify any additional command line arguments you'd like
  65. invoked during the binary startup. See
  66. L<Selenium::CanStartBinary/custom_args> for more information.
  67. =attr startup_timeout
  68. Optional: specify how long to wait for the binary to start itself and
  69. listen on its port. The default duration is arbitrarily 10 seconds. It
  70. accepts an integer number of seconds to wait: the following will wait
  71. up to 20 seconds:
  72. Selenium::Chrome->new( startup_timeout => 20 );
  73. See L<Selenium::CanStartBinary/startup_timeout> for more information.
  74. =method shutdown_binary
  75. Call this method instead of L<Selenium::Remote::Driver/quit> to ensure
  76. that the binary executable is also closed, instead of simply closing
  77. the browser itself. If the browser is still around, it will call
  78. C<quit> for you. After that, it will try to shutdown the browser
  79. binary by making a GET to /shutdown and on Windows, it will attempt to
  80. do a C<taskkill> on the binary CMD window.
  81. $self->shutdown_binary;
  82. It doesn't take any arguments, and it doesn't return anything.
  83. We do our best to call this when the C<$driver> option goes out of
  84. scope, but if that happens during global destruction, there's nothing
  85. we can do.
  86. =cut
  87. 1;