Chrome.pm 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package Selenium::Chrome;
  2. # ABSTRACT: A convenience package for creating a Chrome instance
  3. use Moo;
  4. extends 'Selenium::Remote::Driver';
  5. =head1 SYNOPSIS
  6. my $driver = Selenium::Chrome->new;
  7. =cut
  8. has '+browser_name' => (
  9. is => 'ro',
  10. default => sub { 'chrome' }
  11. );
  12. sub _find_executable {
  13. my ($binary) = @_;
  14. my $executable = which($binary);
  15. if (not defined $executable) {
  16. warn q(
  17. Unable to find the chromedriver binary in your $PATH. Please download the server from http://chromedriver.storage.googleapis.com/index.html and place it somewhere on your $PATH. More info is available at http://code.google.com/p/selenium/wiki/ChromeDriver.
  18. We'll try falling back to standard Remote Driver mode via the webdriver.chrome.driver property...
  19. );
  20. }
  21. return $executable;
  22. }
  23. sub _construct_command {
  24. my ($executable, $port) = @_;
  25. my %args = (
  26. 'base-url' => 'wd/hub',
  27. 'port' => $port
  28. );
  29. my @args = map { '--' . $_ . '=' . $args{$_} } keys %args;
  30. return join(' ', ($executable, @args, '> /dev/null 2>&1 &') );
  31. }
  32. sub _find_open_port_above {
  33. my ($port) = @_;
  34. my $free_port = wait_until {
  35. if (_probe_port($port)) {
  36. $port++;
  37. return 0;
  38. }
  39. else {
  40. return $port;
  41. }
  42. };
  43. return $free_port;
  44. }
  45. sub _probe_port {
  46. my ($port) = @_;
  47. return IO::Socket::INET->new(
  48. PeerAddr => $default_binary_server,
  49. PeerPort => $port,
  50. Timeout => 3
  51. );
  52. }
  53. 1;