1
0

PhantomJS.pm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. # when you're done
  9. $driver->shutdown_binary;
  10. =head1 DESCRIPTION
  11. This class allows you to use PhantomJS via Ghostdriver without needing
  12. the JRE or a selenium server running. When you refrain from passing
  13. the C<remote_server_addr> and C<port> arguments, we will search for
  14. the phantomjs executable binary in your $PATH. We'll try to start the
  15. binary connect to it, shutting it down at the end of the test.
  16. If the binary is not found, we'll fall back to the default
  17. L<Selenium::Remote::Driver> behavior of assuming defaults of
  18. 127.0.0.1:4444 after waiting a few seconds.
  19. If you specify a remote server address, or a port, we'll assume you
  20. know what you're doing and take no additional behavior.
  21. If you're curious whether your Selenium::PhantomJS instance is using a
  22. separate PhantomJS binary, or through the selenium server, you can check
  23. the C<binary_mode> attr after instantiation.
  24. my $driver = Selenium::PhantomJS->new;
  25. print $driver->binary_mode;
  26. N.B. - if you're using Windows and you installed C<phantomjs> via
  27. C<npm install -g phantomjs>, there is a very high probability that we
  28. will _not_ close down your phantomjs binary correctly after your
  29. test. You will be able to tell if we leave around empty command
  30. windows that you didn't start yourself. The easiest way to fix this is
  31. to download PhantomJS manually from their
  32. L<website|http://phantomjs.org/download.html> and put it in your
  33. C<%PATH%>. If this is a blocking issue for you, let us know in
  34. L<Github|https://github.com/gempesaw/Selenium-Remote-Driver>; thanks!
  35. =cut
  36. has '+browser_name' => (
  37. is => 'ro',
  38. default => sub { 'phantomjs' }
  39. );
  40. =attr binary
  41. Optional: specify the path to your binary. If you don't specify
  42. anything, we'll try to find it on our own via L<File::Which/which>.
  43. =cut
  44. has 'binary' => (
  45. is => 'lazy',
  46. coerce => \&coerce_simple_binary,
  47. default => sub { 'phantomjs' },
  48. predicate => 1
  49. );
  50. =attr binary_port
  51. Optional: specify the port that we should bind to. If you don't
  52. specify anything, we'll default to the driver's default port. Since
  53. there's no a priori guarantee that this will be an open port, this is
  54. _not_ necessarily the port that we end up using - if the port here is
  55. already bound, we'll search above it until we find an open one.
  56. See L<Selenium::CanStartBinary/port> for more details, and
  57. L<Selenium::Remote::Driver/port> after instantiation to see what the
  58. actual port turned out to be.
  59. =cut
  60. has 'binary_port' => (
  61. is => 'lazy',
  62. default => sub { 8910 }
  63. );
  64. has '_binary_args' => (
  65. is => 'lazy',
  66. builder => sub {
  67. my ($self) = @_;
  68. return ' --webdriver=127.0.0.1:' . $self->port;
  69. }
  70. );
  71. with 'Selenium::CanStartBinary';
  72. =attr custom_args
  73. Optional: specify any additional command line arguments you'd like
  74. invoked during the binary startup. See
  75. L<Selenium::CanStartBinary/custom_args> for more information.
  76. =attr startup_timeout
  77. Optional: specify how long to wait for the binary to start itself and
  78. listen on its port. The default duration is arbitrarily 10 seconds. It
  79. accepts an integer number of seconds to wait: the following will wait
  80. up to 20 seconds:
  81. Selenium::PhantomJS->new( startup_timeout => 20 );
  82. See L<Selenium::CanStartBinary/startup_timeout> for more information.
  83. =method shutdown_binary
  84. Call this method instead of L<Selenium::Remote::Driver/quit> to ensure
  85. that the binary executable is also closed, instead of simply closing
  86. the browser itself. If the browser is still around, it will call
  87. C<quit> for you. After that, it will try to shutdown the browser
  88. binary by making a GET to /shutdown and on Windows, it will attempt to
  89. do a C<taskkill> on the binary CMD window.
  90. $self->shutdown_binary;
  91. It doesn't take any arguments, and it doesn't return anything.
  92. We do our best to call this when the C<$driver> option goes out of
  93. scope, but if that happens during global destruction, there's nothing
  94. we can do.
  95. =cut
  96. 1;