PhantomJS.pm 4.2 KB

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