1Test::WWW::Mechanize::CUastearlyCsotn(t3r)ibuted Perl DoTceusmte:n:tWaWtWi:o:nMechanize::Catalyst(3)
2
3
4
6 Test::WWW::Mechanize::Catalyst - Test::WWW::Mechanize for Catalyst
7
9 # We're in a t/*.t test script...
10 # To test a Catalyst application named 'Catty':
11 use Test::WWW::Mechanize::Catalyst 'Catty';
12
13 my $mech = Test::WWW::Mechanize::Catalyst->new;
14 $mech->get_ok("/"); # no hostname needed
15 is($mech->ct, "text/html");
16 $mech->title_is("Root", "On the root page");
17 $mech->content_contains("This is the root page", "Correct content");
18 $mech->follow_link_ok({text => 'Hello'}, "Click on Hello");
19 # ... and all other Test::WWW::Mechanize methods
20
22 Catalyst is an elegant MVC Web Application Framework. Test::WWW::Mecha‐
23 nize is a subclass of WWW::Mechanize that incorporates features for web
24 application testing. The Test::WWW::Mechanize::Catalyst module meshes
25 the two to allow easy testing of Catalyst applications without starting
26 up a web server.
27
28 Testing web applications has always been a bit tricky, normally start‐
29 ing a web server for your application and making real HTTP requests to
30 it. This module allows you to test Catalyst web applications but does
31 not start a server or issue HTTP requests. Instead, it passes the HTTP
32 request object directly to Catalyst. Thus you do not need to use a real
33 hostname: "http://localhost/" will do. However, this is optional. The
34 following two lines of code do exactly the same thing:
35
36 $mech->get_ok('/action');
37 $mech->get_ok('http://localhost/action');
38
39 Links which do not begin with / or are not for localhost can be handled
40 as normal Web requests - this is handy if you have an external single
41 sign-on system. You must set allow_external to true for this:
42
43 $m->allow_external(1);
44
45 You can also test a remote server by setting the environment variable
46 CATALYST_SERVER, for example:
47
48 $ CATALYST_SERVER=http://example.com/myapp prove -l t
49
50 will run the same tests on the application running at http://exam‐
51 ple.com/myapp regardless of whether or not you specify http:://local‐
52 host for Test::WWW::Mechanize::Catalyst.
53
54 This makes testing fast and easy. Test::WWW::Mechanize provides func‐
55 tions for common web testing scenarios. For example:
56
57 $mech->get_ok( $page );
58 $mech->title_is( "Invoice Status", "Make sure we're on the invoice page" );
59 $mech->content_contains( "Andy Lester", "My name somewhere" );
60 $mech->content_like( qr/(cpan⎪perl)\.org/, "Link to perl.org or CPAN" );
61
62 This module supports cookies automatically.
63
64 To use this module you must pass it the name of the application. See
65 the SYNOPSIS above.
66
67 Note that Catalyst has a special developing feature: the debug screen.
68 By default this module will treat responses which are the debug screen
69 as failures. If you actually want to test debug screens, please use:
70
71 $m->{catalyst_debug} = 1;
72
73 An alternative to this module is Catalyst::Test.
74
76 new
77
78 Behaves like, and calls, WWW::Mechanize's "new" method. Any parms
79 passed in get passed to WWW::Mechanize's constructor. Note that we need
80 to pass the name of the Catalyst application to the "use":
81
82 use Test::WWW::Mechanize::Catalyst 'Catty';
83 my $mech = Test::WWW::Mechanize::Catalyst->new;
84
86 allow_external
87
88 Links which do not begin with / or are not for localhost can be handled
89 as normal Web requests - this is handy if you have an external single
90 sign-on system. You must set allow_external to true for this:
91
92 $m->allow_external(1);
93
94 $mech->get_ok($url, [ \%LWP_options ,] $desc)
95
96 A wrapper around WWW::Mechanize's get(), with similar options, except
97 the second argument needs to be a hash reference, not a hash. Returns
98 true or false.
99
100 $mech->title_is( $str [, $desc ] )
101
102 Tells if the title of the page is the given string.
103
104 $mech->title_is( "Invoice Summary" );
105
106 $mech->title_like( $regex [, $desc ] )
107
108 Tells if the title of the page matches the given regex.
109
110 $mech->title_like( qr/Invoices for (.+)/
111
112 $mech->title_unlike( $regex [, $desc ] )
113
114 Tells if the title of the page matches the given regex.
115
116 $mech->title_unlike( qr/Invoices for (.+)/
117
118 $mech->content_is( $str [, $desc ] )
119
120 Tells if the content of the page matches the given string
121
122 $mech->content_contains( $str [, $desc ] )
123
124 Tells if the content of the page contains $str.
125
126 $mech->content_lacks( $str [, $desc ] )
127
128 Tells if the content of the page lacks $str.
129
130 $mech->content_like( $regex [, $desc ] )
131
132 Tells if the content of the page matches $regex.
133
134 $mech->content_unlike( $regex [, $desc ] )
135
136 Tells if the content of the page does NOT match $regex.
137
138 $mech->page_links_ok( [ $desc ] )
139
140 Follow all links on the current page and test for HTTP status 200
141
142 $mech->page_links_ok('Check all links');
143
144 $mech->page_links_content_like( $regex,[ $desc ] )
145
146 Follow all links on the current page and test their contents for
147 $regex.
148
149 $mech->page_links_content_like( qr/foo/,
150 'Check all links contain "foo"' );
151
152 $mech->page_links_content_unlike( $regex,[ $desc ] )
153
154 Follow all links on the current page and test their contents do not
155 contain the specified regex.
156
157 $mech->page_links_content_unlike(qr/Restricted/,
158 'Check all links do not contain Restricted');
159
160 $mech->links_ok( $links [, $desc ] )
161
162 Check the current page for specified links and test for HTTP status
163 200. The links may be specified as a reference to an array containing
164 WWW::Mechanize::Link objects, an array of URLs, or a scalar URL name.
165
166 my @links = $mech->find_all_links( url_regex => qr/cnn\.com$/ );
167 $mech->links_ok( \@links, 'Check all links for cnn.com' );
168
169 my @links = qw( index.html search.html about.html );
170 $mech->links_ok( \@links, 'Check main links' );
171
172 $mech->links_ok( 'index.html', 'Check link to index' );
173
174 $mech->link_status_is( $links, $status [, $desc ] )
175
176 Check the current page for specified links and test for HTTP status
177 passed. The links may be specified as a reference to an array contain‐
178 ing WWW::Mechanize::Link objects, an array of URLs, or a scalar URL
179 name.
180
181 my @links = $mech->links();
182 $mech->link_status_is( \@links, 403,
183 'Check all links are restricted' );
184
185 $mech->link_status_isnt( $links, $status [, $desc ] )
186
187 Check the current page for specified links and test for HTTP status
188 passed. The links may be specified as a reference to an array contain‐
189 ing WWW::Mechanize::Link objects, an array of URLs, or a scalar URL
190 name.
191
192 my @links = $mech->links();
193 $mech->link_status_isnt( \@links, 404,
194 'Check all links are not 404' );
195
196 $mech->link_content_like( $links, $regex [, $desc ] )
197
198 Check the current page for specified links and test the content of each
199 against $regex. The links may be specified as a reference to an array
200 containing WWW::Mechanize::Link objects, an array of URLs, or a scalar
201 URL name.
202
203 my @links = $mech->links();
204 $mech->link_content_like( \@links, qr/Restricted/,
205 'Check all links are restricted' );
206
207 $mech->link_content_unlike( $links, $regex [, $desc ] )
208
209 Check the current page for specified links and test the content of each
210 does not match $regex. The links may be specified as a reference to an
211 array containing WWW::Mechanize::Link objects, an array of URLs, or a
212 scalar URL name.
213
214 my @links = $mech->links();
215 $mech->link_content_like( \@links, qr/Restricted/,
216 'Check all links are restricted' );
217
218 follow_link_ok( \%parms [, $comment] )
219
220 Makes a "follow_link()" call and executes tests on the results. The
221 link must be found, and then followed successfully. Otherwise, this
222 test fails.
223
224 %parms is a hashref containing the parms to pass to "follow_link()".
225 Note that the parms to "follow_link()" are a hash whereas the parms to
226 this function are a hashref. You have to call this function like:
227
228 $agent->follow_like_ok( {n=>3}, "looking for 3rd link" );
229
230 As with other test functions, $comment is optional. If it is supplied
231 then it will display when running the test harness in verbose mode.
232
233 Returns true value if the specified link was found and followed suc‐
234 cessfully. The HTTP::Response object returned by follow_link() is not
235 available.
236
238 Related modules which may be of interest: Catalyst, Test::WWW::Mecha‐
239 nize, WWW::Mechanize.
240
242 Leon Brocard, "<acme@astray.com>"
243
245 Copyright (C) 2005-7, Leon Brocard
246
247 This module is free software; you can redistribute it or modify it
248 under the same terms as Perl itself.
249
250
251
252perl v5.8.8 2007-09-17 Test::WWW::Mechanize::Catalyst(3)