File: 05-Form-1.t

package info (click to toggle)
libwww-mechanize-formfiller-perl 0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 292 kB
  • ctags: 46
  • sloc: perl: 1,504; sh: 6; makefile: 2
file content (113 lines) | stat: -rwxr-xr-x 3,121 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use strict;

my @tests;
BEGIN{
@tests = (
  # Class, name,args, html index, expected value
  ['Fixed',"text_input_1",["bar"],0,"bar"],
  ['Fixed',"text_input_2",["bar"],1,"bar"],
  ['Fixed',"radio",["0"],2,"0"],
  ['Fixed',"radio",["1"],2,"1"],
  ['Fixed',"radio",["2"],2,"2"],
  ['Fixed',"radio",["3"],2,"3"],
  ['Fixed',"checkbox_1",[""],3,undef],
  ['Fixed',"checkbox_1",["on"],3,"on"],
  ['Fixed',"checkbox_2",[""],4,undef],
  ['Fixed',"checkbox_2",["on"],4,"on"],
  ['Default',"text_input_1",["bar"],0,"bar"],
  ['Default',"text_input_2",["bar"],1,"xxx"],
  ['Default',"radio",["0"],2,"1"],
  ['Default',"radio",["1"],2,"1"],
  ['Default',"radio",["2"],2,"1"],
  ['Default',"radio",["3"],2,"1"],
  ['Default',"checkbox_1",[""],3,"on"],
  ['Default',"checkbox_1",["on"],3,"on"],
  ['Default',"checkbox_2",[""],4,undef], # ??? This is a bit weird, but that's OK at the moment
  ['Default',"checkbox_2",["on"],4,"on"],

  # REs
  ['Fixed',qr/text_input/,["bar"],0,"bar"],
  ['Fixed',qr/text_input/,["bar"],1,"bar"],
);
};

use Test::More tests => 1 + scalar @tests * 3;
BEGIN {
  use_ok("WWW::Mechanize::FormFiller");
};
SKIP: {
  eval { require HTML::Form };
  skip "Need HTML::Form to run the more extensive tests", scalar @tests * 3 if $@;

  # Load the different HTML sets
  my @forms = split /---/, do {
    local $/ = undef;
    <DATA>;
  };

  for my $row (@tests) {
    my ($class,$name,$args,$index,$expected) = @$row;

    my $f = WWW::Mechanize::FormFiller->new();
    isa_ok($f,"WWW::Mechanize::FormFiller");
    my $form = HTML::Form->parse($forms[$index],"http://www.nowhere.org");

    my $filler = $f->add_filler($name,$class,@$args);
    isa_ok($filler, "WWW::Mechanize::FormFiller::Value::$class");
    $f->fill_form($form);
    my @filled_inputs;
    if (ref $name and UNIVERSAL::isa( $name,'Regexp')) {
	    @filled_inputs = grep { $_->name =~ $name } $form->inputs;
    } else {
      @filled_inputs = $form->find_input($name);
    };
    for my $input (@filled_inputs) {
      is($input->value,$expected,"Modified the expected field for page $index/$name ($class:".join(":",@$args).")");
    };
  };
};

__DATA__
<html><head><title>Text box</title></head>
<body>
<form>
  <input type="text" name="text_input_1" value="">
  <input type="text" name="secondary" value="">
</form>
</body>
</html>
---
<html><head><title>Text box (prefilled)</title></head>
<body>
<form>
  <input type="text" name="text_input_2" value="xxx">
</form>
</body>
</html>
---
<html><head><title>Radio box</title></head>
<body>
<form>
  <input type=radio name=radio value="0">
  <input type=radio name=radio value="1" checked>
  <input type=radio name=radio value="2" >
  <input type=radio name=radio value="3" >
</form>
</body>
</html>
---
<html><head><title>Readonly checkbox</title></head>
<body>
<form>
  <input type=checkbox name=checkbox_1 checked=1>
</form>
</body>
</html>
---
<html><head><title>Readonly checkbox</title></head>
<body>
<form>
  <input type=checkbox name=checkbox_2>
</form>
</body>
</html>