Chrome.pm 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package Selenium::Chrome;
  2. # ABSTRACT: A convenience package for creating a Chrome instance
  3. use Selenium::Binary qw/start_binary_on_port/;
  4. use Moo;
  5. use namespace::clean;
  6. extends 'Selenium::Remote::Driver';
  7. =head1 SYNOPSIS
  8. my $driver = Selenium::Chrome->new;
  9. =cut
  10. use constant CHROMEDRIVER_PORT => 9515;
  11. has '+browser_name' => (
  12. is => 'ro',
  13. default => sub { 'chrome' }
  14. );
  15. # By shadowing the parent's port function, we can set the port in
  16. # _build_binary_mode's builder
  17. has '+port' => (
  18. is => 'lazy'
  19. );
  20. has 'binary_mode' => (
  21. is => 'ro',
  22. init_arg => undef,
  23. builder => 1
  24. );
  25. sub _build_binary_mode {
  26. my ($self) = @_;
  27. if (! $self->has_remote_server_addr && ! $self->has_port) {
  28. try {
  29. my $port = start_binary_on_port('chromedriver', CHROMEDRIVER_PORT);
  30. $self->port($port);
  31. return 1;
  32. }
  33. catch {
  34. warn $_;
  35. return 0;
  36. }
  37. }
  38. else {
  39. return 0;
  40. }
  41. }
  42. sub DEMOLISH {
  43. my ($self) = @_;
  44. if ($self->binary_mode) {
  45. my $port = $self->port;
  46. my $ua = LWP::UserAgent->new;
  47. $ua->get('127.0.0.1:' . $port . '/wd/hub/shutdown');
  48. }
  49. }
  50. 1;