blib/lib/Test/HTML/Form.pm

filter: Statementstmt 93.5% 217 / 232 Branchbran 65.4% 55 / 84 Conditioncond 47.0% 16 / 34 MC/DCmcdc 19.0% 4 / 21 Subroutinesub 100% 33 / 33 Podpod 100% 16 / 16 totaltotal 81.1% 341 / 420 CC 61 SCAR 18.3
CC
61
Cov
82%
_compare
17.0
form_select_field_matches
10.9
__ANON__
8.1
CRAP 6.3
↑ Summary
1 package Test::HTML::Form;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Test::HTML::Form - HTML Testing and Value Extracting
9  
10 =head1 VERSION
11  
12 1.00
13  
14 =head1 SYNOPSIS
15  
16 use Test::HTML::Form;
17  
18 my $filename = 't/form_with_errors.html';
19  
20 my $response = $ua->request($request)
21  
22 # test functions
23  
24 title_matches($filename,'Foo Bar','title matches');
25  
26 no_title($filename,'test site','no english title');
27  
28 tag_matches($response,
29 'p',
30 { class => 'formError',
31 _content => 'There is an error in this form.' },
32 'main error message appears as expected' );
33  
34 no_tag($filename,
35 'p',
36 { class => 'formError',
37 _content => 'Error' },
38 'no unexpected errors' );
39  
40  
41 text_matches($filename,'koncerty','found text : koncerty'); # check text found in file
42  
43 no_text($filename,'Concert','no text matching : Concert'); # check text found in file
44  
45 image_matches($filename,'/images/error.gif','matching image found image in HTML');
46  
47 link_matches($filename,'/post/foo.html','Found link in HTML');
48  
49 script_matches($response, qr/function someWidget/, 'found widget in JS');
50  
51 form_field_value_matches($response,'category_id', 12345678, undef, 'category_id matches');
52  
53 form_select_field_matches($filename,{ field_name => $field_name, selected => $field_value, form_name => $form_name}, $description);
54  
55 form_checkbox_field_matches($response,{ field_name => $field_name, selected => $field_value, form_name => $form_name}, $description);
56  
57 # Data extraction functions
58  
59 my $form_values = Test::HTML::Form->get_form_values({filename => $filename, form_name => 'form1'});
60  
61 my $posting_id = Test::HTML::Form->extract_text({filename => 'publish.html', pattern => 'Reference :\s(\d+)'});
62  
63 =head1 DESCRIPTION
64  
65 Test HTML pages and forms, and extract values.
66  
67 Developed for and released with permission of Slando (http://www.slando.com)
68  
69 All test functions will take either a filename or an HTTP::Response compatible object (i.e. any object with a content method)
70  
71 =cut
72  
73 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 3 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 3 ▶use base qw( Exporter Test::Builder::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
77 3 our @EXPORT = qw(
78 link_matches no_link
79 image_matches no_image
80 tag_matches no_tag
81 text_matches no_text
82 script_matches
83 title_matches no_title
84 form_field_value_matches
85 form_select_field_matches
86 form_checkbox_field_matches
87 );
88  
89 3 my $Test = Test::Builder->new;
90 3 my $CLASS = __PACKAGE__;
91 3 my %parsed_files = ();
92 3 my %parsed_file_forms = ();
93  
94 3 our $VERSION = 1.00;
95  
96 =head1 FUNCTIONS
97  
98 =head2 image_matches
99  
100 Test that some HTML contains an img tag with a src attribute matching the link provided.
101  
102 image_matches($filename,$image_source,'matching image found image in HTML');
103  
104 Passes when at least one instance found, fails if no matches found.
105  
106 Takes a list of arguments filename/response, string or quoted-regexp to match, and optional test comment/name
107  
108 =cut
109  
110 sub image_matches {
111 1 ▶ my ($filename,$link,$name) = (@_);
Subroutine image_matches: called CC=1 SCAR=0.0
Pod documented
112 1 local $Test::Builder::Level = 2;
113 1 return tag_matches($filename,'img',{ src => $link },$name);
114 };
115  
116  
117 =head2 no_image
118  
119 Test that some HTML doesn't contain any img tag with a src attribute matching the link provided.
120  
121 no_image($response,$image_source,'no matching image found in HTML');
122  
123 Passes when no matches found, fails if any matches found.
124  
125 Takes a list of arguments filename/response, string or quoted-regexp to match, and optional test comment/name
126  
127 =cut
128  
129 sub no_image {
130 1 ▶ my ($filename,$link,$name) = (@_);
Subroutine no_image: called CC=1 SCAR=0.0
Pod documented
131 1 local $Test::Builder::Level = 2;
132 1 return no_tag($filename,'img',{ src => $link },$name);
133 };
134  
135  
136 =head2 link_matches
137  
138 Test that some HTML contains a href tag with a src attribute matching the link provided.
139  
140 link_matches($response,$link_destination,'Found link in HTML');
141  
142 Passes when at least one instance found, fails if no matches found.
143  
144 Takes a list of arguments filename/response, string or quoted-regexp to match, and optional test comment/name
145  
146 =cut
147  
148 sub link_matches {
149 3 ▶ my ($filename,$link,$name) = (@_);
Subroutine link_matches: called CC=1 SCAR=0.0
Pod documented
150 3 local $Test::Builder::Level = 2;
151 3 return tag_matches($filename,['a','link'],{ href => $link },$name);
152 };
153  
154 =head2 no_link
155  
156 Test that some HTML does not contain a href tag with a src attribute matching the link provided.
157  
158 link_matches($filename,$link_destination,'Link not in HTML');
159  
160 Passes when if no matches found, fails when at least one instance found.
161  
162 Takes a list of arguments filename/response, string or quoted-regexp to match, and optional test comment/name
163  
164 =cut
165  
166 sub no_link {
167 1 ▶ my ($filename,$link,$name) = (@_);
Subroutine no_link: called CC=1 SCAR=0.0
Pod documented
168 1 local $Test::Builder::Level = 2;
169 1 return no_tag($filename,'a',{ href => $link },$name);
170 };
171  
172 =head2 title_matches
173  
174 Test that some HTML contains a title tag with content matching the pattern/string provided.
175  
176 title_matches($filename,'Foo bar home page','title matches');
177  
178 Passes when at least one instance found, fails if no matches found.
179  
180 Takes a list of arguments filename/response, string or quoted-regexp to match, and optional test comment/name
181  
182 =cut
183  
184 sub title_matches {
185 1 ▶ my ($filename,$title,$name) = @_;
Subroutine title_matches: called CC=1 SCAR=0.0
Pod documented
186 1 local $Test::Builder::Level = 2;
187 1 return tag_matches($filename,"title", { _content => $title } ,$name);
188 };
189  
190 =head2 no_title
191  
192 Test that some HTML does not contain a title tag with content matching the pattern/string provided.
193  
194 no_title($filename,'Foo bar home page','title matches');
195  
196 Passes if no matches found, fails when at least one instance found.
197  
198 Takes a list of arguments filename/response, string or quoted-regexp to match, and optional test comment/name
199  
200 =cut
201  
202 sub no_title {
203 1 ▶ my ($filename,$title,$name) = (@_);
Subroutine no_title: called CC=1 SCAR=0.0
Pod documented
204 1 local $Test::Builder::Level = 2;
205 1 ▶ return no_tag($filename,'title', sub { shift->as_trimmed_text =~ m/$title/ },$name);
Subroutine __ANON__: called CC=1 SCAR=0.0
206 }
207  
208  
209 =head2 tag_matches
210  
211 Test that some HTML contains a tag with content or attributes matching the pattern/string provided.
212  
213 tag_matches($filename,'a',{ href => $link },$name); # check matching tag found in file
214  
215 Passes when at least one instance found, fails if no matches found.
216  
217 Takes a list of arguments
218  
219 =over 4
220  
221 =item filename/response - string of path/name of file, or an HTTP::Response object
222  
223 =item tag type(s) - string or arrarref of strings naming which tag(s) to match
224  
225 =item attributes - hashref of attributes and strings or quoted-regexps to match
226  
227 =item comment - an optional test comment/name
228  
229 =back
230  
231 =cut
232  
233 sub tag_matches {
234 7 ▶ my ($filename, $tag, $attr_ref, $name) = @_;
Subroutine tag_matches: called CC=5 SCAR=16.1
Pod documented
235 7 my $count = 0;
236  
237 7 ▶ if (ref $tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tag) { } 4 3 7
238 4 foreach my $this_tag (@$tag) {
239 8 $count += _tag_count($filename, $this_tag, $attr_ref);
240 }
241 } else {
242 3 $count = _tag_count($filename, $tag, $attr_ref);
243 }
244  
245 7 my $tb = $CLASS->builder;
246 7 my $ok = $tb->ok( $count, $name);
247 7 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 6 7
248 1 ▶ my $tagname = ( ref $tag ) ? join (' or ', @$tag) : $tag ;
Branch: 1 branch
BranchTrueFalseTotal
ref $tag ? : 1 0 1
249 1 $tb->diag("Expected at least one tag of type '$tagname' in file $filename matching condition, but got 0\n");
250 }
251 7 return $ok;
252 }
253  
254  
255  
256 =head2 no_tag
257  
258 Test that some HTML does not contain a tag with content or attributes matching the pattern/string provided.
259  
260 no_tag($filename,'a',{ href => $link },$name); # check matching tag NOT found in file
261  
262 Passes if no matches found, fails when at least one instance found.
263  
264 Takes a list of arguments filename/response, hashref of attributes and strings or quoted-regexps to match, and optional test comment/name
265  
266 =cut
267  
268 sub no_tag {
269 4 ▶ my ($filename,$tag,$attr_ref,$name) = @_;
Subroutine no_tag: called CC=2 SCAR=7.1
Pod documented
270 4 my $count = _tag_count($filename, $tag, $attr_ref);
271 4 my $tb = $CLASS->builder;
272 4 my $ok = $tb->is_eq( $count, 0, $name);
273 4 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 4 4
274 0 $tb->diag("Expected no tags of type $tag matching criteria in file $filename, but got $count\n");
275 }
276 4 return $ok;
277 };
278  
279 =head2 text_matches
280  
281 Test that some HTML contains some content matching the pattern/string provided.
282  
283 text_matches($filename,$text,$name); # check text found in file
284  
285 Passes when at least one instance found, fails if no matches found.
286  
287 Takes a list of arguments filename/response, string or quoted-regexp to match, and optional test comment/name
288  
289 =cut
290  
291 sub text_matches {
292 1 ▶ my ($filename,$text,$name) = @_;
Subroutine text_matches: called CC=2 SCAR=7.1
Pod documented
293 1 my $count = _count_text({filename => $filename, text => $text });
294 1 my $tb = $CLASS->builder;
295 1 my $ok = $tb->ok( $count, $name);
296 1 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 1 1
297 0 $tb->diag("Expected HTML to contain at least one instance of text '$text' in file $filename but not found\n");
298 }
299 1 return $ok;
300 };
301  
302 =head2 no_text
303  
304 Test that some HTML does not contain some content matching the pattern/string provided.
305  
306 no_text($filename,$text,$name); # check text NOT found in file
307  
308 Passes if no matches found, fails when at least one instance found.
309  
310 Takes a list of arguments filename/response, string or quoted-regexp to match, and optional test comment/name
311  
312 =cut
313  
314 sub no_text {
315 1 ▶ my ($filename,$text,$name) = @_;
Subroutine no_text: called CC=2 SCAR=7.1
Pod documented
316 1 my $count = _count_text({filename => $filename, text => $text });
317 1 my $tb = $CLASS->builder;
318 1 my $ok = $tb->is_eq( $count, 0 , $name);
319 1 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 1 1
320 0 $tb->diag("Expected HTML to not contain text '$text' in file $filename but $count instances found\n");
321 }
322 1 return $ok;
323 };
324  
325  
326 =head2 script_matches
327  
328 Test that HTML script element contains text matcging that provided.
329  
330 script_matches($response, qr/function someWidget/, 'found widget in JS');
331  
332 Passes when at least one instance found, fails if no matches found.
333  
334 Takes a list of arguments filename/response, string or quoted-regexp to match, and optional test comment/name
335  
336 =cut
337  
338 sub script_matches {
339 1 ▶ my ($filename,$text_to_match,$name) = @_;
Subroutine script_matches: called CC=3 SCAR=11.5
Pod documented
340 1 my $pattern;
341 1 ▶ if (ref($text_to_match) eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $text_to_match eq "Regexp") 1 0 1
342 1 $pattern = $text_to_match;
343 }
344 1 my $tree = _get_tree($filename);
345  
346 my @parse_args = sub {
347 1 ▶ my $elem = shift;
Subroutine __ANON__: called CC=4 SCAR=15.2
348 1 ▶ return 0 unless (ref $elem eq 'HTML::Element' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $elem eq "HTML::Element" 0 1 1
349 1 my $ok = 0;
350 1 (my $text = $elem->as_HTML) =~ s/<(.|\n)*?>//g;
351 1 ▶ if ($pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern) { } 1 0 1
352 1 my $ok = $text =~ m/$pattern/;
353 1 ▶ return $ok || $text =~ m/$pattern/;
Condition33%1/3
$ok || $text =~ /$pattern/
l!l&&r!l&&!r
100
MC/DC0%0/2
$ok || $text =~ /$pattern/
$ok $text =~ /$pattern/
Truth table33%1/3
A || B
A= $okB= $text =~ /$pattern/
ABresult
1X1
011
000
354 } else {
355 0 $text eq $text_to_match;
356 }
357 1 };
358  
359 1 my $count = $tree->look_down( _tag => 'script', @parse_args );
360  
361 1 my $tb = $CLASS->builder;
362 1 my $ok = $tb->ok( $count, $name);
363 1 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 1 1
364 0 $tb->diag("Expected script tag in file $filename matching $text_to_match, but got 0\n");
365 }
366 1 return $ok;
367 };
368  
369  
370  
371 =head2 form_field_value_matches
372  
373 Test that the HTML contains a form element with the value matching that provided.
374  
375 form_field_value_matches($filename,$field_name, $field_value, $form_name, $description);
376  
377 form_field_value_matches($filename,$field_name, qr/some pattern/, undef, 'test for foo in bar form field');
378  
379 Takes a list of arguments : filename/response, string or quoted-regexp to match, optional form_name, and optional test comment/name
380  
381 Field value argument can be a string (for exact matches) or a quoted regexp (for pattern matches)
382  
383 Use form_select_field_matches for select elements.
384  
385 Use form_checkbox_field_matches for checkbox elements
386  
387 =cut
388  
389 sub form_field_value_matches {
390 2 ▶ my ($filename,$field_name, $field_value, $form_name, $description) = @_;
Subroutine form_field_value_matches: called CC=4 SCAR=14.3
Pod documented
391 2 my $form_fields = __PACKAGE__->get_form_values({ filename => $filename, form_name => $form_name });
392 2 my $tb = $CLASS->builder;
393  
394 2 my $elems = $form_fields->{$field_name};
395  
396 2 my $ok = 0;
397 2 foreach my $elem (@$elems) {
398 2 my $matches = _compare($elem,$field_value);
399 2 ▶ if ($matches) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matches) 2 0 2
400 2 $ok = $tb->ok( $matches , $description);
401 2 last;
402 }
403 }
404  
405 2 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 2 2
406 0 $tb->ok( 0 , $description);
407 0 $tb->diag("Expected form to contain field '$field_name' and have value of '$field_value' but not found in file $filename\n");
408 }
409 2 return $ok;
410 };
411  
412 =head2 form_select_field_matches
413  
414 Test that the HTML contains a form element with the value matching that provided.
415  
416 form_select_field_matches($filename,{ field_name => $field_name, selected => $field_value, form_name => $form_name}, $description);
417  
418 Takes a mixed list/ hashref of arguments :
419  
420 =over 4
421  
422 =item filename/response,
423  
424 =item hashref of search attributes, keys are : field_name, selected, form_name (optional)
425  
426 =item optional test comment/name
427  
428 =back
429  
430 Selected field value can be string or quoted regexp
431  
432 =cut
433  
434 sub form_select_field_matches {
435 2 ▶ my ($filename, $field_value_args, $description) = @_;
Subroutine form_select_field_matches: called CC=10 SCAR=23.9
Pod documented
436 2 my $form_fields = __PACKAGE__->get_form_values({ filename => $filename, form_name => $field_value_args->{form_name} });
437 2 my $tb = $CLASS->builder;
438 2 my $ok = 0;
439 2 my $field_value = $field_value_args->{selected};
440 2 my $field_name = $field_value_args->{field_name};
441  
442 2 my $select_elem = $form_fields->{$field_name}[0];
443  
444  
445 2 ▶ if ($select_elem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select_elem) { } 1 1 2
446 1 ▶ unless (UNIVERSAL::can($select_elem,'descendants')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::can($select_elem, "descendants")) 0 1 1
447 0 die "$select_elem (",$select_elem->tag,") is not a select html element for field : $field_name - did you mean to call form_checkbox_field_matches ?";
448 }
449 1 my $selected_option;
450 1 foreach my $option ( $select_elem->descendants ) {
451 10 ▶ next unless (ref($option) && ( lc($option->tag) eq 'option') );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $option and lc $option->tag eq "option" 0 10 10
Condition33%1/3
ref $option and lc $option->tag eq "option"
!ll&&!rl&&r
0010
MC/DC0%0/2
ref $option and lc $option->tag eq "option"
ref $option lc $option->tag eq "option"
Truth table33%1/3
A and B
A= ref $optionB= lc $option->tag eq "option"
ABresult
0X0
100
111
452 10 ▶ if ( _compare($option, $field_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_compare($option, $field_value)) 1 9 10
453 1 $selected_option = $option;
454 1 last;
455 }
456 }
457  
458 1 ▶ $ok = $tb->ok( $selected_option && scalar grep (m/selected/i && $selected_option->attr($_), $selected_option->all_external_attr_names), $description);
Condition40%2/5
$selected_option && scalar grep((/selected/i && $selected_option->attr($_)), $selected_option->all_external_attr_names)
!ll
01
/selected/i && $selected_option->attr($_)
!ll&&!rl&&r
001
MC/DC0%0/1
$selected_option && scalar grep((/selected/i && $selected_option->attr($_)), $selected_option->all_external_attr_names)
$selected_option
MC/DC0%0/2
/selected/i && $selected_option->attr($_)
/selected/i $selected_option->attr($_)
Truth table50%1/2
A
A= $selected_option
Aresult
00
11
Truth table33%1/3
A && B
A= /selected/iB= $selected_option->attr($_)
ABresult
0X0
100
111
459 } else {
460 1 $ok = $tb->ok(0, $description);
461 }
462 2 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 1 2
463 1 $tb->diag("Expected form to contain field '$field_name' and have option with value of '$field_value' selected but not found in file $filename \n");
464 }
465 2 return $ok;
466 }
467  
468 =head2 form_checkbox_field_matches
469  
470 Test that the HTML contains a form element with the value matching that provided.
471  
472 form_checkbox_field_matches($filename,{ field_name => $field_name, selected => $field_value, form_name => $form_name}, $description);
473  
474 Takes a mixed list/ hashref of arguments :
475  
476 =over 4
477  
478 =item filename/response,
479  
480 =item hashref of search attributes, keys are : field_name, selected, form_name (optional)
481  
482 =item optional test comment/name
483  
484 =back
485  
486 Selected field value can be string or quoted regexp
487  
488 =cut
489  
490 sub form_checkbox_field_matches {
491 1 ▶ my ($filename, $field_value_args, $description) = @_;
Subroutine form_checkbox_field_matches: called CC=7 SCAR=20.3
Pod documented
492 1 my $form_fields = __PACKAGE__->get_form_values({ filename => $filename, form_name => $field_value_args->{form_name} });
493 1 my $tb = $CLASS->builder;
494  
495 1 my $field_value = $field_value_args->{selected};
496 1 my $field_name = $field_value_args->{field_name};
497 1 my $selected_box;
498 1 ▶ my $checkbox_elems = $form_fields->{$field_name} || [];
Condition50%1/2
$form_fields->{$field_name} || []
l!l
10
MC/DC0%0/1
$form_fields->{$field_name} || []
$form_fields->{$field_name}
Truth table50%1/2
A
A= $form_fields->{$field_name}
Aresult
11
00
499  
500 1 foreach my $checkbox ( @$checkbox_elems ) {
501 1 ▶ if ( _compare($checkbox, $field_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_compare($checkbox, $field_value)) 1 0 1
502 1 $selected_box = $checkbox;
503 1 last;
504 }
505 }
506  
507 1 ▶ my $ok = $tb->ok( $selected_box && scalar grep (m/checked/i && $selected_box->attr($_), $selected_box->all_attr_names), $description);
Condition60%3/5
$selected_box && scalar grep((/checked/i && $selected_box->attr($_)), $selected_box->all_attr_names)
!ll
01
/checked/i && $selected_box->attr($_)
!ll&&!rl&&r
501
MC/DC0%0/1
$selected_box && scalar grep((/checked/i && $selected_box->attr($_)), $selected_box->all_attr_names)
$selected_box
MC/DC50%1/2
/checked/i && $selected_box->attr($_)
/checked/i $selected_box->attr($_)
Truth table50%1/2
A
A= $selected_box
Aresult
00
11
Truth table66%2/3
A && B
A= /checked/iB= $selected_box->attr($_)
ABresult
0X0
100
111
508 1 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 1 1
509 0 $tb->diag("Expected form to contain field '$field_name' and have option with value of '$field_value' selected but not found in file $filename\n");
510 }
511 1 return $ok;
512 }
513  
514 =head2 get_form_values
515  
516 Extract form fields and their values from HTML content
517  
518 my $form_values = Test::HTML::Form->get_form_values({filename => $filename, form_name => 'form1'});
519  
520 Takes a hashref of arguments : filename (name of file or an HTTP::Response object, required), form_name (optional).
521  
522 Returns a hashref of form fields, with name as key, and arrayref of XML elements for that field.
523  
524 =cut
525  
526 sub get_form_values {
527 6 ▶ my $class = shift;
Subroutine get_form_values: called CC=7 SCAR=20.0
Pod documented
528 6 my $args = shift;
529 3 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
530 6 my $form_name = $args->{form_name};
531 6 my $internal_form_name = $form_name . ' form';
532 6 ▶ if ($parsed_file_forms{$args->{filename}}{$internal_form_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed_file_forms{$args->{'filename'}}{$internal_form_name}) { } 4 2 6
533 4 return $parsed_file_forms{$args->{filename}}{$internal_form_name};
534 } else {
535 2 my $tree = _get_tree($args->{filename});
536 2 my $form_fields = { };
537 my ($form) = $tree->look_down('_tag', 'form',
538 sub {
539 2 ▶ my $form = shift;
Subroutine __ANON__: called CC=4 SCAR=20.9
540 2 ▶ if ($form_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form_name) { } 0 2 2
541 0 ▶ return 1 if $form->attr('name') eq $form_name;
Branch: 1 branch
BranchTrueFalseTotal
if $form->attr("name") eq $form_name 0 0 0
542 0 ▶ return 1 if $form->attr('id') eq $form_name;
Branch: 1 branch
BranchTrueFalseTotal
if $form->attr("id") eq $form_name 0 0 0
543 } else {
544 2 return 1;
545 }
546 }
547 2 );
548 2 ▶ if (ref $form) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $form) 2 0 2
549 2 my @form_nodes = $form->descendants();
550 2 foreach my $node (@form_nodes) {
551 126 ▶ next unless (ref($node));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 0 126 126
552 126 ▶ if (lc($node->tag) =~ /^(input|select|textarea|button)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($node->tag) =~ /^(input|select|textarea|button)$/i) 22 104 126
553 22 ▶ if (lc $node->attr('type') =~ /(radio|checkbox)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $node->attr('type') =~ /(radio|checkbox)/) { } 6 16 22
554 6 push (@{$form_fields->{$node->attr('name')}},$node);
555 } else {
556 16 $form_fields->{$node->attr('name')} = [ $node ];
557 }
558 }
559 }
560 }
561 2 $parsed_file_forms{$args->{filename}}{$internal_form_name} = $form_fields;
562  
563 2 return $form_fields;
564 }
565 }
566  
567 =head2 extract_text
568  
569 my $posting_id = Test::HTML::Form->extract_text({filename => 'publish.html', pattern => 'Reference :\s(\d+)'});
570  
571 =cut
572  
573 sub extract_text {
574 1 ▶ my $class = shift;
Subroutine extract_text: called CC=1 SCAR=0.0
Pod documented
575 1 my $args = shift;
576 1 my $tree = _get_tree($args->{filename});
577 1 my $pattern = $args->{pattern};
578 my ($node) = $tree->look_down( sub {
579 76 ▶ my $thisnode = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
580 76 $thisnode->normalize_content;
581 76 ▶ return 1 if ($thisnode->as_trimmed_text =~ m/$pattern/i);
Branch: 1 branch
BranchTrueFalseTotal
if $thisnode->as_trimmed_text =~ /$pattern/i 3 73 76
582 1 });
583 1 my ($match) = ($node->as_trimmed_text =~ m/$pattern/i);
584  
585 1 return $match;
586 }
587  
588  
589  
590 #
591 ##########################################
592 # Private / Internal methods and functions
593  
594 sub _compare {
595 13 ▶ my ($elem, $field_value) = @_;
Subroutine _compare: called CC=10 SCAR=28.3
596 13 ▶ unless ($elem && (ref$elem eq 'HTML::Element') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem and ref $elem eq "HTML::Element") 0 13 13
Condition33%1/3
$elem and ref $elem eq "HTML::Element"
!ll&&!rl&&r
0013
MC/DC0%0/2
$elem and ref $elem eq "HTML::Element"
$elem ref $elem eq "HTML::Element"
Truth table33%1/3
A and B
A= $elemB= ref $elem eq "HTML::Element"
ABresult
0X0
100
111
597 0 warn "_compare passed $elem and value $field_value, $elem should be HTML::Element but is : ", ref $elem, "\n";
598 0 return 0 ;
599 }
600  
601 13 ▶ my $have_regexp = ( ref($field_value) eq 'Regexp' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $field_value eq 'Regexp' ? : 0 13 13
602 13 my $value = $elem->attr('value') ;
603 13 ▶ unless (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 11 2 13
604 11 $value = $elem->as_trimmed_text;
605 }
606 13 my $ok;
607 13 ▶ if ($have_regexp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_regexp) { } 0 13 13
608 0 ▶ $ok = ( $elem && $value =~ m/$field_value/ ) ? 1 : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
$elem && $value =~ /$field_value/ ? : 0 0 0
Condition0%0/3
$elem and $value =~ /$field_value/
!ll&&!rl&&r
000
MC/DC0%0/2
$elem and $value =~ /$field_value/
$elem $value =~ /$field_value/
Truth table0%0/3
A and B
A= $elemB= $value =~ /$field_value/
ABresult
0X0
100
111
609 } else {
610 13 ▶ $ok = ( $elem && $value eq $field_value ) ? 1 : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
$elem && $value eq $field_value ? : 4 9 13
Condition66%2/3
$elem and $value eq $field_value
!ll&&!rl&&r
094
MC/DC50%1/2
$elem and $value eq $field_value
$elem $value eq $field_value
Truth table66%2/3
A and B
A= $elemB= $value eq $field_value
ABresult
0X0
100
111
611 }
612 13 return $ok
613 }
614  
615 sub _tag_count {
616 15 ▶ my ($filename, $tag, $attr_ref) = @_;
Subroutine _tag_count: called CC=5 SCAR=16.1
617 15 my $tree = _get_tree($filename);
618 15 my @parse_args = ();
619 15 ▶ if ( ref $attr_ref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_ref eq 'HASH') { } 14 1 15
620 14 my $pattern;
621 14 ▶ if (ref($attr_ref->{_content}) eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_ref->{"_content"} eq "Regexp") 3 11 14
622 3 $pattern = $attr_ref->{_content};
623 3 delete $attr_ref->{_content};
624 }
625  
626 14 @parse_args = %$attr_ref ;
627 14 ▶ if ($pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern) 3 11 14
628 push( @parse_args, sub {
629 2 ▶ return 0 unless (ref $_[0] eq 'HTML::Element' );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HTML::Element" 0 2 2
630 2 return $_[0]->as_trimmed_text =~ m/$pattern/;
631 3 } );
632 }
633 } else {
634 1 @parse_args = $attr_ref ;
635 }
636 15 my $count = $tree->look_down( _tag => $tag, @parse_args );
637  
638 15 ▶ return $count || 0;
Condition100%2/2
$count || 0
l!l
69
MC/DC100%1/1
$count || 0
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
639 }
640  
641  
642 sub _count_text {
643 2 ▶ my $args = shift;
Subroutine _count_text: called CC=2 SCAR=6.9
644 2 my $tree = _get_tree($args->{filename});
645 2 my $text = $args->{text};
646 my $count = $tree->look_down( sub {
647 77 ▶ my $node = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
648 77 $node->normalize_content;
649 77 ▶ return 1 if ($node->as_trimmed_text =~ m/$text/);
Branch: 1 branch
BranchTrueFalseTotal
if $node->as_trimmed_text =~ /$text/ 1 76 77
650 2 });
651 2 ▶ return $count || 0;
Condition100%2/2
$count || 0
l!l
11
MC/DC100%1/1
$count || 0
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
652 }
653  
654 sub _get_tree {
655 21 ▶ my $filename = shift;
Subroutine _get_tree: called CC=5 SCAR=17.0
656 21 ▶ unless ($parsed_files{$filename}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed_files{$filename}) 2 19 21
657 2 my $tree = HTML::TreeBuilder->new;
658 2 $tree->store_comments(1);
659 2 ▶ if (ref $filename && $filename->can('content')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename and $filename->can('content')) { } 0 2 2
Condition33%1/3
ref $filename and $filename->can("content")
!ll&&!rl&&r
200
MC/DC0%0/2
ref $filename and $filename->can("content")
ref $filename $filename->can("content")
Truth table33%1/3
A and B
A= ref $filenameB= $filename->can("content")
ABresult
0X0
100
111
660 0 $tree->parse_content($filename->decoded_content);
661 } else {
662 2 ▶ die "can't find file $filename" unless (-f $filename);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 2 2
663 2 $tree->parse_file($filename);
664 }
665 2 $parsed_files{$filename} = $tree;
666 }
667 21 return $parsed_files{$filename};
668 }
669  
670  
671 =head1 SEE ALSO
672  
673 =over 4
674  
675 =item Test::HTML::Content
676  
677 =item HTML::TreeBuilder
678  
679 =item Test::HTTP::Response
680  
681 =back
682  
683 =head1 AUTHOR
684  
685 Aaron Trevena
686  
687 =head1 BUGS
688  
689 Please report any bugs or feature requests to http://rt.cpan.org
690  
691 =head1 COPYRIGHT & LICENSE
692  
693 Copyright 2008 Slando.
694 Copyright 2009 Aaron Trevena.
695  
696 This library is free software; you can redistribute it and/or modify
697 it under the same terms as Perl itself, either Perl version 5.8.8 or,
698 at your option, any later version of Perl 5 you may have available.
699  
700 =cut
701  
702  
703 3 1;
↑ Summary