Chrome.pm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. =head1 DESCRIPTION
  9. This class allows you to use the ChromeDriver without needing the JRE
  10. or a selenium server running. When you refrain from passing the
  11. C<remote_server_addr> and C<port> arguments, we will search for the
  12. chromedriver executable binary in your $PATH. We'll try to start the
  13. binary connect to it, shutting it down at the end of the test.
  14. If the chromedriver binary is not found, we'll fall back to the
  15. default L<Selenium::Remote::Driver> behavior of assuming defaults of
  16. 127.0.0.1:4444 after waiting a few seconds.
  17. If you specify a remote server address, or a port, we'll assume you
  18. know what you're doing and take no additional behavior.
  19. If you're curious whether your Selenium::Chrome instance is using a
  20. separate ChromeDriver binary, or through the selenium server, you can
  21. check the C<binary_mode> attr after instantiation.
  22. =cut
  23. has '+browser_name' => (
  24. is => 'ro',
  25. default => sub { 'chrome' }
  26. );
  27. =attr binary
  28. Optional: specify the path to your binary. If you don't specify
  29. anything, we'll try to find it on our own via L<File::Which/which>.
  30. =cut
  31. has 'binary' => (
  32. is => 'lazy',
  33. coerce => \&coerce_simple_binary,
  34. default => sub { 'chromedriver' },
  35. predicate => 1
  36. );
  37. =attr binary_port
  38. Optional: specify the port that we should bind to. If you don't
  39. specify anything, we'll default to the driver's default port. Since
  40. there's no a priori guarantee that this will be an open port, this is
  41. _not_ necessarily the port that we end up using - if the port here is
  42. already bound, we'll search above it until we find an open one.
  43. See L<Selenium::CanStartBinary/port> for more details, and
  44. L<Selenium::Remote::Driver/port> after instantiation to see what the
  45. actual port turned out to be.
  46. =cut
  47. has 'binary_port' => (
  48. is => 'lazy',
  49. default => sub { 9515 }
  50. );
  51. has '_binary_args' => (
  52. is => 'lazy',
  53. builder => sub {
  54. my ($self) = @_;
  55. my $context = $self->wd_context_prefix;
  56. $context =~ s{^/}{};
  57. return ' --port=' . $self->port . ' --url-base=' . $context . ' ';
  58. }
  59. );
  60. with 'Selenium::CanStartBinary';
  61. =attr startup_timeout
  62. Optional: specify how long to wait for the binary to start itself and
  63. listen on its port. The default duration is arbitrarily 10 seconds. It
  64. accepts an integer number of seconds to wait: the following will wait
  65. up to 20 seconds:
  66. Selenium::Chrome->new( startup_timeout => 20 );
  67. See L<Selenium::CanStartBinary/startup_timeout> for more information.
  68. =cut
  69. 1;