1Test::WWW::Mechanize::PUSsGeIr(3C)ontributed Perl DocumeTnetsatt:i:oWnWW::Mechanize::PSGI(3)
2
3
4
6 Test::WWW::Mechanize::PSGI - Test PSGI programs using WWW::Mechanize
7
9 # We're in a t/*.t test script...
10 use Test::WWW::Mechanize::PSGI;
11
12 my $mech = Test::WWW::Mechanize::PSGI->new(
13 app => sub {
14 my $env = shift;
15 return [
16 200,
17 [ 'Content-Type' => 'text/html' ],
18 [ '<html><head><title>Hi</title></head><body>Hello World</body></html>'
19 ]
20 ];
21 },
22 );
23 $mech->get_ok('/');
24 is( $mech->ct, 'text/html', 'Is text/html' );
25 $mech->title_is('Hi');
26 $mech->content_contains('Hello World');
27 # ... and all other Test::WWW::Mechanize methods
28
30 PSGI is a specification to decouple web server environments from web
31 application framework code. Test::WWW::Mechanize is a subclass of
32 WWW::Mechanize that incorporates features for web application testing.
33 The Test::WWW::Mechanize::PSGI module meshes the two to allow easy
34 testing of PSGI applications.
35
36 Testing web applications has always been a bit tricky, normally
37 requiring starting a web server for your application and making real
38 HTTP requests to it. This module allows you to test PSGI web
39 applications but does not require a server or issue HTTP requests.
40 Instead, it passes the HTTP request object directly to PSGI. Thus you
41 do not need to use a real hostname: "http://localhost/" will do.
42 However, this is optional. The following two lines of code do exactly
43 the same thing:
44
45 $mech->get_ok('/action');
46 $mech->get_ok('http://localhost/action');
47
48 This makes testing fast and easy. Test::WWW::Mechanize provides
49 functions for common web testing scenarios. For example:
50
51 $mech->get_ok( $page );
52 $mech->title_is( "Invoice Status", "Make sure we're on the invoice page" );
53 $mech->content_contains( "Andy Lester", "My name somewhere" );
54 $mech->content_like( qr/(cpan|perl)\.org/, "Link to perl.org or CPAN" );
55
56 An alternative to this module is Plack::Test.
57
59 new
60 Behaves like, and calls, WWW::Mechanize's "new" method. You should pass
61 in your application:
62
63 my $mech = Test::WWW::Mechanize::PSGI->new(
64 app => sub {
65 my $env = shift;
66 return [ 200, [ 'Content-Type' => 'text/plain' ], ['Hello World'] ],;
67 },
68 );
69
71 $mech->get_ok($url, [ \%LWP_options ,] $desc)
72 A wrapper around WWW::Mechanize's get(), with similar options, except
73 the second argument needs to be a hash reference, not a hash. Like
74 well-behaved "*_ok()" functions, it returns true if the test passed, or
75 false if not.
76
77 A default description of "GET $url" is used if none if provided.
78
79 $mech->head_ok($url, [ \%LWP_options ,] $desc)
80 A wrapper around WWW::Mechanize's head(), with similar options, except
81 the second argument needs to be a hash reference, not a hash. Like
82 well-behaved "*_ok()" functions, it returns true if the test passed, or
83 false if not.
84
85 A default description of "HEAD $url" is used if none if provided.
86
87 $mech->post_ok( $url, [ \%LWP_options ,] $desc )
88 A wrapper around WWW::Mechanize's post(), with similar options, except
89 the second argument needs to be a hash reference, not a hash. Like
90 well-behaved "*_ok()" functions, it returns true if the test passed, or
91 false if not.
92
93 A default description of "POST to $url" is used if none if provided.
94
95 $mech->put_ok( $url, [ \%LWP_options ,] $desc )
96 A wrapper around WWW::Mechanize's put(), with similar options, except
97 the second argument needs to be a hash reference, not a hash. Like
98 well-behaved "*_ok()" functions, it returns true if the test passed, or
99 false if not.
100
101 A default description of "PUT to $url" is used if none if provided.
102
103 $mech->submit_form_ok( \%parms [, $desc] )
104 Makes a "submit_form()" call and executes tests on the results. The
105 form must be found, and then submitted successfully. Otherwise, this
106 test fails.
107
108 %parms is a hashref containing the parms to pass to "submit_form()".
109 Note that the parms to "submit_form()" are a hash whereas the parms to
110 this function are a hashref. You have to call this function like:
111
112 $agent->submit_form_ok( {n=>3}, "looking for 3rd link" );
113
114 As with other test functions, $desc is optional. If it is supplied
115 then it will display when running the test harness in verbose mode.
116
117 Returns true value if the specified link was found and followed
118 successfully. The HTTP::Response object returned by submit_form() is
119 not available.
120
121 $mech->follow_link_ok( \%parms [, $desc] )
122 Makes a "follow_link()" call and executes tests on the results. The
123 link must be found, and then followed successfully. Otherwise, this
124 test fails.
125
126 %parms is a hashref containing the parms to pass to "follow_link()".
127 Note that the parms to "follow_link()" are a hash whereas the parms to
128 this function are a hashref. You have to call this function like:
129
130 $mech->follow_link_ok( {n=>3}, "looking for 3rd link" );
131
132 As with other test functions, $desc is optional. If it is supplied
133 then it will display when running the test harness in verbose mode.
134
135 Returns a true value if the specified link was found and followed
136 successfully. The HTTP::Response object returned by follow_link() is
137 not available.
138
139 click_ok( $button[, $desc] )
140 Clicks the button named by $button. An optional $desc can be given for
141 the test.
142
144 $mech->html_lint_ok( [$desc] )
145 Checks the validity of the HTML on the current page. If the page is
146 not HTML, then it fails. The URI is automatically appended to the
147 $desc.
148
149 Note that HTML::Lint must be installed for this to work. Otherwise, it
150 will blow up.
151
152 $mech->title_is( $str [, $desc ] )
153 Tells if the title of the page is the given string.
154
155 $mech->title_is( "Invoice Summary" );
156
157 $mech->title_like( $regex [, $desc ] )
158 Tells if the title of the page matches the given regex.
159
160 $mech->title_like( qr/Invoices for (.+)/
161
162 $mech->title_unlike( $regex [, $desc ] )
163 Tells if the title of the page matches the given regex.
164
165 $mech->title_unlike( qr/Invoices for (.+)/
166
167 $mech->base_is( $str [, $desc ] )
168 Tells if the base of the page is the given string.
169
170 $mech->base_is( "http://example.com/" );
171
172 $mech->base_like( $regex [, $desc ] )
173 Tells if the base of the page matches the given regex.
174
175 $mech->base_like( qr{http://example.com/index.php?PHPSESSID=(.+)});
176
177 $mech->base_unlike( $regex [, $desc ] )
178 Tells if the base of the page matches the given regex.
179
180 $mech->base_unlike( qr{http://example.com/index.php?PHPSESSID=(.+)});
181
182 $mech->content_is( $str [, $desc ] )
183 Tells if the content of the page matches the given string
184
185 $mech->content_contains( $str [, $desc ] )
186 Tells if the content of the page contains $str.
187
188 $mech->content_lacks( $str [, $desc ] )
189 Tells if the content of the page lacks $str.
190
191 $mech->content_like( $regex [, $desc ] )
192 Tells if the content of the page matches $regex.
193
194 $mech->content_unlike( $regex [, $desc ] )
195 Tells if the content of the page does NOT match $regex.
196
197 $mech->has_tag( $tag, $text [, $desc ] )
198 Tells if the page has a $tag tag with the given content in its text.
199
200 $mech->has_tag_like( $tag, $regex [, $desc ] )
201 Tells if the page has a $tag tag with the given content in its text.
202
203 $mech->followable_links()
204 Returns a list of links that Mech can follow. This is only http and
205 https links.
206
207 $mech->page_links_ok( [ $desc ] )
208 Follow all links on the current page and test for HTTP status 200
209
210 $mech->page_links_ok('Check all links');
211
212 $mech->page_links_content_like( $regex [, $desc ] )
213 Follow all links on the current page and test their contents for
214 $regex.
215
216 $mech->page_links_content_like( qr/foo/,
217 'Check all links contain "foo"' );
218
219 $mech->links_ok( $links [, $desc ] )
220 Follow specified links on the current page and test for HTTP status
221 200. The links may be specified as a reference to an array containing
222 WWW::Mechanize::Link objects, an array of URLs, or a scalar URL name.
223
224 my @links = $mech->find_all_links( url_regex => qr/cnn\.com$/ );
225 $mech->links_ok( \@links, 'Check all links for cnn.com' );
226
227 my @links = qw( index.html search.html about.html );
228 $mech->links_ok( \@links, 'Check main links' );
229
230 $mech->links_ok( 'index.html', 'Check link to index' );
231
232 $mech->link_status_is( $links, $status [, $desc ] )
233 Follow specified links on the current page and test for HTTP status
234 passed. The links may be specified as a reference to an array
235 containing WWW::Mechanize::Link objects, an array of URLs, or a scalar
236 URL name.
237
238 my @links = $mech->followable_links();
239 $mech->link_status_is( \@links, 403,
240 'Check all links are restricted' );
241
242 $mech->link_status_isnt( $links, $status [, $desc ] )
243 Follow specified links on the current page and test for HTTP status
244 passed. The links may be specified as a reference to an array
245 containing WWW::Mechanize::Link objects, an array of URLs, or a scalar
246 URL name.
247
248 my @links = $mech->followable_links();
249 $mech->link_status_isnt( \@links, 404,
250 'Check all links are not 404' );
251
252 $mech->link_content_like( $links, $regex [, $desc ] )
253 Follow specified links on the current page and test the resulting
254 content of each against $regex. The links may be specified as a
255 reference to an array containing WWW::Mechanize::Link objects, an array
256 of URLs, or a scalar URL name.
257
258 my @links = $mech->followable_links();
259 $mech->link_content_like( \@links, qr/Restricted/,
260 'Check all links are restricted' );
261
262 $mech->link_content_unlike( $links, $regex [, $desc ] )
263 Follow specified links on the current page and test that the resulting
264 content of each does not match $regex. The links may be specified as a
265 reference to an array containing WWW::Mechanize::Link objects, an array
266 of URLs, or a scalar URL name.
267
268 my @links = $mech->followable_links();
269 $mech->link_content_unlike( \@links, qr/Restricted/,
270 'No restricted links' );
271
272 $mech->stuff_inputs( [\%options] )
273 Finds all free-text input fields (text, textarea, and password) in the
274 current form and fills them to their maximum length in hopes of finding
275 application code that can't handle it. Fields with no maximum length
276 and all textarea fields are set to 66000 bytes, which will often be
277 enough to overflow the data's eventual recepticle.
278
279 There is no return value.
280
281 If there is no current form then nothing is done.
282
283 The hashref $options can contain the following keys:
284
285 · ignore
286
287 hash value is arrayref of field names to not touch, e.g.:
288
289 $mech->stuff_inputs( {
290 ignore => [qw( specialfield1 specialfield2 )],
291 } );
292
293 · fill
294
295 hash value is default string to use when stuffing fields. Copies
296 of the string are repeated up to the max length of each field.
297 E.g.:
298
299 $mech->stuff_inputs( {
300 fill => '@' # stuff all fields with something easy to recognize
301 } );
302
303 · specs
304
305 hash value is arrayref of hashrefs with which you can pass detailed
306 instructions about how to stuff a given field. E.g.:
307
308 $mech->stuff_inputs( {
309 specs=>{
310 # Some fields are datatype-constrained. It's most common to
311 # want the field stuffed with valid data.
312 widget_quantity => { fill=>'9' },
313 notes => { maxlength=>2000 },
314 }
315 } );
316
317 The specs allowed are fill (use this fill for the field rather than
318 the default) and maxlength (use this as the field's maxlength
319 instead of any maxlength specified in the HTML).
320
322 Leon Brocard <acme@astray.com>.
323
325 Copyright (C) 2009, Leon Brocard
326
328 This module is free software; you can redistribute it or modify it
329 under the same terms as Perl itself.
330
331
332
333perl v5.12.2 2010-01-06 Test::WWW::Mechanize::PSGI(3)