File: form_with_fields.t

package info (click to toggle)
libwww-mechanize-perl 2.20-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,080 kB
  • sloc: perl: 4,623; makefile: 6
file content (292 lines) | stat: -rw-r--r-- 6,933 bytes parent folder | download
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!perl

use warnings;
use strict;
use Test::More 'no_plan';
use Test::Fatal qw( exception );
use Test::Warnings ':all';
use Test::Deep qw( array_each cmp_deeply code re );
use URI::file  ();

BEGIN {
    use_ok('WWW::Mechanize');
}

my $mech = WWW::Mechanize->new( cookie_jar => undef, autocheck => 0 );
isa_ok( $mech, 'WWW::Mechanize' );
my $uri = URI::file->new_abs('t/form_with_fields.html')->as_string;

$mech->get($uri);
ok( $mech->success, "Fetched $uri" ) or die q{Can't get test page};

{
    my $test = 'dies with no input';
    like(
        exception { my $form = $mech->form_with_fields(); },
        qr/no fields provided/,
        $test,
    );
}

{
    my $form;
    cmp_deeply(
        [ warnings { $form = $mech->form_with_fields(qw/1b/) } ],
        [
            re(
                qr/There are 2 forms with the named fields.  The first one was used./
            )
        ],
        'warning on ambiguous match (1)',
    );
    isa_ok( $form, 'HTML::Form' );
    is( $form->attr('name'), '1st_form', 'first form matches' );
}

{
    my $form = $mech->form_with_fields( '1b', 'opt[2]' );
    isa_ok( $form, 'HTML::Form' );
    is( $form->attr('name'), '2nd_form', 'second form matches' );
}

{
    my $form;
    cmp_deeply(
        [ warnings { $form = $mech->form_with_fields( '4a', '4b' ) } ],
        [
            re(
                qr/There are 2 forms with the named fields.  The first one was used./
            )
        ],
        'warning on ambiguous match (2)',
    );
    isa_ok( $form, 'HTML::Form' );
    is( $form->attr('name'), '4th_form_1', 'fourth form matches' );
}

{
    my $form = $mech->form_with_fields( '4a', '4b', { n => 1 } );
    isa_ok( $form, 'HTML::Form' );
    is( $form->attr('name'), '4th_form_1', 'fourth form match 1 matches' );
}

{
    my $form = $mech->form_with_fields( '4a', '4b', { n => 2 } );
    isa_ok( $form, 'HTML::Form' );
    is( $form->attr('name'), '4th_form_2', 'fourth form match 2 matches' );
}

{
    my $form;
    cmp_deeply(
        [
            warnings {
                $form = $mech->form_with_fields( '4a', '4b', { n => 3 } )
            }
        ],
        [ re(qr/There is no match #3 form with the requested fields/) ],
        'warning on unmatched nth form',
    );
    is( $form, undef, 'undef on unmatched nth form' );
}

{
    my @forms = $mech->all_forms_with( name => '3rd_form_ambiguous' );
    is( scalar @forms, 2, 'all_forms_with finds similar forms' );
    isa_ok( $forms[0], 'HTML::Form' );
    isa_ok( $forms[1], 'HTML::Form' );
    is(
        $forms[0]->attr('name'), '3rd_form_ambiguous',
        'first result of 3rd_form_ambiguous'
    );
    is(
        $forms[1]->attr('name'), '3rd_form_ambiguous',
        'second result of 3rd_form_ambiguous'
    );
}

{
    my @forms = $mech->all_forms_with( action => 'http://localhost/' );
    is( scalar @forms, 7, 'all_forms_with action finds all 7 forms' );
    cmp_deeply(
        \@forms,
        array_each(
            code(
                # one of the forms is missing the trailing slash, we
                # should not have it
                sub {
                    $_[0]->action eq 'http://localhost/';
                }
            )
        ),
        '... and all of them have the correct action'
    );
}

{
    $mech->get($uri);
    like(
        exception {
            $mech->submit_form(
                with_fields => { 'xx' => q{} },
            );
        },
        qr/There is no form with the requested fields/,
        'submit_form with no match (1)',
    );
}

{
    $mech->get($uri);
    like(
        exception {
            $mech->submit_form(
                with_fields => { '1a' => q{} },
                form_number => 2,
            );
        },
        qr/There is no form that satisfies all the criteria/,
        'submit_form with no match (2)',
    );
}

{
    $mech->get($uri);
    like(
        exception {
            $mech->submit_form(
                form_number => 2,
                form_name   => '3rd_form_ambiguous',
            );
        },
        qr/There is no form that satisfies all the criteria/,
        'submit_form with no match (3)',
    );
}

{
    $mech->get($uri);
    like(
        exception {
            $mech->submit_form(
                form_name => '3rd_form_ambiguous',
            );
        },
        qr/More than one form satisfies all the criteria/,
        'submit_form with more than one match',
    );
}

{
    $mech->get($uri);
    is(
        exception {
            $mech->submit_form(
                with_fields => { 'x' => q{} },
                form_name   => '3rd_form_ambiguous',
            );
        },
        undef,
        'submit_form with intersection of two criteria',
    );
}

{
    $mech->get($uri);
    is(
        exception {
            $mech->submit_form(
                with_fields => { '1b' => q{}, 'opt[2]' => q{} },
            );
        },
        undef,
        ' submit_form( with_fields => %data ) ',
    );
}

{
    $mech->get($uri);
    is(
        exception {
            $mech->submit_form(
                form_name => '1st_form',
                fields    => {
                    '1c' => 'madeup_field',
                },
            );
        },
        undef,
        'submit_form with invalid field and without strict_forms option succeeds',
    );
}

{
    $mech->get($uri);
    like(
        exception {
            $mech->submit_form(
                form_name => '1st_form',
                fields    => {
                    '1c' => 'madeup_field',
                },
                strict_forms => 1,
            );
        },
        qr/^No such field '1c'/,
        'submit_form with invalid field and strict_forms option fails',
    );
}

{
    $mech->get($uri);
    is(
        exception {
            $mech->submit_form(
                form_name => '1st_form',
                fields    => {
                    '1a' => 'value1',
                    '1b' => 'value2',
                },
                strict_forms => 1,
            );
        },
        undef,
        'submit_form with valid fields and strict_forms option succeeds',
    );
}

{
    $mech->get($uri);
    my $form = $mech->form_name('6th_form');

    is(
        exception {
            $mech->submit_form(
                with_fields => { 'select' => \1 },
            );
        },
        undef,
        'submit_form with valid field and ref to first value of select'
    );

    is $form->value('select'), 'two',
        '... and the second (index 1) value was set';
}

{
    $mech->get($uri);
    my $form = $mech->form_name('6th_form');

    is(
        exception {
            $mech->submit_form(
                with_fields => { 'radio' => \-1 },
            );
        },
        undef,
        'submit_form with valid field and ref to last value of radio'
    );

    is $form->value('radio'), 'baz',
        '... and the last (index -1) value was set';
}