Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<html>
2
<head>
3
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
4
<title>Simple Test for PHP web script testing documentation</title>
5
<link rel="stylesheet" type="text/css" href="docs.css" title="Styles">
6
</head>
7
<body>
8
<div class="menu_back">
9
<div class="menu">
10
<h2>
11
<a href="index.html">SimpleTest</a>
12
</h2>
13
<ul>
14
<li>
15
<a href="overview.html">Overview</a>
16
</li>
17
<li>
18
<a href="unit_test_documentation.html">Unit tester</a>
19
</li>
20
<li>
21
<a href="group_test_documentation.html">Group tests</a>
22
</li>
23
<li>
24
<a href="mock_objects_documentation.html">Mock objects</a>
25
</li>
26
<li>
27
<a href="partial_mocks_documentation.html">Partial mocks</a>
28
</li>
29
<li>
30
<a href="reporter_documentation.html">Reporting</a>
31
</li>
32
<li>
33
<a href="expectation_documentation.html">Expectations</a>
34
</li>
35
<li>
36
<span class="chosen">Web tester</span>
37
</li>
38
<li>
39
<a href="form_testing_documentation.html">Testing forms</a>
40
</li>
41
<li>
42
<a href="authentication_documentation.html">Authentication</a>
43
</li>
44
<li>
45
<a href="browser_documentation.html">Scriptable browser</a>
46
</li>
47
</ul>
48
</div>
49
</div>
50
<h1>Web tester documentation</h1>
51
<div class="content">
52
        <p>
53
<a class="target" name="fetch">
54
<h2>Fetching a page</h2>
55
</a>
56
</p>
57
            <p>
58
                Testing classes is all very well, but PHP is predominately
59
                a language for creating functionality within web pages.
60
                How do we test the front end presentation role of our PHP
61
                applications?
62
                Well the web pages are just text, so we should be able to
63
                examine them just like any other test data.
64
            </p>
65
            <p>
66
                This leads to a tricky issue.
67
                If we test at too low a level, testing for matching tags
68
                in the page with pattern matching for example, our tests will
69
                be brittle.
70
                The slightest change in layout could break a large number of
71
                tests.
72
                If we test at too high a level, say using mock versions of a
73
                template engine, then we lose the ability to automate some classes
74
                of test.
75
                For example, the interaction of forms and navigation will
76
                have to be tested manually.
77
                These types of test are extremely repetitive and error prone.
78
            </p>
79
            <p>
80
                SimpleTest includes a special form of test case for the testing
81
                of web page actions.
82
                The <span class="new_code">WebTestCase</span> includes facilities
83
                for navigation, content and cookie checks and form handling.
84
                Usage of these test cases is similar to the
85
                <a href="unit_tester_documentation.html">UnitTestCase</a>...
86
<pre>
87
<strong>class TestOfLastcraft extends WebTestCase {
88
}</strong>
89
</pre>
90
                Here we are about to test the
91
                <a href="http://www/lastcraft.com/">Last Craft</a> site itself.
92
                If this test case is in a file called <em>lastcraft_test.php</em>
93
                then it can be loaded in a runner script just like unit tests...
94
<pre>
95
&lt;?php<strong>
96
    require_once('simpletest/web_tester.php');</strong>
97
    require_once('simpletest/reporter.php');
98
 
99
    $test = &amp;new GroupTest('Web site tests');<strong>
100
    $test-&gt;addTestFile('lastcraft_test.php');</strong>
101
    exit ($test-&gt;run(new TextReporter()) ? 0 : 1);
102
?&gt;
103
</pre>
104
                I am using the text reporter here to more clearly
105
                distinguish the web content from the test output.
106
            </p>
107
            <p>
108
                Nothing is being tested yet.
109
                We can fetch the home page by using the
110
                <span class="new_code">get()</span> method...
111
<pre>
112
class TestOfLastcraft extends WebTestCase {
113
    <strong>
114
    function testHomepage() {
115
        $this-&gt;assertTrue($this-&gt;get('http://www.lastcraft.com/'));
116
    }</strong>
117
}
118
</pre>
119
                The <span class="new_code">get()</span> method will
120
                return true only if page content was successfully
121
                loaded.
122
                It is a simple, but crude way to check that a web page
123
                was actually delivered by the web server.
124
                However that content may be a 404 response and yet
125
                our <span class="new_code">get()</span> method will still return true.
126
            </p>
127
            <p>
128
                Assuming that the web server for the Last Craft site is up
129
                (sadly not always the case), we should see...
130
<pre class="shell">
131
Web site tests
132
OK
133
Test cases run: 1/1, Failures: 0, Exceptions: 0
134
</pre>
135
                All we have really checked is that any kind of page was
136
                returned.
137
                We don't yet know if it was the right one.
138
            </p>
139
 
140
        <p>
141
<a class="target" name="content">
142
<h2>Testing page content</h2>
143
</a>
144
</p>
145
            <p>
146
                To confirm that the page we think we are on is actually the
147
                page we are on, we need to verify the page content.
148
<pre>
149
class TestOfLastcraft extends WebTestCase {
150
 
151
    function testHomepage() {<strong>
152
        $this-&gt;get('http://www.lastcraft.com/');
153
        $this-&gt;assertTest('Why the last craft');</strong>
154
    }
155
}
156
</pre>
157
                The page from the last fetch is held in a buffer in
158
                the test case, so there is no need to refer to it directly.
159
                The pattern match is always made against the buffer.
160
            </p>
161
            <p>
162
                Here is the list of possible content assertions...
163
                <table>
164
<tbody>
165
                    <tr>
166
<td><span class="new_code">assertTitle($title)</span></td><td>Pass if title is an exact match</td>
167
</tr>
168
                    <tr>
169
<td><span class="new_code">assertPattern($pattern)</span></td><td>A Perl pattern match against the page content</td>
170
</tr>
171
                    <tr>
172
<td><span class="new_code">assertNoPattern($pattern)</span></td><td>A Perl pattern match to not find content</td>
173
</tr>
174
                    <tr>
175
<td><span class="new_code">assertText($text)</span></td><td>Pass if matches visible and "alt" text</td>
176
</tr>
177
                    <tr>
178
<td><span class="new_code">assertNoText($text)</span></td><td>Pass if doesn't match visible and "alt" text</td>
179
</tr>
180
                    <tr>
181
<td><span class="new_code">assertLink($label)</span></td><td>Pass if a link with this text is present</td>
182
</tr>
183
                    <tr>
184
<td><span class="new_code">assertNoLink($label)</span></td><td>Pass if no link with this text is present</td>
185
</tr>
186
                    <tr>
187
<td><span class="new_code">assertLinkById($id)</span></td><td>Pass if a link with this id attribute is present</td>
188
</tr>
189
                    <tr>
190
<td><span class="new_code">assertNoLinkById($id)</span></td><td>Pass if no link with this id attribute is present</td>
191
</tr>
192
                    <tr>
193
<td><span class="new_code">assertField($name, $value)</span></td><td>Pass if an input tag with this name has this value</td>
194
</tr>
195
                    <tr>
196
<td><span class="new_code">assertFieldById($id, $value)</span></td><td>Pass if an input tag with this id has this value</td>
197
</tr>
198
                    <tr>
199
<td><span class="new_code">assertResponse($codes)</span></td><td>Pass if HTTP response matches this list</td>
200
</tr>
201
                    <tr>
202
<td><span class="new_code">assertMime($types)</span></td><td>Pass if MIME type is in this list</td>
203
</tr>
204
                    <tr>
205
<td><span class="new_code">assertAuthentication($protocol)</span></td><td>Pass if the current challenge is this protocol</td>
206
</tr>
207
                    <tr>
208
<td><span class="new_code">assertNoAuthentication()</span></td><td>Pass if there is no current challenge</td>
209
</tr>
210
                    <tr>
211
<td><span class="new_code">assertRealm($name)</span></td><td>Pass if the current challenge realm matches</td>
212
</tr>
213
                    <tr>
214
<td><span class="new_code">assertHeader($header, $content)</span></td><td>Pass if a header was fetched matching this value</td>
215
</tr>
216
                    <tr>
217
<td><span class="new_code">assertNoHeader($header)</span></td><td>Pass if a header was not fetched</td>
218
</tr>
219
                    <tr>
220
<td><span class="new_code">assertCookie($name, $value)</span></td><td>Pass if there is currently a matching cookie</td>
221
</tr>
222
                    <tr>
223
<td><span class="new_code">assertNoCookie($name)</span></td><td>Pass if there is currently no cookie of this name</td>
224
</tr>
225
                </tbody>
226
</table>
227
                As usual with the SimpleTest assertions, they all return
228
                false on failure and true on pass.
229
                They also allow an optional test message and you can embed
230
                the original test message inside using "%s" inside
231
                your custom message.
232
            </p>
233
            <p>
234
                So now we could instead test against the title tag with...
235
<pre>
236
<strong>$this-&gt;assertTitle('The Last Craft? Web developer tutorials on PHP, Extreme programming and Object Oriented development');</strong>
237
</pre>
238
                ...or, if that is too long and fragile...
239
<pre>
240
<strong>$this-&gt;assertTitle(new PatternExpectation('/The Last Craft/'));</strong>
241
</pre>
242
                As well as the simple HTML content checks we can check
243
                that the MIME type is in a list of allowed types with...
244
<pre>
245
<strong>$this-&gt;assertMime(array('text/plain', 'text/html'));</strong>
246
</pre>
247
                More interesting is checking the HTTP response code.
248
                Like the MIME type, we can assert that the response code
249
                is in a list of allowed values...
250
<pre>
251
class TestOfLastcraft extends WebTestCase {
252
 
253
    function testRedirects() {
254
        $this-&gt;get('http://www.lastcraft.com/test/redirect.php');
255
        $this-&gt;assertResponse(200);&lt;/strong&gt;
256
    }
257
}
258
</pre>
259
                Here we are checking that the fetch is successful by
260
                allowing only a 200 HTTP response.
261
                This test will pass, but it is not actually correct to do so.
262
                There is no page, instead the server issues a redirect.
263
                The <span class="new_code">WebTestCase</span> will
264
                automatically follow up to three such redirects.
265
                The tests are more robust this way and we are usually
266
                interested in the interaction with the pages rather
267
                than their delivery.
268
                If the redirects are of interest then this ability must
269
                be disabled...
270
<pre>
271
class TestOfLastcraft extends WebTestCase {
272
 
273
    function testHomepage() {<strong>
274
        $this-&gt;setMaximumRedirects(0);</strong>
275
        $this-&gt;get('http://www.lastcraft.com/test/redirect.php');
276
        $this-&gt;assertResponse(200);
277
    }
278
}
279
</pre>
280
                The assertion now fails as expected...
281
<pre class="shell">
282
Web site tests
283
1) Expecting response in [200] got [302]
284
	in testhomepage
285
	in testoflastcraft
286
	in lastcraft_test.php
287
FAILURES!!!
288
Test cases run: 1/1, Failures: 1, Exceptions: 0
289
</pre>
290
                We can modify the test to correctly assert redirects with...
291
<pre>
292
class TestOfLastcraft extends WebTestCase {
293
 
294
    function testHomepage() {
295
        $this-&gt;setMaximumRedirects(0);
296
        $this-&gt;get('http://www.lastcraft.com/test/redirect.php');
297
        $this-&gt;assertResponse(<strong>array(301, 302, 303, 307)</strong>);
298
    }
299
}
300
</pre>
301
                This now passes.
302
            </p>
303
 
304
        <p>
305
<a class="target" name="navigation">
306
<h2>Navigating a web site</h2>
307
</a>
308
</p>
309
            <p>
310
                Users don't often navigate sites by typing in URLs, but by
311
                clicking links and buttons.
312
                Here we confirm that the contact details can be reached
313
                from the home page...
314
<pre>
315
class TestOfLastcraft extends WebTestCase {
316
    ...
317
    function testContact() {
318
        $this-&gt;get('http://www.lastcraft.com/');<strong>
319
        $this-&gt;clickLink('About');
320
        $this-&gt;assertTitle(new PatternExpectation('/About Last Craft/'));</strong>
321
    }
322
}
323
</pre>
324
                The parameter is the text of the link.
325
            </p>
326
            <p>
327
                If the target is a button rather than an anchor tag, then
328
                <span class="new_code">clickSubmit()</span> can be used
329
                with the button title...
330
<pre>
331
<strong>$this-&gt;clickSubmit('Go!');</strong>
332
</pre>
333
                If you are not sure or don't care, the usual case, then just
334
                use the <span class="new_code">click()</span> method...
335
<pre>
336
<strong>$this-&gt;click('Go!');</strong>
337
</pre>
338
            </p>
339
            <p>
340
                The list of navigation methods is...
341
                <table>
342
<tbody>
343
                    <tr>
344
<td><span class="new_code">getUrl()</span></td><td>The current location</td>
345
</tr>
346
                    <tr>
347
<td><span class="new_code">get($url, $parameters)</span></td><td>Send a GET request with these parameters</td>
348
</tr>
349
                    <tr>
350
<td><span class="new_code">post($url, $parameters)</span></td><td>Send a POST request with these parameters</td>
351
</tr>
352
                    <tr>
353
<td><span class="new_code">head($url, $parameters)</span></td><td>Send a HEAD request without replacing the page content</td>
354
</tr>
355
                    <tr>
356
<td><span class="new_code">retry()</span></td><td>Reload the last request</td>
357
</tr>
358
                    <tr>
359
<td><span class="new_code">back()</span></td><td>Like the browser back button</td>
360
</tr>
361
                    <tr>
362
<td><span class="new_code">forward()</span></td><td>Like the browser forward button</td>
363
</tr>
364
                    <tr>
365
<td><span class="new_code">authenticate($name, $password)</span></td><td>Retry after a challenge</td>
366
</tr>
367
                    <tr>
368
<td><span class="new_code">restart()</span></td><td>Restarts the browser as if a new session</td>
369
</tr>
370
                    <tr>
371
<td><span class="new_code">getCookie($name)</span></td><td>Gets the cookie value for the current context</td>
372
</tr>
373
                    <tr>
374
<td><span class="new_code">ageCookies($interval)</span></td><td>Ages current cookies prior to a restart</td>
375
</tr>
376
                    <tr>
377
<td><span class="new_code">clearFrameFocus()</span></td><td>Go back to treating all frames as one page</td>
378
</tr>
379
                    <tr>
380
<td><span class="new_code">clickSubmit($label)</span></td><td>Click the first button with this label</td>
381
</tr>
382
                    <tr>
383
<td><span class="new_code">clickSubmitByName($name)</span></td><td>Click the button with this name attribute</td>
384
</tr>
385
                    <tr>
386
<td><span class="new_code">clickSubmitById($id)</span></td><td>Click the button with this ID attribute</td>
387
</tr>
388
                    <tr>
389
<td><span class="new_code">clickImage($label, $x, $y)</span></td><td>Click an input tag of type image by title or alt text</td>
390
</tr>
391
                    <tr>
392
<td><span class="new_code">clickImageByName($name, $x, $y)</span></td><td>Click an input tag of type image by name</td>
393
</tr>
394
                    <tr>
395
<td><span class="new_code">clickImageById($id, $x, $y)</span></td><td>Click an input tag of type image by ID attribute</td>
396
</tr>
397
                    <tr>
398
<td><span class="new_code">submitFormById($id)</span></td><td>Submit a form without the submit value</td>
399
</tr>
400
                    <tr>
401
<td><span class="new_code">clickLink($label, $index)</span></td><td>Click an anchor by the visible label text</td>
402
</tr>
403
                    <tr>
404
<td><span class="new_code">clickLinkById($id)</span></td><td>Click an anchor by the ID attribute</td>
405
</tr>
406
                    <tr>
407
<td><span class="new_code">getFrameFocus()</span></td><td>The name of the currently selected frame</td>
408
</tr>
409
                    <tr>
410
<td><span class="new_code">setFrameFocusByIndex($choice)</span></td><td>Focus on a frame counting from 1</td>
411
</tr>
412
                    <tr>
413
<td><span class="new_code">setFrameFocus($name)</span></td><td>Focus on a frame by name</td>
414
</tr>
415
                </tbody>
416
</table>
417
            </p>
418
            <p>
419
                The parameters in the <span class="new_code">get()</span>, <span class="new_code">post()</span> or
420
                <span class="new_code">head()</span> methods are optional.
421
                The HTTP HEAD fetch does not change the browser context, only loads
422
                cookies.
423
                This can be useful for when an image or stylesheet sets a cookie
424
                for crafty robot blocking.
425
            </p>
426
            <p>
427
                The <span class="new_code">retry()</span>, <span class="new_code">back()</span> and
428
                <span class="new_code">forward()</span> commands work as they would on
429
                your web browser.
430
                They use the history to retry pages.
431
                This can be handy for checking the effect of hitting the
432
                back button on your forms.
433
            </p>
434
            <p>
435
                The frame methods need a little explanation.
436
                By default a framed page is treated just like any other.
437
                Content will be searced for throughout the entire frameset,
438
                so clicking a link will work no matter which frame
439
                the anchor tag is in.
440
                You can override this behaviour by focusing on a single
441
                frame.
442
                If you do that, all searches and actions will apply to that
443
                frame alone, such as authentication and retries.
444
                If a link or button is not in a focused frame then it cannot
445
                be clicked.
446
            </p>
447
            <p>
448
                Testing navigation on fixed pages only tells you when you
449
                have broken an entire script.
450
                For highly dynamic pages, such as for bulletin boards, this can
451
                be crucial for verifying the correctness of the application.
452
                For most applications though, the really tricky logic is usually in
453
                the handling of forms and sessions.
454
                Fortunately SimpleTest includes
455
                <a href="form_testing_documentation.html">tools for testing web forms</a>
456
                as well.
457
            </p>
458
 
459
        <p>
460
<a class="target" name="request">
461
<h2>Modifying the request</h2>
462
</a>
463
</p>
464
            <p>
465
                Although SimpleTest does not have the goal of testing networking
466
                problems, it does include some methods to modify and debug
467
                the requests it makes.
468
                Here is another method list...
469
                <table>
470
<tbody>
471
                    <tr>
472
<td><span class="new_code">getTransportError()</span></td><td>The last socket error</td>
473
</tr>
474
                    <tr>
475
<td><span class="new_code">showRequest()</span></td><td>Dump the outgoing request</td>
476
</tr>
477
                    <tr>
478
<td><span class="new_code">showHeaders()</span></td><td>Dump the incoming headers</td>
479
</tr>
480
                    <tr>
481
<td><span class="new_code">showSource()</span></td><td>Dump the raw HTML page content</td>
482
</tr>
483
                    <tr>
484
<td><span class="new_code">ignoreFrames()</span></td><td>Do not load framesets</td>
485
</tr>
486
                    <tr>
487
<td><span class="new_code">setCookie($name, $value)</span></td><td>Set a cookie from now on</td>
488
</tr>
489
                    <tr>
490
<td><span class="new_code">addHeader($header)</span></td><td>Always add this header to the request</td>
491
</tr>
492
                    <tr>
493
<td><span class="new_code">setMaximumRedirects($max)</span></td><td>Stop after this many redirects</td>
494
</tr>
495
                    <tr>
496
<td><span class="new_code">setConnectionTimeout($timeout)</span></td><td>Kill the connection after this time between bytes</td>
497
</tr>
498
                    <tr>
499
<td><span class="new_code">useProxy($proxy, $name, $password)</span></td><td>Make requests via this proxy URL</td>
500
</tr>
501
                </tbody>
502
</table>
503
                These methods are principally for debugging.
504
            </p>
505
 
506
    </div>
507
<div class="copyright">
508
            Copyright<br>Marcus Baker, Jason Sweat, Perrick Penet 2004
509
        </div>
510
</body>
511
</html>