1
0

PhantomJS.pm 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package Selenium::PhantomJS;
  2. # ABSTRACT: A convenience package for creating a PhantomJS 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::PhantomJS->new;
  9. =cut
  10. use constant PHANTOMJS_PORT => 8910;
  11. has '+browser_name' => (
  12. is => 'ro',
  13. default => sub { 'phantomjs' }
  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('phantomjs', PHANTOMJS_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;