| 1 | package Selenium::PhantomJS; | ||
| 2 | 3 | $Selenium::PhantomJS::VERSION = '1.49'; | |
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | # ABSTRACT: Use GhostDriver without a Selenium server | ||
| 7 | 3 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use Selenium::CanStartBinary::FindBinary qw/coerce_simple_binary/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 3 | extends 'Selenium::Remote::Driver'; | |
| 10 | |||
| 11 | |||
| 12 | has '+browser_name' => ( | ||
| 13 | is => 'ro', | ||
| 14 | 2 | ▶ | default => sub { 'phantomjs' } |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 15 | 3 | ); | |
| 16 | |||
| 17 | |||
| 18 | has 'binary' => ( | ||
| 19 | is => 'lazy', | ||
| 20 | coerce => \&coerce_simple_binary, | ||
| 21 | 0 | ▶ | default => sub { 'phantomjs' }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 22 | 3 | predicate => 1 | |
| 23 | ); | ||
| 24 | |||
| 25 | |||
| 26 | has 'binary_port' => ( | ||
| 27 | is => 'lazy', | ||
| 28 | 0 | ▶ | default => sub { 8910 } |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 29 | 3 | ); | |
| 30 | |||
| 31 | has '_binary_args' => ( | ||
| 32 | is => 'lazy', | ||
| 33 | builder => sub { | ||
| 34 | 0 | ▶ | my ($self) = @_; |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 35 | |||
| 36 | 0 | return ' --webdriver=127.0.0.1:' . $self->port; | |
| 37 | } | ||
| 38 | 3 | ); | |
| 39 | |||
| 40 | 3 | with 'Selenium::CanStartBinary'; | |
| 41 | |||
| 42 | |||
| 43 | 3 | 1; | |
| 44 | |||
| 45 | __END__ | ||
| 46 | |||
| 47 | =pod | ||
| 48 | |||
| 49 | =encoding UTF-8 | ||
| 50 | |||
| 51 | =head1 NAME | ||
| 52 | |||
| 53 | Selenium::PhantomJS - Use GhostDriver without a Selenium server | ||
| 54 | |||
| 55 | =head1 VERSION | ||
| 56 | |||
| 57 | version 1.49 | ||
| 58 | |||
| 59 | =head1 SYNOPSIS | ||
| 60 | |||
| 61 | my $driver = Selenium::PhantomJS->new; | ||
| 62 | # when you're done | ||
| 63 | $driver->shutdown_binary; | ||
| 64 | |||
| 65 | =head1 DESCRIPTION | ||
| 66 | |||
| 67 | This class allows you to use PhantomJS via Ghostdriver without needing | ||
| 68 | the JRE or a selenium server running. When you refrain from passing | ||
| 69 | the C<remote_server_addr> and C<port> arguments, we will search for | ||
| 70 | the phantomjs executable binary in your $PATH. We'll try to start the | ||
| 71 | binary connect to it, shutting it down at the end of the test. | ||
| 72 | |||
| 73 | If the binary is not found, we'll fall back to the default | ||
| 74 | L<Selenium::Remote::Driver> behavior of assuming defaults of | ||
| 75 | 127.0.0.1:4444 after waiting a few seconds. | ||
| 76 | |||
| 77 | If you specify a remote server address, or a port, we'll assume you | ||
| 78 | know what you're doing and take no additional behavior. | ||
| 79 | |||
| 80 | If you're curious whether your Selenium::PhantomJS instance is using a | ||
| 81 | separate PhantomJS binary, or through the selenium server, you can check | ||
| 82 | the C<binary_mode> attr after instantiation. | ||
| 83 | |||
| 84 | my $driver = Selenium::PhantomJS->new; | ||
| 85 | print $driver->binary_mode; | ||
| 86 | |||
| 87 | N.B. - if you're using Windows and you installed C<phantomjs> via | ||
| 88 | C<npm install -g phantomjs>, there is a very high probability that we | ||
| 89 | will _not_ close down your phantomjs binary correctly after your | ||
| 90 | test. You will be able to tell if we leave around empty command | ||
| 91 | windows that you didn't start yourself. The easiest way to fix this is | ||
| 92 | to download PhantomJS manually from their | ||
| 93 | L<website|http://phantomjs.org/download.html> and put it in your | ||
| 94 | C<%PATH%>. If this is a blocking issue for you, let us know in | ||
| 95 | L<Github|https://github.com/gempesaw/Selenium-Remote-Driver>; thanks! | ||
| 96 | |||
| 97 | =head1 ATTRIBUTES | ||
| 98 | |||
| 99 | =head2 binary | ||
| 100 | |||
| 101 | Optional: specify the path to your binary. If you don't specify | ||
| 102 | anything, we'll try to find it on our own via L<File::Which/which>. | ||
| 103 | |||
| 104 | =head2 binary_port | ||
| 105 | |||
| 106 | Optional: specify the port that we should bind to. If you don't | ||
| 107 | specify anything, we'll default to the driver's default port. Since | ||
| 108 | there's no a priori guarantee that this will be an open port, this is | ||
| 109 | _not_ necessarily the port that we end up using - if the port here is | ||
| 110 | already bound, we'll search above it until we find an open one. | ||
| 111 | |||
| 112 | See L<Selenium::CanStartBinary/port> for more details, and | ||
| 113 | L<Selenium::Remote::Driver/port> after instantiation to see what the | ||
| 114 | actual port turned out to be. | ||
| 115 | |||
| 116 | =head2 custom_args | ||
| 117 | |||
| 118 | Optional: specify any additional command line arguments you'd like | ||
| 119 | invoked during the binary startup. See | ||
| 120 | L<Selenium::CanStartBinary/custom_args> for more information. | ||
| 121 | |||
| 122 | =head2 startup_timeout | ||
| 123 | |||
| 124 | Optional: specify how long to wait for the binary to start itself and | ||
| 125 | listen on its port. The default duration is arbitrarily 10 seconds. It | ||
| 126 | accepts an integer number of seconds to wait: the following will wait | ||
| 127 | up to 20 seconds: | ||
| 128 | |||
| 129 | Selenium::PhantomJS->new( startup_timeout => 20 ); | ||
| 130 | |||
| 131 | See L<Selenium::CanStartBinary/startup_timeout> for more information. | ||
| 132 | |||
| 133 | =head1 METHODS | ||
| 134 | |||
| 135 | =head2 shutdown_binary | ||
| 136 | |||
| 137 | Call this method instead of L<Selenium::Remote::Driver/quit> to ensure | ||
| 138 | that the binary executable is also closed, instead of simply closing | ||
| 139 | the browser itself. If the browser is still around, it will call | ||
| 140 | C<quit> for you. After that, it will try to shutdown the browser | ||
| 141 | binary by making a GET to /shutdown and on Windows, it will attempt to | ||
| 142 | do a C<taskkill> on the binary CMD window. | ||
| 143 | |||
| 144 | $self->shutdown_binary; | ||
| 145 | |||
| 146 | It doesn't take any arguments, and it doesn't return anything. | ||
| 147 | |||
| 148 | We do our best to call this when the C<$driver> option goes out of | ||
| 149 | scope, but if that happens during global destruction, there's nothing | ||
| 150 | we can do. | ||
| 151 | |||
| 152 | =for Pod::Coverage has_binary | ||
| 153 | |||
| 154 | =head1 AUTHORS | ||
| 155 | |||
| 156 | Current Maintainers: | ||
| 157 | |||
| 158 | =over 4 | ||
| 159 | |||
| 160 | =item * | ||
| 161 | |||
| 162 | George S. Baugh <george@troglodyne.net> | ||
| 163 | |||
| 164 | =back | ||
| 165 | |||
| 166 | Previous maintainers: | ||
| 167 | |||
| 168 | =over 4 | ||
| 169 | |||
| 170 | =item * | ||
| 171 | |||
| 172 | Daniel Gempesaw <gempesaw@gmail.com> | ||
| 173 | |||
| 174 | =item * | ||
| 175 | |||
| 176 | Emmanuel Peroumalnaïk <peroumalnaik.emmanuel@gmail.com> | ||
| 177 | |||
| 178 | =item * | ||
| 179 | |||
| 180 | Luke Closs <cpan@5thplane.com> | ||
| 181 | |||
| 182 | =item * | ||
| 183 | |||
| 184 | Mark Stosberg <mark@stosberg.com> | ||
| 185 | |||
| 186 | =back | ||
| 187 | |||
| 188 | Original authors: | ||
| 189 | |||
| 190 | =over 4 | ||
| 191 | |||
| 192 | =item * | ||
| 193 | |||
| 194 | Aditya Ivaturi <ivaturi@gmail.com> | ||
| 195 | |||
| 196 | =back | ||
| 197 | |||
| 198 | =head1 COPYRIGHT AND LICENSE | ||
| 199 | |||
| 200 | Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child | ||
| 201 | |||
| 202 | Copyright (c) 2014-2017 Daniel Gempesaw | ||
| 203 | |||
| 204 | Copyright (c) 2018-2021 George S. Baugh | ||
| 205 | |||
| 206 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 207 | you may not use this file except in compliance with the License. | ||
| 208 | You may obtain a copy of the License at | ||
| 209 | |||
| 210 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 211 | |||
| 212 | Unless required by applicable law or agreed to in writing, software | ||
| 213 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 214 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 215 | See the License for the specific language governing permissions and | ||
| 216 | limitations under the License. | ||
| 217 | |||
| 218 | =cut | ||