Firefox/Application.pm

filter: Statementstmt 30.2% 23 / 76 Branchbran 18.1% 4 / 22 Conditioncond 5.8% 1 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 26.3% 5 / 19 Podpod 92.8% 13 / 14 totaltotal 28.9% 46 / 159 CC 18 SCAR 24.1
CC
18
Cov
24%
new
26.5
activateTab
12.0
set_tab_content
12.0
CRAP 11.2
« HTTP/Cookies/MozRepl.pm ↑ Summary HTML/Display/MozRepl.pm »
1 package Firefox::Application;
2 82 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 82 ▶use MozRepl::RemoteObject ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 82 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 82 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 82 our $VERSION = '0.80';
9  
10 =head1 NAME
11  
12 Firefox::Application - inspect and automate the Firefox UI
13  
14 =head1 SYNOPSIS
15  
16 use Firefox::Application;
17 my $ff = Firefox::Application->new();
18  
19 This module will let you automate Firefox through the
20 Mozrepl plugin. You need to have installed
21 that plugin in your Firefox.
22  
23 For more examples see L<WWW::Mechanize::Firefox::Examples>.
24  
25 =head1 METHODS
26  
27 =head2 C<< Firefox::Application->new( %args ) >>
28  
29 use Firefox::Application;
30 my $ff = Firefox::Application->new();
31  
32 Creates a new instance and connects it to Firefox.
33  
34 Note that Firefox must have the C<mozrepl>
35 extension installed and enabled.
36  
37 The following options are recognized:
38  
39 =over 4
40  
41 =item *
42  
43 C<launch> - name of the program to launch if we can't connect to it on
44 the first try.
45  
46 =item *
47  
48 C<log> - array reference to log levels, passed through to L<MozRepl::RemoteObject>
49  
50 =item *
51  
52 C<bufsize> - L<Net::Telnet> buffer size, if the default of 1MB is not enough
53  
54 =item *
55  
56 C<repl> - a premade L<MozRepl::RemoteObject> instance or a connection string
57 suitable for initializing one.
58  
59 =item *
60  
61 C<use_queue> - whether to enable L<MozRepl::RemoteObject> command queueing
62  
63 =item *
64  
65 C<api> - class for the API wrapper
66  
67 You almost never want to use this parameter, as Firefox::Application
68 asks Firefox about its version.
69  
70 =back
71  
72 =cut
73  
74 78 ▶sub new {
Subroutine new: called CC=9 SCAR=32.8
Pod documented
75 78 my ($class, %args) = @_;
76 78 ▶ my $loglevel = delete $args{ log } || [qw[ error ]];
Condition50%1/2
delete $args{'log'} || ['error']
l!l
078
MC/DC0%0/1
delete $args{'log'} || ['error']
delete $args{'log'}
Truth table50%1/2
A
A= delete $args{'log'}
Aresult
11
00
77 78 ▶ my $use_queue = exists $args{ use_queue } ? delete $args{ use_queue } : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'use_queue'} ? : 0 78 78
78 78 my $api = delete $args{ api };
79 78 ▶ if (! ref $args{ repl }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args{"repl"}) 78 0 78
80 78 my @passthrough = qw(repl js_JSON launch);
81 78 ▶ my %options = map { exists $args{ $_ } ? ($_ => delete $args{ $_ }) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} ? : 1 233 234
82 @passthrough;
83 $args{ repl } = MozRepl::RemoteObject->install_bridge(
84 log => $loglevel,
85 use_queue => $use_queue,
86 bufsize => delete $args{ bufsize },
87 78 %options,
88 );
89 };
90
91 # Now install the proper API
92 0 ▶ if (! $api) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($api) 0 0 0
93 0 my $info = $args{ repl }->appinfo;
94 0 my $v = $info->{version};
95 0 ▶ $v =~ s!^(\d+.\d+).*!$1!
Branch: 1 branch
BranchTrueFalseTotal
$v =~ s/^(\d+.\d+).*/$1/ or $v = "3.0" 0 0 0
96 or $v = '3.0'; # Wild guess
97
98 0 ▶ if ($v >= 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v >= 4) { } 0 0 0
elsif ($v >= 3.6) { } 0 0 0
99 0 $api = 'Firefox::Application::API40';
100 } elsif ($v >= 3.6) {
101 0 $api = 'Firefox::Application::API36';
102 } else {
103 0 $api = 'Firefox::Application::API35';
104 };
105 };
106 0 MozRepl::RemoteObject::require_module( $api );
107
108 0 bless \%args, $api;
109 };
110  
111 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
112 0 my ($self) = @_;
113 0 local $@;
114 #warn "App cleaning up";
115 0 ▶ if (my $repl = delete $self->{ repl }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $repl = delete $self->{"repl"}) 0 0 0
116 0 %$self = (); # wipe out all references we keep
117 # but keep $repl alive until we can dispose of it
118 # as the last thing, now:
119 0 $repl = undef;
120 };
121 #warn "App cleaned up";
122 }
123  
124 =head2 C<< $ff->repl >>
125  
126 my ($value,$type) = $ff->repl->expr('2+2');
127  
128 Gets the L<MozRepl::RemoteObject> instance that is used.
129  
130 =cut
131  
132 0 ▶sub repl { $_[0]->{repl} };
Subroutine repl: not called CC=1 SCAR=6.9
Pod documented
133  
134 =head1 APPLICATION INFORMATION
135  
136 =cut
137  
138 =head2 C<< $ff->appinfo >>
139  
140 my $info = $ff->appinfo;
141 print 'ID : ', $info->{ID};
142 print 'name : ', $info->{name};
143 print 'version : ', $info->{version};
144  
145 Returns information about Firefox.
146  
147 =cut
148  
149 0 ▶sub appinfo {
Subroutine appinfo: not called CC=1 SCAR=6.9
Pod documented
150 0 $_[0]->repl->appinfo
151 };
152  
153 =head2 C<< $ff->addons( %args ) >>
154  
155 for my $addon ($ff->addons) {
156 print sprintf "Name: %s\n", $addon->{name};
157 print sprintf "Version: %s\n", $addon->{version};
158 print sprintf "GUID: %s\n", $addon->{id};
159 };
160  
161 Returns the list of installed addons as C<nsIUpdateItem>s (FF 3.5+)
162 or C<Addon>s (FF4+).
163 See L<https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIUpdateItem>
164 or L<https://developer.mozilla.org/en/Addons/Add-on_Manager/Addon>,
165 depending on your Firefox version.
166  
167 =head2 C<< $ff->locales( %args ) >>
168  
169 for my $locale ($ff->locales) {
170 print sprintf "Name: %s\n", $locale->{name};
171 print sprintf "Version: %s\n", $locale->{version};
172 print sprintf "GUID: %s\n", $locale->{id};
173 };
174  
175 Returns the list of installed locales as C<nsIUpdateItem>s.
176  
177 =head2 C<< $ff->themes( %args ) >>
178  
179 for my $theme ($ff->themes) {
180 print sprintf "Name: %s\n", $theme->{name};
181 print sprintf "Version: %s\n", $theme->{version};
182 print sprintf "GUID: %s\n", $theme->{id};
183 };
184  
185 Returns the list of installed locales as C<nsIUpdateItem>s.
186  
187 =head2 C<< $ff->updateitems( %args ) >>
188  
189 for my $item ($ff->updateitems) {
190 print sprintf "Name: %s\n", $item->{name};
191 print sprintf "Version: %s\n", $item->{version};
192 print sprintf "GUID: %s\n", $item->{id};
193 };
194  
195 Returns the list of updateable items. The type of item
196 can be restricted by the C<type> option.
197  
198 =over 4
199  
200 =item * C<type> - type of items to fetch
201  
202 C<ANY> - fetch any item
203  
204 C<ADDON> - fetch add-ons
205  
206 C<LOCALE> - fetch locales
207  
208 C<THEME> - fetch themes
209  
210 =back
211  
212 =cut
213  
214 0 ▶sub profileService {
Subroutine profileService: not called CC=1 SCAR=6.9
Pod undocumented
215 0 my ($self) = @_;
216
217 0 my $profileService = $self->repl->declare(<<'JS')->();
218 function () {
219 return Components.classes["@mozilla.org/toolkit/profile-service;1"]
220 .createInstance(Components.interfaces.nsIToolkitProfileService);
221 }
222 JS
223 }
224  
225 =head2 C<< $ff->current_profile >>
226  
227 print $ff->current_profile->{name}, "\n";
228  
229 Returns currently selected profile as C<nsIToolkitProfile>.
230  
231 See L<https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIToolkitProfile>.
232  
233 =cut
234  
235 0 ▶sub current_profile {
Subroutine current_profile: not called CC=1 SCAR=6.9
Pod documented
236 0 my ($self) = @_;
237 $self->profileService->{selectedProfile}
238 0 }
239  
240 =head2 C<< $ff->find_profile( $name ) >>
241  
242 print $ff->find_profile("")->{localDir}, "\n";
243  
244 Returns the profile given its name. Dies
245 if the profile cannot be found.
246  
247 =cut
248  
249 0 ▶sub find_profile {
Subroutine find_profile: not called CC=1 SCAR=6.9
Pod documented
250 0 my ($self,$name) = @_;
251 0 $self->profileService->getProfileByName($name);
252 }
253  
254 =head2 C<< $ff->profiles >>
255  
256 for ($ff->profiles) {
257 print "Profile: ", $_->{name}, "\n";
258 }
259  
260 Lists the installed profiles as C<nsIToolkitProfile>s.
261  
262 See L<https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIToolkitProfile>.
263  
264 =cut
265  
266 0 ▶sub profiles {
Subroutine profiles: not called CC=1 SCAR=6.9
Pod documented
267 0 my ($self) = @_;
268
269 0 my $getProfiles = $self->repl->declare(<<'JS', 'list');
270 function () {
271 var toolkitProfileService = Components.classes["@mozilla.org/toolkit/profile-service;1"]
272 .createInstance(Components.interfaces.nsIToolkitProfileService);
273 var res = new Array;
274 var i = toolkitProfileService.profiles;
275 while( i.hasMoreElements() ) {
276 res.push( i.getNext() );
277 };
278 return res
279 }
280 JS
281 0 $getProfiles->()
282 }
283  
284 =head1 UI METHODS
285  
286 =head2 C<< $ff->addTab( %options ) >>
287  
288 my $new = $ff->addTab();
289  
290 Creates a new tab and returns it.
291 The tab will be automatically closed upon program exit.
292  
293 If you want the tab to remain open, pass a false value to the the C< autoclose >
294 option.
295  
296 The recognized options are:
297  
298 =over 4
299  
300 =item *
301  
302 C<repl> - the repl to use. By default it will use C<< $ff->repl >>.
303  
304 =item *
305  
306 C<autoclose> - whether to automatically close the tab at program exit. Default is
307 to close the tab.
308  
309 =back
310  
311 =cut
312  
313 =head2 C<< $ff->selectedTab( %options ) >>
314  
315 my $curr = $ff->selectedTab();
316  
317 Sets the currently active tab.
318  
319 =cut
320  
321  
322 =head2 C<< $ff->closeTab( $tab [,$repl] ) >>
323  
324 $ff->closeTab( $tab );
325  
326 Close the given tab.
327  
328 =cut
329  
330 =head2 C<< $ff->openTabs( [$repl] ) >>
331  
332 my @tab_info = $ff->openTabs();
333 print "$_->{title}, $_->{location}, \n"
334 for @tab_info;
335  
336 Returns a list of information about the currently open tabs.
337  
338 =cut
339  
340  
341 =head2 C<< $ff->activateTab( [ $tab [, $repl ]] ) >>
342  
343 $ff->activateTab( $mytab ); # bring to foreground
344
345 Activates the tab passed in.
346  
347 =cut
348  
349 0 ▶sub activateTab {
Subroutine activateTab: not called CC=3 SCAR=24.8
Pod documented
350 0 my ($self, $tab, $repl ) = @_;
351 0 ▶ $repl ||= $self->repl;
Condition0%0/3
$repl ||= $self->repl
l!l&&r!l&&!r
000
MC/DC0%0/2
$repl ||= $self->repl
$repl $self->repl
Truth table0%0/3
A ||= B
A= $replB= $self->repl
ABresult
1X1
011
000
352 0 ▶ croak "No tab given"
Branch: 1 branch
BranchTrueFalseTotal
unless $tab 0 0 0
353 unless $tab;
354 0 $self->browser( $repl )->{tabContainer}->{selectedItem} = $tab;
355 };
356  
357 =head2 C<< $ff->browser( [$repl] ) >>
358  
359 my $b = $ff->browser();
360  
361 Returns the current Firefox browser instance, or opens a new browser
362 window if none is available, and returns its browser instance.
363  
364 If you need to call this as a class method, pass in the L<MozRepl::RemoteObject>
365 bridge to use.
366  
367 =cut
368  
369 0 ▶sub browser {
Subroutine browser: not called CC=2 SCAR=17.9
Pod documented
370 0 my ($self,$repl) = @_;
371 0 ▶ $repl ||= $self->repl;
Condition0%0/3
$repl ||= $self->repl
l!l&&r!l&&!r
000
MC/DC0%0/2
$repl ||= $self->repl
$repl $self->repl
Truth table0%0/3
A ||= B
A= $replB= $self->repl
ABresult
1X1
011
000
372 0 return $repl->declare(<<'JS')->();
373 function() {
374 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
375 .getService(Components.interfaces.nsIWindowMediator);
376 var win = wm.getMostRecentWindow('navigator:browser');
377 if (! win) {
378 // No browser windows are open, so open a new one.
379 win = window.open('about:blank');
380 };
381 return win.gBrowser
382 // return win.getBrowser()
383 }
384 JS
385 };
386  
387 =head2 C<< $ff->getMostRecentWindow >>
388  
389 Returns the most recently used Firefox window.
390  
391 =cut
392  
393 0 ▶sub getMostRecentWindow {
Subroutine getMostRecentWindow: not called CC=1 SCAR=6.9
Pod documented
394 0 my ($self) = @_;
395 0 my $get = $self->repl->declare(<<'JS');
396 function() {
397 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
398 .getService(Components.interfaces.nsIWindowMediator);
399 return wm.getMostRecentWindow('navigator:browser');
400 }
401 JS
402 0 return $get->()
403 };
404  
405 =head2 C<< $ff->set_tab_content( $tab, $html [,$repl] ) >>
406  
407 $ff->set_tab_content('<html><h1>Hello</h1></html>');
408  
409 This is a more general method that allows you to replace
410 the HTML of an arbitrary tab, and not only the tab that
411 WWW::Mechanize::Firefox is associated with.
412  
413 It has the flaw of not waiting until the tab has
414 loaded.
415  
416 =cut
417  
418 0 ▶sub set_tab_content {
Subroutine set_tab_content: not called CC=3 SCAR=24.8
Pod documented
419 0 my ($self, $tab, $content, $repl) = @_;
420 0 my $url = URI->new('data:');
421 0 $url->media_type("text/html");
422 0 $url->data($content);
423
424 0 ▶ $tab ||= $self->tab;
Condition0%0/3
$tab ||= $self->tab
l!l&&r!l&&!r
000
MC/DC0%0/2
$tab ||= $self->tab
$tab $self->tab
Truth table0%0/3
A ||= B
A= $tabB= $self->tab
ABresult
1X1
011
000
425 0 ▶ $repl ||= $self->repl;
Condition0%0/3
$repl ||= $self->repl
l!l&&r!l&&!r
000
MC/DC0%0/2
$repl ||= $self->repl
$repl $self->repl
Truth table0%0/3
A ||= B
A= $replB= $self->repl
ABresult
1X1
011
000
426
427 0 $tab->{linkedBrowser}->loadURI("".$url);
428 };
429  
430 =head2 C<< $ff->quit( %options ) >>
431  
432 $ff->quit( restart => 1 ); # restart
433 $ff->quit(); # quit
434  
435 Quits or restarts the application
436  
437 =cut
438  
439 0 ▶sub quit {
Subroutine quit: not called CC=3 SCAR=24.8
Pod documented
440 0 my ($self, %options) = @_;
441 0 ▶ my $repl = $options{ repl } || $self->repl;
Condition0%0/3
$options{'repl'} || $self->repl
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'repl'} || $self->repl
$options{'repl'} $self->repl
Truth table0%0/3
A || B
A= $options{'repl'}B= $self->repl
ABresult
1X1
011
000
442 my $flags = $options{ restart }
443 0 ▶ ? 0x13 # force-quit
Branch: 1 branch
BranchTrueFalseTotal
$options{'restart'} ? : 0 0 0
444 : 0x03 # force-quit + restart
445 ;
446
447 0 my $get_startup = $repl->declare(<<'JS');
448 function() {
449 return Components.classes["@mozilla.org/toolkit/app-startup;1"]
450 .getService(Components.interfaces.nsIAppStartup);
451 }
452 JS
453 0 my $s = $get_startup->();
454 0 $s->quit($flags);
455 };
456  
457 =head2 C<< $ff->bool_ff_to_perl $val >>
458  
459 Normalizes the (checkbox) truth value C<$val> to 1 or 0.
460  
461 Different Firefox versions return C<true> or C<false>
462 as the checkbox values. This function converts
463 a Firefox checkbox value to 1 or 0.
464  
465 =cut
466  
467 # FF 31 has 1,0
468 0 ▶sub bool_ff_to_perl {
Subroutine bool_ff_to_perl: not called CC=1 SCAR=6.9
Pod documented
469 0 my( $self, $value )= @_;
470 0 $value
471 }
472  
473 =head2 C<< $ff->bool_perl_to_ff $val >>
474  
475 Normalizes the truth value C<$val> to 1 or 0.
476  
477 Different Firefox versions want C<true> or C<false>
478 as the checkbox values. This function converts
479 a Perl truth value to 1 or 0 respectively C<true> or C<false>,
480 depending on what Firefox wants.
481  
482 =cut
483  
484 # FF 31 has 1,0
485 0 ▶sub bool_perl_to_ff {
Subroutine bool_perl_to_ff: not called CC=2 SCAR=17.9
Pod documented
486 0 my( $self, $value )= @_;
487 0 ▶ $value ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
488 }
489  
490 =head1 TODO
491  
492 =over 4
493  
494 =item *
495  
496 Consider how to roll L<http://kb.mozillazine.org/Command_line_arguments>
497 into this module for convenient / versatile launching of Firefox
498  
499 =back
500  
501 =head1 AUTHOR
502  
503 Max Maischein C<corion@cpan.org>
504  
505 =head1 COPYRIGHT (c)
506  
507 Copyright 2009-2013 by Max Maischein C<corion@cpan.org>.
508  
509 =head1 LICENSE
510  
511 This module is released under the same terms as Perl itself.
512  
513 =cut
514  
515 82 1;
« HTTP/Cookies/MozRepl.pm ↑ Summary HTML/Display/MozRepl.pm »