File Coverage

lib/Brownie/Driver/Mechanize.pm
Criterion Covered Total %
statement 67 68 98.5
branch 8 12 66.6
condition 3 6 50.0
subroutine 22 22 100.0
pod 10 10 100.0
total 110 118 93.2


line stmt bran cond sub pod time code
1             package Brownie::Driver::Mechanize;
2              
3 19     19   1635752 use strict;
  19         44  
  19         1010  
4 19     19   206 use warnings;
  19         39  
  19         645  
5 19     19   14043 use parent 'Brownie::Driver';
  19         1474  
  19         164  
6 19     19   30181 use WWW::Mechanize;
  19         3470048  
  19         887  
7 19     19   22492 use HTML::TreeBuilder::XPath;
  19         1484204  
  19         274  
8 19     19   1118 use constant HAS_LIBXML => eval { require HTML::TreeBuilder::LibXML; 1 };
  19         41  
  19         44  
  19         10112  
  0         0  
9 19     19   108 use Scalar::Util qw(blessed);
  19         39  
  19         1260  
10              
11 19     19   11961 use Brownie;
  19         59  
  19         697  
12 19     19   124 use Brownie::XPath;
  19         38  
  19         370  
13 19     19   13014 use Brownie::Node::Mechanize;
  19         56  
  19         14188  
14              
15             our $NodeClass = 'Brownie::Node::Mechanize';
16              
17             sub DESTROY {
18 20     20   125790 my $self = shift;
19 20         1634 delete $self->{browser};
20             }
21              
22             sub browser {
23 478     478 1 3921 my $self = shift;
24              
25 478   66     3602 $self->{browser} ||= WWW::Mechanize->new(
26             agent => "Brownie/${Brownie::VERSION}",
27             cookie_jar => +{},
28             quiet => 1,
29             stack_depth => 1,
30             );
31              
32 478         380378 return $self->{browser};
33             }
34              
35             sub visit {
36 89     89 1 485497 my ($self, $url) = @_;
37 89         475 $self->browser->get("$url"); # stringify for URI
38             }
39              
40             sub current_url {
41 114     114 1 19572 my $self = shift;
42 114         443 return $self->browser->uri->clone;
43             }
44              
45             sub current_path {
46 46     46 1 17637 my $self = shift;
47 46         607 return $self->current_url->path;
48             }
49              
50             sub status_code {
51 4     4 1 62534 my $self = shift;
52 4         22 return $self->browser->status;
53             }
54              
55             sub response_headers {
56 1     1 1 1673 my $self = shift;
57 1         42 return $self->browser->res->headers;
58             }
59              
60             sub title {
61 1     1 1 66950 my $self = shift;
62 1         5 return $self->browser->title;
63             }
64              
65             sub source {
66 137     137 1 3080 my $self = shift;
67 137         484 my $content = $self->browser->content;
68             # TODO: consider gzip and deflate
69 137         6580 return $content;
70             }
71              
72             sub _root {
73 133     133   223 my $self = shift;
74 133 50       865 my $builder = HAS_LIBXML ? 'HTML::TreeBuilder::LibXML' : 'HTML::TreeBuilder::XPath';
75 133         480 my $tree = $builder->new_from_content($self->source);
76             }
77              
78             sub find {
79 281     281 1 228109 my ($self, $locator, %args) = @_;
80              
81 281         1107 my @elements = $self->all($locator, %args);
82 281 50       3652 return @elements ? shift(@elements) : undef;
83             }
84              
85             sub all {
86 287     287 1 669 my ($self, $locator, %args) = @_;
87              
88 287         567 my @elements = ();
89 287         1377 my $xpath = Brownie::XPath::to_xpath($locator);
90              
91 287 100       3032 if (my $base = $args{base}) {
92 154 50 33     2490 my $node = (blessed($base) and $base->can('native')) ? $base->native : $base;
93 154 50       1023 $xpath = ".$xpath" unless $xpath =~ /^\./;
94 154         693 @elements = $node->findnodes($xpath); # abs2rel
95             }
96             else {
97 133         653 @elements = $self->_root->findnodes($xpath);
98             }
99              
100 287 100       8678767 return @elements ? map { $NodeClass->new(driver => $self, native => $_) } @elements : ();
  305         3179  
101             }
102              
103             1;
104              
105             =head1 NAME
106              
107             Brownie::Driver::Mechanize - WWW::Mechanize bridge implementation
108              
109             =head1 SYNOPSIS
110              
111             my $driver = Brownie::Driver::Mechanize->new;
112              
113             $driver->visit($url);
114             my $title = $driver->title;
115              
116             =head1 METHODS
117              
118             =head2 IMPLEMENTED
119              
120             =over 4
121              
122             =item * C
123              
124             =item * C
125              
126             =item * C
127              
128             =item * C
129              
130             =item * C
131              
132             =item * C
133              
134             =item * C </td> </tr> <tr> <td class="h" > <a name="135">135</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="136">136</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =item * C<source> </td> </tr> <tr> <td class="h" > <a name="137">137</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="138">138</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =item * C<find($locator)> </td> </tr> <tr> <td class="h" > <a name="139">139</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="140">140</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =item * C<all($locator)> </td> </tr> <tr> <td class="h" > <a name="141">141</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="142">142</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =back </td> </tr> <tr> <td class="h" > <a name="143">143</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="144">144</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =head2 NOT SUPPORTED </td> </tr> <tr> <td class="h" > <a name="145">145</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="146">146</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =over 4 </td> </tr> <tr> <td class="h" > <a name="147">147</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="148">148</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =item * C<screenshot($filename)> </td> </tr> <tr> <td class="h" > <a name="149">149</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="150">150</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =item * C<execute_script($javascript)> </td> </tr> <tr> <td class="h" > <a name="151">151</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="152">152</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =item * C<evaluate_script($javascript)> </td> </tr> <tr> <td class="h" > <a name="153">153</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="154">154</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =back </td> </tr> <tr> <td class="h" > <a name="155">155</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="156">156</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =head1 AUTHOR </td> </tr> <tr> <td class="h" > <a name="157">157</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="158">158</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt> </td> </tr> <tr> <td class="h" > <a name="159">159</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="160">160</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =head1 LICENSE </td> </tr> <tr> <td class="h" > <a name="161">161</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="162">162</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> This library is free software; you can redistribute it and/or modify </td> </tr> <tr> <td class="h" > <a name="163">163</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> it under the same terms as Perl itself. </td> </tr> <tr> <td class="h" > <a name="164">164</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="165">165</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =head1 SEE ALSO </td> </tr> <tr> <td class="h" > <a name="166">166</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="167">167</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> L<Brownie::Driver>, L<WWW::Mechanize>, L<Brownie::Node::Mechanize> </td> </tr> <tr> <td class="h" > <a name="168">168</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="169">169</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =cut </td> </tr> </table> </body> </html>