File Coverage

lib/WWW/Mechanize/Firefox/Extended.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package WWW::Mechanize::Firefox::Extended;
2              
3 1     1   14213 use 5.006;
  1         3  
  1         31  
4 1     1   3 use strict;
  1         1  
  1         21  
5 1     1   4 use warnings FATAL => 'all';
  1         8  
  1         30  
6 1     1   316 use parent 'WWW::Mechanize::Firefox';
  1         245  
  1         4  
7              
8             use Time::HiRes qw/usleep/;
9             my $USLEEP_INTERVAL = 200000; # 200 milliseconds
10              
11             =head1 NAME
12              
13             WWW::Mechanize::Firefox::Extended - Adds handy functions to WWW::Mechanize::Firefox
14              
15             =head1 VERSION
16              
17             Version 0.01
18              
19             =cut
20              
21             our $VERSION = '0.01';
22              
23              
24             =head1 SYNOPSIS
25              
26             Module provides handy functions to check existence of selectors on a page and
27             to wait for selectors to come into existence.
28              
29             use WWW::Mechanize::Firefox::Extended;
30              
31             my $mech = WWW::Mechanize::Firefox::Extended->new();
32             $mech->get('https://www.example.com/');
33              
34             $mech->hasAll('#username', '#password', '#Image1');
35              
36             $mech->hasAny('.close-button', '.exit-button', '.out-button');
37              
38             $mech->waitAll(5, '#slow-loading-element')
39             or die "Expected element not found";
40              
41             $mech->waitAny(5, '#slow-loading-element', '#another-element')
42             or die "Expected element not found";
43              
44             =head1 SUBROUTINES/METHODS
45              
46             =head2 hasAll( $mech, @selectors )
47              
48             Returns true if all selectors exists. False otherwise.
49              
50             =cut
51             sub hasAll {
52             my $m = shift;
53             for (@_) {
54             return 0 if scalar ($m->selector($_,all=>1)) == 0; # short-circuit
55             }
56             return 1;
57             }
58              
59             =head2 hasAny( $mech, @selectors )
60              
61             Returns true if any selector exists. False if none exists.
62              
63             =cut
64             sub hasAny {
65             my $m = shift;
66             for (@_) {
67             return 1 if scalar ($m->selector($_,all=>1)) > 0; # short-circuit
68             }
69             return 0;
70             }
71              
72             =head2 waitAll( $mech, $max_wait_seconds, @selectors )
73              
74             Wait until all selectors are present or the wait times out.
75             Returns true if all selectors found or false if none found
76             within the timeout period.
77              
78             Uses Time::HiRes
79              
80             =cut
81             sub waitAll {
82             my ($m, $timeout, @selectors) = @_;
83             my ($slept, $max_sleep) = (0, $timeout * 1000000); # microseconds
84             while ($slept < $max_sleep) {
85             return 1 if (hasAll($m, @selectors));
86             usleep($USLEEP_INTERVAL); # sleep 200 milliseconds
87             $slept += $USLEEP_INTERVAL;
88             }
89             return 0;
90             }
91              
92             =head2 waitAny( $mech, $max_wait_seconds, @selectors )
93              
94             Wait until any selectors are present or the wait times out.
95             Returns true if any selectors are or false if none found
96             within the timeout period.
97              
98             Uses Time::HiRes
99              
100             =cut
101             sub waitAny {
102             my ($m, $timeout, @selectors) = @_;
103             my ($slept, $max_sleep) = (0, $timeout * 1000000); # microseconds
104             while ($slept < $max_sleep) {
105             return 1 if (hasAny($m, @selectors));
106             usleep($USLEEP_INTERVAL); # sleep 200 milliseconds
107             $slept += $USLEEP_INTERVAL;
108             }
109             return 0;
110             }
111              
112              
113              
114             =head1 AUTHOR
115              
116             Hoe-Kit Chew, C<< >>
117              
118             =head1 BUGS
119              
120             Please report any bugs or feature requests to C, or through
121             the web interface at L. I will be notified, and then you'll
122             automatically be notified of progress on your bug as I make changes.
123              
124             =head1 SUPPORT
125              
126             You can find documentation for this module with the perldoc command.
127              
128             perldoc WWW::Mechanize::Firefox::Extended
129              
130              
131             You can also look for information at:
132              
133             =over 4
134              
135             =item * RT: CPAN's request tracker (report bugs here)
136              
137             L
138              
139             =item * AnnoCPAN: Annotated CPAN documentation
140              
141             L
142              
143             =item * CPAN Ratings
144              
145             L
146              
147             =item * Search CPAN
148              
149             L
150              
151             =back
152              
153             =head1 REPOSITORY AND PULL REQUESTS
154              
155             This module is available on GitHub at
156             L.
157              
158             Pull requests welcomed.
159              
160             =head1 LICENSE AND COPYRIGHT
161              
162             Copyright 2015 Hoe-Kit Chew.
163              
164             This library is free software; you can redistribute it and/or modify it
165             under the same terms as Perl itself, either Perl version 5.10.0 or, at
166             your option, any later version of Perl 5 you may have available.
167              
168             =cut
169              
170             1; # End of WWW::Mechanize::Firefox::Extended