1
0

PhantomJS.pm 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. my $driver = Selenium::PhantomJS->new;
  23. print $driver->binary_mode;
  24. N.B. - if you're using Windows and you installed C<phantomjs> via
  25. C<npm install -g phantomjs>, there is a very high probability that we
  26. will _not_ close down your phantomjs binary correctly after your
  27. test. You will be able to tell if we leave around empty command
  28. windows that you didn't start yourself. The easiest way to fix this is
  29. to download PhantomJS manually from their
  30. L<website|http://phantomjs.org/download.html> and put it in your
  31. C<%PATH%>. If this is a blocking issue for you, let us know in
  32. L<Github|https://github.com/gempesaw/Selenium-Remote-Driver>; thanks!
  33. =cut
  34. has '+browser_name' => (
  35. is => 'ro',
  36. default => sub { 'phantomjs' }
  37. );
  38. =attr binary
  39. Optional: specify the path to your binary. If you don't specify
  40. anything, we'll try to find it on our own via L<File::Which/which>.
  41. =cut
  42. has 'binary' => (
  43. is => 'lazy',
  44. coerce => \&coerce_simple_binary,
  45. default => sub { 'phantomjs' },
  46. predicate => 1
  47. );
  48. =attr binary_port
  49. Optional: specify the port that we should bind to. If you don't
  50. specify anything, we'll default to the driver's default port. Since
  51. there's no a priori guarantee that this will be an open port, this is
  52. _not_ necessarily the port that we end up using - if the port here is
  53. already bound, we'll search above it until we find an open one.
  54. See L<Selenium::CanStartBinary/port> for more details, and
  55. L<Selenium::Remote::Driver/port> after instantiation to see what the
  56. actual port turned out to be.
  57. =cut
  58. has 'binary_port' => (
  59. is => 'lazy',
  60. default => sub { 8910 }
  61. );
  62. has '_binary_args' => (
  63. is => 'lazy',
  64. builder => sub {
  65. my ($self) = @_;
  66. return ' --webdriver=127.0.0.1:' . $self->port;
  67. }
  68. );
  69. with 'Selenium::CanStartBinary';
  70. 1;