-
Notifications
You must be signed in to change notification settings - Fork 23
/
stuff_inputs.t
174 lines (145 loc) · 4.51 KB
/
stuff_inputs.t
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!perl -Tw
use strict;
use warnings;
use Test::More tests => 44;
use URI::file;
BEGIN {
use_ok( 'Test::WWW::Mechanize' );
}
my $mech = Test::WWW::Mechanize->new();
my $uri = URI::file->new_abs( 't/stuff_inputs.html' )->as_string;
EMPTY_FIELDS: {
$mech->get( $uri );
ok( $mech->success, "Fetched $uri" ) or die q{Can't get test page};
add_test_fields( $mech );
$mech->stuff_inputs();
field_checks(
$mech, {
text0 => '',
text1 => '@',
text10 => '@' x 10,
text70k => '@' x 70000,
textunlimited => '@' x 66000,
textarea => '@' x 66000,
},
'filling empty fields'
);
}
MULTICHAR_FILL: {
$mech->get( $uri );
ok( $mech->success, "Fetched $uri" ) or die q{Can't get test page};
add_test_fields( $mech );
$mech->stuff_inputs( { fill => '123' } );
field_checks(
$mech, {
text0 => '',
text1 => '1',
text10 => '1231231231',
text70k => '123' x 23333 . '1',
textunlimited => '123' x 22000,
textarea => '123' x 22000,
},
'multichar_fill'
);
}
OVERWRITE: {
$mech->get( $uri );
ok( $mech->success, "Fetched $uri" ) or die q{Can't get test page};
add_test_fields( $mech );
$mech->stuff_inputs();
is( $mech->value('text10'), '@' x 10, 'overwriting fields: initial fill as expected' );
$mech->stuff_inputs( { fill => 'X' } );
field_checks(
$mech, {
text0 => '',
text1 => 'X',
text10 => 'X' x 10,
text70k => 'X' x 70000,
textunlimited => 'X' x 66000,
textarea => 'X' x 66000,
},
'overwriting fields'
);
}
CUSTOM_FILL: {
$mech->get( $uri );
ok( $mech->success, "Fetched $uri" ) or die q{Can't get test page};
add_test_fields( $mech );
$mech->stuff_inputs( {
fill => 'z',
specs => {
text10 => { fill=>'#' },
textarea => { fill=>'*' },
}
} );
field_checks(
$mech, {
text0 => '',
text1 => 'z',
text10 => '#' x 10,
text70k => 'z' x 70000,
textunlimited => 'z' x 66000,
textarea => '*' x 66000,
},
'custom fill'
);
}
MAXLENGTH: {
$mech->get( $uri );
ok( $mech->success, "Fetched $uri" ) or die q{Can't get test page};
add_test_fields( $mech );
$mech->stuff_inputs( {
specs => {
text10 => { maxlength=>7 },
textarea => { fill=>'*', maxlength=>9 },
}
}
);
field_checks(
$mech, {
text0 => '',
text1 => '@',
text10 => '@' x 7,
text70k => '@' x 70000,
textunlimited => '@' x 66000,
textarea => '*' x 9,
},
'maxlength'
);
}
IGNORE: {
$mech->get( $uri );
ok( $mech->success, "Fetched $uri" ) or die q{Can't get test page};
add_test_fields( $mech );
$mech->stuff_inputs( { ignore => [ 'text10' ] } );
field_checks(
$mech, {
text0 => '',
text1 => '@',
text10 => undef,
text70k => '@' x 70000,
textunlimited => '@' x 66000,
textarea => '@' x 66000,
},
'ignore'
);
}
sub add_test_fields {
my $mech = shift;
HTML::Form::Input->new( type=>'text', name=>'text0', maxlength=>0 )->add_to_form( $mech->current_form() );
HTML::Form::Input->new( type=>'text', name=>'text1', maxlength=>1 )->add_to_form( $mech->current_form() );
HTML::Form::Input->new( type=>'text', name=>'text10', maxlength=>10 )->add_to_form( $mech->current_form() );
HTML::Form::Input->new( type=>'text', name=>'text70k', maxlength=>70000 )->add_to_form( $mech->current_form() );
HTML::Form::Input->new( type=>'text', name=>'textunlimited' )->add_to_form( $mech->current_form() );
HTML::Form::Input->new( type=>'textarea', name=>'textarea' )->add_to_form( $mech->current_form() );
return;
}
sub field_checks {
my $mech = shift;
my $expected = shift;
my $desc = shift;
foreach my $key ( qw( text0 text1 text10 text70k textunlimited textarea ) ) {
is( $mech->value($key), $expected->{$key}, "$desc: field $key" );
}
return;
}