PhantomJS.pm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package Selenium::PhantomJS;
  2. # ABSTRACT: Use GhostDriver 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::PhantomJS->new;
  8. =head1 DESCRIPTION
  9. This class allows you to use PhantomJS via Ghostdriver without needing
  10. the JRE or a selenium server running. When you refrain from passing
  11. the C<remote_server_addr> and C<port> arguments, we will search for
  12. the phantomjs 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 binary is not found, we'll fall back to the default
  15. 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::PhantomJS instance is using a
  20. separate PhantomJS binary, or through the selenium server, you can check
  21. the C<binary_mode> attr after instantiation.
  22. N.B. If you're on Windows, we're probably going to fail at closing the
  23. command window that runs the PhantomJS binary. Feel free to bug us
  24. over at L<Github|https://github.com/gempesaw/Selenium-Remote-Driver>
  25. and/or tell us how to fix it if it's a blocking issue for you. Thanks!
  26. =cut
  27. has '+browser_name' => (
  28. is => 'ro',
  29. default => sub { 'phantomjs' }
  30. );
  31. =attr binary
  32. Optional: specify the path to your binary. If you don't specify
  33. anything, we'll try to find it on our own via L<File::Which/which>.
  34. =cut
  35. has 'binary' => (
  36. is => 'lazy',
  37. coerce => \&coerce_simple_binary,
  38. default => sub { 'phantomjs' },
  39. predicate => 1
  40. );
  41. =attr binary_port
  42. Optional: specify the port that we should bind to. If you don't
  43. specify anything, we'll default to the driver's default port. Since
  44. there's no a priori guarantee that this will be an open port, this is
  45. _not_ necessarily the port that we end up using - if the port here is
  46. already bound, we'll search above it until we find an open one.
  47. See L<Selenium::CanStartBinary/port> for more details, and
  48. L<Selenium::Remote::Driver/port> after instantiation to see what the
  49. actual port turned out to be.
  50. =cut
  51. has 'binary_port' => (
  52. is => 'lazy',
  53. default => sub { 8910 }
  54. );
  55. has '_binary_args' => (
  56. is => 'lazy',
  57. builder => sub {
  58. my ($self) = @_;
  59. return ' --webdriver=127.0.0.1:' . $self->port;
  60. }
  61. );
  62. with 'Selenium::CanStartBinary';
  63. 1;