Selenium/Firefox.pm

filter: Statementstmt 55.1% 32 / 58 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 7 / 14 Podpod 100% 2 / 2 totaltotal 47.6% 41 / 86 SCAR 27.0
CC
7
Cov
46%
set_context
20.0
_build__binary_args
6.0
__ANON__
6.0
CRAP 14.8
« Test/Selenium/Remote/Role/DoesTesting.pm ↑ Summary Selenium/Remote/Commands.pm »
1 package Selenium::Firefox;
2 3 $Selenium::Firefox::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 FirefoxDriver without a Selenium server
7 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Selenium::Firefox::Binary qw/firefox_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 use Selenium::CanStartBinary::FindBinary
11 3 ▶ qw/coerce_simple_binary coerce_firefox_binary/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 extends 'Selenium::Remote::Driver';
13  
14  
15 has '+browser_name' => (
16 is => 'ro',
17 2 ▶ default => sub { 'firefox' }
Subroutine __ANON__: called CC=1 SCAR=0.0
18 3 );
19  
20  
21 has 'binary' => (
22 is => 'lazy',
23 coerce => \&coerce_simple_binary,
24 0 ▶ default => sub { 'geckodriver' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 3 predicate => 1
26 );
27  
28  
29 has 'binary_port' => (
30 is => 'lazy',
31 0 ▶ default => sub { 9090 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 3 );
33  
34  
35 has '_binary_args' => (
36 is => 'lazy',
37 builder => sub {
38 0 ▶ my ($self) = @_;
Subroutine _build__binary_args: not called CC=2 SCAR=17.9
39  
40 0 ▶ if ( $self->marionette_enabled ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->marionette_enabled) { } 0 0 0
41 0 my $args =
42 ' --port '
43 . $self->port
44 . ' --marionette-port '
45 . $self->marionette_port
46 . ' --binary "'
47 . $self->firefox_binary . '"';
48  
49 0 return $args;
50 }
51 else {
52 0 return ' -no-remote';
53 }
54 }
55 3 );
56  
57 has '+wd_context_prefix' => (
58 is => 'ro',
59 default => sub {
60 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
61  
62 0 ▶ if ( $self->marionette_enabled ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->marionette_enabled) { } 0 0 0
63 0 return '';
64 }
65 else {
66 0 return '/hub';
67 }
68  
69 }
70 3 );
71  
72  
73 has 'marionette_binary_port' => (
74 is => 'lazy',
75 0 ▶ default => sub { 2828 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 3 );
77  
78  
79 3 has 'marionette_enabled' => (
80 is => 'lazy',
81 default => 1
82 );
83  
84  
85 3 has 'firefox_binary' => (
86 is => 'lazy',
87 coerce => \&coerce_firefox_binary,
88 predicate => 1,
89 builder => 'firefox_path'
90 );
91  
92 3 has '_execute_script_suffix' => (
93 is => 'lazy',
94 default => 'Gecko'
95 );
96  
97  
98 sub get_context {
99 0 ▶ my $self = shift;
Subroutine get_context: not called CC=2 SCAR=17.9
Pod documented
100  
101 0 ▶ if ( $self->_is_old_ff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_old_ff) 0 0 0
102 0 return 0;
103 }
104 0 my $res = { 'command' => 'getContext' };
105 0 return $self->_execute_command($res);
106 }
107  
108  
109 sub set_context {
110 0 ▶ my ( $self, $context ) = @_;
Subroutine set_context: not called CC=4 SCAR=30.0
Pod documented
111  
112 0 ▶ if ( $self->_is_old_ff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_old_ff) 0 0 0
113 0 return 0;
114 }
115 0 ▶ if ( not defined $context ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $context) 0 0 0
116 0 croak "Expecting context";
117 }
118 0 ▶ if ( $context !~ m/chrome|content/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context =~ /chrome|content/i) 0 0 0
119 0 croak "Expecting context value: chrome or content";
120 }
121 0 my $res = { 'command' => 'setContext' };
122 0 return $self->_execute_command( $res, { context => $context } );
123 }
124  
125 3 with 'Selenium::CanStartBinary';
126  
127  
128 3 1;
129  
130 __END__
131  
132 =pod
133  
134 =encoding UTF-8
135  
136 =head1 NAME
137  
138 Selenium::Firefox - Use FirefoxDriver without a Selenium server
139  
140 =head1 VERSION
141  
142 version 1.49
143  
144 =head1 SYNOPSIS
145  
146 # These two are the same, and will only work with Firefox 48+
147 my $driver = Selenium::Firefox->new;
148 $driver = Selenium::Firefox->new( marionette_enabled => 1 );
149  
150 #Do stuff...
151  
152 $driver->shutdown_binary;
153  
154 # For Firefox 47 and older, disable marionette:
155 $driver = Selenium::Firefox->new( marionette_enabled => 0 );
156 $driver->shutdown_binary;
157  
158 =head1 DESCRIPTION
159  
160 B<Breaking Changes:> There are breaking changes in v1.0+ of this
161 module if you're using it to start FF47; please see L</"BREAKING
162 CHANGES">. You can ignore this if you're using v1.0+ of this module to
163 start FF48.
164  
165 This class allows you to use the FirefoxDriver without needing the JRE
166 or a selenium server running. Unlike starting up an instance of
167 S::R::D, do not pass the C<remote_server_addr> and C<port> arguments,
168 and we will search for the Firefox executable in your C<$PATH>. We'll
169 try to start the binary, connect to it, and shut it down at the end of
170 the test.
171  
172 If the Firefox application is not found in the expected places, we'll
173 fall back to the default L<Selenium::Remote::Driver> behavior of
174 assuming defaults of 127.0.0.1:4444 after waiting a few seconds.
175  
176 If you specify a remote server address, or a port, our assumption is
177 that you are doing standard S::R::D behavior and we will not attempt
178 any binary startup.
179  
180 If you're curious whether your Selenium::Firefox instance is using a
181 separate Firefox binary, or through the selenium server, you can check
182 the value of the C<binary_mode> attr after instantiation.
183  
184 =head1 ATTRIBUTES
185  
186 =head2 binary
187  
188 Optional: specify the path to the C<geckodriver> binary - this is NOT
189 the path to the Firefox browser. To specify the path to your Firefox
190 browser binary, see the L</firefox_binary> attr.
191  
192 For Firefox 48 and greater, this is the path to your C<geckodriver>
193 executable. If you don't specify anything, we'll search for
194 C<geckodriver> in your C<$PATH>.
195  
196 For Firefox 47 and older, this attribute does not apply, because the
197 older FF browsers do not use the separate driver binary startup.
198  
199 =head2 binary_port
200  
201 Optional: specify the port that we should bind to. If you don't
202 specify anything, we'll default to the driver's default port. Since
203 there's no a priori guarantee that this will be an open port, this is
204 _not_ necessarily the port that we end up using - if the port here is
205 already bound, we'll search above it until we find an open one.
206  
207 See L<Selenium::CanStartBinary/port> for more details, and
208 L<Selenium::Remote::Driver/port> after instantiation to see what the
209 actual port turned out to be.
210  
211 =head2 firefox_profile
212  
213 Optional: Pass in an instance of L<Selenium::Firefox::Profile>
214 pre-configured as you please. The preferences you specify will be
215 merged with the ones necessary for setting up webdriver, and as a
216 result some options may be overwritten or ignored.
217  
218 my $profile = Selenium::Firefox::Profile->new;
219 my $firefox = Selenium::Firefox->new(
220 firefox_profile => $profile
221 );
222  
223 =head2 marionette_binary_port
224  
225 Optional: specify the port that we should bind marionette to. If you don't
226 specify anything, we'll default to the marionette's default port. Since
227 there's no a priori guarantee that this will be an open port, this is
228 _not_ necessarily the port that we end up using - if the port here is
229 already bound, we'll search above it until we find an open one.
230  
231 Selenium::Firefox->new(
232 marionette_enabled => 1,
233 marionette_binary_port => 12345,
234 );
235  
236 Attempting to specify a C<marionette_binary_port> in conjunction with
237 setting C<marionette_enabled> does not make sense and will most likely
238 not do anything useful.
239  
240 =head2 marionette_enabled
241  
242 Optional: specify whether
243 L<marionette|https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette>
244 should be enabled or not. By default, marionette is enabled, which
245 assumes you are running with Firefox 48 or newer. To use this module to
246 start Firefox 47 or older, you must pass C<< marionette_enabled => 0 >>.
247  
248 my $ff48 = Selenium::Firefox->new( marionette_enabled => 1 ); # defaults to 1
249 my $ff47 = Selenium::Firefox->new( marionette_enabled => 0 );
250  
251 =head2 firefox_binary
252  
253 Optional: specify the path to the Firefox browser executable. Although
254 we will attempt to locate this in your C<$PATH>, you may specify it
255 explicitly here. Note that path here must point to a file that exists
256 and is executable, or we will croak.
257  
258 For Firefox 48 and newer, this will be passed to C<geckodriver> such
259 that it will attempt to start up the Firefox at the specified path. If
260 you do not specify anything, we will look for the Firefox browser on
261 our own in the normal places, but if the browser cannot be found,
262 we'll probably C<die> during instantiation.
263  
264 For Firefox 47 and older, this browser path should be the file that we
265 directly start up.
266  
267 =head2 custom_args
268  
269 Optional: specify any additional command line arguments you'd like
270 invoked during the binary startup. See
271 L<Selenium::CanStartBinary/custom_args> for more information.
272  
273 For Firefox 48 and newer, these arguments will be passed to
274 geckodriver during start up.
275  
276 For Firefox 47 and older, these arguments will be passed to the
277 Firefox browser during start up.
278  
279 =head2 startup_timeout
280  
281 Optional: specify how long to wait for the binary to start itself and
282 listen on its port. The default duration is arbitrarily 10 seconds. It
283 accepts an integer number of seconds to wait: the following will wait
284 up to 20 seconds:
285  
286 Selenium::Firefox->new( startup_timeout => 20 );
287  
288 See L<Selenium::CanStartBinary/startup_timeout> for more information.
289  
290 =head2 fixed_ports
291  
292 Optional: Throw instead of searching for additional ports; see
293 L<Selenium::CanStartBinary/fixed_ports> for more info.
294  
295 =head1 METHODS
296  
297 =head2 shutdown_binary
298  
299 Call this method instead of L<Selenium::Remote::Driver/quit> to ensure
300 that the binary executable is also closed, instead of simply closing
301 the browser itself. If the browser is still around, it will call
302 C<quit> for you. After that, it will try to shutdown the browser
303 binary by making a GET to /shutdown and on Windows, it will attempt to
304 do a C<taskkill> on the binary CMD window.
305  
306 $self->shutdown_binary;
307  
308 It doesn't take any arguments, and it doesn't return anything.
309  
310 We do our best to call this when the C<$driver> option goes out of
311 scope, but if that happens during global destruction, there's nothing
312 we can do.
313  
314 =for Pod::Coverage has_binary
315  
316 =for Pod::Coverage has_firefox_binary
317  
318 =head1 BREAKING CHANGES
319  
320 In version v1.0+ and newer, the default behavior is to enable
321 marionette & geckodriver mode. This means that an existing script that
322 works with v0.2701 and Firefox v47 will require modification if you
323 upgrade Selenium::Firefox to v1.0+. That is,
324  
325 # v0.2701 of Selenium::Firefox works with FF47 like such; this will not
326 # work for FF47 after upgrade:
327 my $fx47_old = Selenium::Firefox->new;
328 ...
329 $fx47_old->shutdown_binary;
330  
331 # v1.0 of Selenium::Firefox works with FF47 like this
332 my $fx47_new = Selenium::Firefox->new( marionette_enabled => 0);
333 ...
334 $fx47_new->shutdown_binary;
335  
336 We default to assuming FF48 and geckodriver mode because all
337 forthcoming versions of the Firefox browser will be using the
338 geckodriver architecture, and also because that's consistent with the
339 rest of the driver setups, which all have separate driver binaries
340 apart from the browser itself. This means that:
341  
342 # v0.2701 of Selenium::Firefox cannot start up FF48 at all
343  
344 # v1.0+ of Selenium::Firefox works with FF48+ like this:
345 my $fx48 = Selenium::Firefox->new;
346  
347 As with the other drivers, Selenium::Firefox in marionette/geckodriver
348 mode requires a C<geckodriver> executable in the path or provided
349 during startup, and it will also attempt to find the path to your
350 Firefox browser. During testing, we found that it was necessary for us
351 to pass the Firefox browser file path to the C<geckodriver> executable
352 during start up, or else C<geckodriver> would have trouble finding
353 Firefox.
354  
355 =head2 get_context
356  
357 Description:
358 Firefox extension: Retrieve browser's scope (chrome or content).
359 Chrome is a privileged scope where you can access things like the
360 Firefox UI itself. Content scope is where things like webpages live.
361  
362 Output:
363 STRING - context {CHROME|CONTENT}
364  
365 Usage:
366 print $firefox_driver->get_context();
367  
368 =head2 set_context
369  
370 Description:
371 Firefox extension: Set browser's scope (chrome or content).
372 Chrome is a privileged scope where you can access things like the
373 Firefox UI itself. Content scope is where things like webpages live.
374  
375 Input:
376 Required:
377 <STRING> - context {CHROME|CONTENT}
378  
379 Usage:
380 $firefox_driver->set_context( $context );
381  
382 Output:
383 BOOLEAN - success or failure
384  
385 =head1 AUTHORS
386  
387 Current Maintainers:
388  
389 =over 4
390  
391 =item *
392  
393 George S. Baugh <george@troglodyne.net>
394  
395 =back
396  
397 Previous maintainers:
398  
399 =over 4
400  
401 =item *
402  
403 Daniel Gempesaw <gempesaw@gmail.com>
404  
405 =item *
406  
407 Emmanuel Peroumalnaïk <peroumalnaik.emmanuel@gmail.com>
408  
409 =item *
410  
411 Luke Closs <cpan@5thplane.com>
412  
413 =item *
414  
415 Mark Stosberg <mark@stosberg.com>
416  
417 =back
418  
419 Original authors:
420  
421 =over 4
422  
423 =item *
424  
425 Aditya Ivaturi <ivaturi@gmail.com>
426  
427 =back
428  
429 =head1 COPYRIGHT AND LICENSE
430  
431 Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child
432  
433 Copyright (c) 2014-2017 Daniel Gempesaw
434  
435 Copyright (c) 2018-2021 George S. Baugh
436  
437 Licensed under the Apache License, Version 2.0 (the "License");
438 you may not use this file except in compliance with the License.
439 You may obtain a copy of the License at
440  
441 http://www.apache.org/licenses/LICENSE-2.0
442  
443 Unless required by applicable law or agreed to in writing, software
444 distributed under the License is distributed on an "AS IS" BASIS,
445 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
446 See the License for the specific language governing permissions and
447 limitations under the License.
448  
449 =cut
« Test/Selenium/Remote/Role/DoesTesting.pm ↑ Summary Selenium/Remote/Commands.pm »