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>SimpleTest documentation for the scriptable web browser component</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
<a href="web_tester_documentation.html">Web tester</a>
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
<span class="chosen">Scriptable browser</span>
46
</li>
47
</ul>
48
</div>
49
</div>
50
<h1>PHP Scriptable Web Browser</h1>
51
<div class="content">
52
 
53
            <p>
54
                SimpleTest's web browser component can be used not just
55
                outside of the <span class="new_code">WebTestCase</span> class, but also
56
                independently of the SimpleTest framework itself.
57
            </p>
58
 
59
        <p>
60
<a class="target" name="scripting">
61
<h2>The Scriptable Browser</h2>
62
</a>
63
</p>
64
            <p>
65
                You can use the web browser in PHP scripts to confirm
66
                services are up and running, or to extract information
67
                from them at a regular basis.
68
                For example, here is a small script to extract the current number of
69
                open PHP 5 bugs from the <a href="http://www.php.net/">PHP web site</a>...
70
<pre>
71
<strong>&lt;?php
72
    require_once('simpletest/browser.php');
73
 
74
    $browser = &amp;new SimpleBrowser();
75
    $browser-&gt;get('http://php.net/');
76
    $browser-&gt;click('reporting bugs');
77
    $browser-&gt;click('statistics');
78
    $page = $browser-&gt;click('PHP 5 bugs only');
79
    preg_match('/status=Open.*?by=Any.*?(\d+)&lt;\/a&gt;/', $page, $matches);
80
    print $matches[1];
81
?&gt;</strong>
82
</pre>
83
                There are simpler methods to do this particular example in PHP
84
                of course.
85
                For example you can just use the PHP <span class="new_code">file()</span>
86
                command against what here is a pretty fixed page.
87
                However, using the web browser for scripts allows authentication,
88
                correct handling of cookies, automatic loading of frames, redirects,
89
                form submission and the ability to examine the page headers.
90
                Such methods are fragile against a site that is constantly
91
                evolving and you would want a more direct way of accessing
92
                data in a permanent set up, but for simple tasks this can provide
93
                a very rapid solution.
94
            </p>
95
            <p>
96
                All of the navigation methods used in the
97
                <a href="web_tester_documentation.html">WebTestCase</a>
98
                are present in the <span class="new_code">SimpleBrowser</span> class, but
99
                the assertions are replaced with simpler accessors.
100
                Here is a full list of the page navigation methods...
101
                <table>
102
<tbody>
103
                    <tr>
104
<td><span class="new_code">addHeader($header)</span></td><td>Adds a header to every fetch</td>
105
</tr>
106
                    <tr>
107
<td><span class="new_code">useProxy($proxy, $username, $password)</span></td><td>Use this proxy from now on</td>
108
</tr>
109
                    <tr>
110
<td><span class="new_code">head($url, $parameters)</span></td><td>Perform a HEAD request</td>
111
</tr>
112
                    <tr>
113
<td><span class="new_code">get($url, $parameters)</span></td><td>Fetch a page with GET</td>
114
</tr>
115
                    <tr>
116
<td><span class="new_code">post($url, $parameters)</span></td><td>Fetch a page with POST</td>
117
</tr>
118
                    <tr>
119
<td><span class="new_code">clickLink($label)</span></td><td>Follows a link by label</td>
120
</tr>
121
                    <tr>
122
<td><span class="new_code">isLink($label)</span></td><td>See if a link is present by label</td>
123
</tr>
124
                    <tr>
125
<td><span class="new_code">clickLinkById($id)</span></td><td>Follows a link by attribute</td>
126
</tr>
127
                    <tr>
128
<td><span class="new_code">isLinkById($id)</span></td><td>See if a link is present by attribut</td>
129
</tr>
130
                    <tr>
131
<td><span class="new_code">getUrl()</span></td><td>Current URL of page or frame</td>
132
</tr>
133
                    <tr>
134
<td><span class="new_code">getTitle()</span></td><td>Page title</td>
135
</tr>
136
                    <tr>
137
<td><span class="new_code">getContent()</span></td><td>Raw page or frame</td>
138
</tr>
139
                    <tr>
140
<td><span class="new_code">getContentAsText()</span></td><td>HTML removed except for alt text</td>
141
</tr>
142
                    <tr>
143
<td><span class="new_code">retry()</span></td><td>Repeat the last request</td>
144
</tr>
145
                    <tr>
146
<td><span class="new_code">back()</span></td><td>Use the browser back button</td>
147
</tr>
148
                    <tr>
149
<td><span class="new_code">forward()</span></td><td>Use the browser forward button</td>
150
</tr>
151
                    <tr>
152
<td><span class="new_code">authenticate($username, $password)</span></td><td>Retry page or frame after a 401 response</td>
153
</tr>
154
                    <tr>
155
<td><span class="new_code">restart($date)</span></td><td>Restarts the browser for a new session</td>
156
</tr>
157
                    <tr>
158
<td><span class="new_code">ageCookies($interval)</span></td><td>Ages the cookies by the specified time</td>
159
</tr>
160
                    <tr>
161
<td><span class="new_code">setCookie($name, $value)</span></td><td>Sets an additional cookie</td>
162
</tr>
163
                    <tr>
164
<td><span class="new_code">getCookieValue($host, $path, $name)</span></td><td>Reads the most specific cookie</td>
165
</tr>
166
                    <tr>
167
<td><span class="new_code">getCurrentCookieValue($name)</span></td><td>Reads cookie for the current context</td>
168
</tr>
169
                </tbody>
170
</table>
171
                The methods <span class="new_code">SimpleBrowser::useProxy()</span> and
172
                <span class="new_code">SimpleBrowser::addHeader()</span> are special.
173
                Once called they continue to apply to all subsequent fetches.
174
            </p>
175
            <p>
176
                Navigating forms is similar to the
177
                <a href="form_testing_documentation.html">WebTestCase form navigation</a>...
178
                <table>
179
<tbody>
180
                    <tr>
181
<td><span class="new_code">setField($name, $value)</span></td><td>Sets all form fields with that name</td>
182
</tr>
183
                    <tr>
184
<td><span class="new_code">setFieldById($id, $value)</span></td><td>Sets all form fields with that id</td>
185
</tr>
186
                    <tr>
187
<td><span class="new_code">getField($name)</span></td><td>Accessor for a form element value</td>
188
</tr>
189
                    <tr>
190
<td><span class="new_code">getFieldById($id)</span></td><td>Accessor for a form element value</td>
191
</tr>
192
                    <tr>
193
<td><span class="new_code">clickSubmit($label)</span></td><td>Submits form by button label</td>
194
</tr>
195
                    <tr>
196
<td><span class="new_code">clickSubmitByName($name)</span></td><td>Submits form by button attribute</td>
197
</tr>
198
                    <tr>
199
<td><span class="new_code">clickSubmitById($id)</span></td><td>Submits form by button attribute</td>
200
</tr>
201
                    <tr>
202
<td><span class="new_code">clickImage($label, $x, $y)</span></td><td>Clicks the image by alt text</td>
203
</tr>
204
                    <tr>
205
<td><span class="new_code">clickImageByName($name, $x, $y)</span></td><td>Clicks the image by attribute</td>
206
</tr>
207
                    <tr>
208
<td><span class="new_code">clickImageById($id, $x, $y)</span></td><td>Clicks the image by attribute</td>
209
</tr>
210
                    <tr>
211
<td><span class="new_code">submitFormById($id)</span></td><td>Submits by the form tag attribute</td>
212
</tr>
213
                </tbody>
214
</table>
215
                At the moment there aren't any methods to list available forms
216
                and fields.
217
                This will probably be added to later versions of SimpleTest.
218
            </p>
219
            <p>
220
                Within a page, individual frames can be selected.
221
                If no selection is made then all the frames are merged together
222
                in one large conceptual page.
223
                The content of the current page will be a concatenation of all of the
224
                frames in the order that they were specified in the "frameset"
225
                tags.
226
                <table>
227
<tbody>
228
                    <tr>
229
<td><span class="new_code">getFrames()</span></td><td>A dump of the current frame structure</td>
230
</tr>
231
                    <tr>
232
<td><span class="new_code">getFrameFocus()</span></td><td>Current frame label or index</td>
233
</tr>
234
                    <tr>
235
<td><span class="new_code">setFrameFocusByIndex($choice)</span></td><td>Select a frame numbered from 1</td>
236
</tr>
237
                    <tr>
238
<td><span class="new_code">setFrameFocus($name)</span></td><td>Select frame by label</td>
239
</tr>
240
                    <tr>
241
<td><span class="new_code">clearFrameFocus()</span></td><td>Treat all the frames as a single page</td>
242
</tr>
243
                </tbody>
244
</table>
245
                When focused on a single frame, the content will come from
246
                that frame only.
247
                This includes links to click and forms to submit.
248
            </p>
249
 
250
        <p>
251
<a class="target" name="debug">
252
<h2>What went wrong?</h2>
253
</a>
254
</p>
255
            <p>
256
                All of this functionality is great when we actually manage to fetch pages,
257
                but that doesn't always happen.
258
                To help figure out what went wrong, the browser has some methods to
259
                aid in debugging...
260
                <table>
261
<tbody>
262
                    <tr>
263
<td><span class="new_code">setConnectionTimeout($timeout)</span></td><td>Close the socket on overrun</td>
264
</tr>
265
                    <tr>
266
<td><span class="new_code">getRequest()</span></td><td>Raw request header of page or frame</td>
267
</tr>
268
                    <tr>
269
<td><span class="new_code">getHeaders()</span></td><td>Raw response header of page or frame</td>
270
</tr>
271
                    <tr>
272
<td><span class="new_code">getTransportError()</span></td><td>Any socket level errors in the last fetch</td>
273
</tr>
274
                    <tr>
275
<td><span class="new_code">getResponseCode()</span></td><td>HTTP response of page or frame</td>
276
</tr>
277
                    <tr>
278
<td><span class="new_code">getMimeType()</span></td><td>Mime type of page or frame</td>
279
</tr>
280
                    <tr>
281
<td><span class="new_code">getAuthentication()</span></td><td>Authentication type in 401 challenge header</td>
282
</tr>
283
                    <tr>
284
<td><span class="new_code">getRealm()</span></td><td>Authentication realm in 401 challenge header</td>
285
</tr>
286
                    <tr>
287
<td><span class="new_code">setMaximumRedirects($max)</span></td><td>Number of redirects before page is loaded anyway</td>
288
</tr>
289
                    <tr>
290
<td><span class="new_code">setMaximumNestedFrames($max)</span></td><td>Protection against recursive framesets</td>
291
</tr>
292
                    <tr>
293
<td><span class="new_code">ignoreFrames()</span></td><td>Disables frames support</td>
294
</tr>
295
                    <tr>
296
<td><span class="new_code">useFrames()</span></td><td>Enables frames support</td>
297
</tr>
298
                    <tr>
299
<td><span class="new_code">ignoreCookies()</span></td><td>Disables sending and receiving of cookies</td>
300
</tr>
301
                    <tr>
302
<td><span class="new_code">useCookies()</span></td><td>Enables cookie support</td>
303
</tr>
304
                </tbody>
305
</table>
306
                The methods <span class="new_code">SimpleBrowser::setConnectionTimeout()</span>
307
                <span class="new_code">SimpleBrowser::setMaximumRedirects()</span>,
308
                <span class="new_code">SimpleBrowser::setMaximumNestedFrames()</span>,
309
                <span class="new_code">SimpleBrowser::ignoreFrames()</span>,
310
                <span class="new_code">SimpleBrowser::useFrames()</span>,
311
                <span class="new_code">SimpleBrowser::ignoreCookies()</span> and
312
                <span class="new_code">SimpleBrowser::useCokies()</span> continue to apply
313
                to every subsequent request.
314
                The other methods are frames aware.
315
                This means that if you have an individual frame that is not
316
                loading, navigate to it using <span class="new_code">SimpleBrowser::setFrameFocus()</span>
317
                and you can then use <span class="new_code">SimpleBrowser::getRequest()</span>, etc to
318
                see what happened.
319
            </p>
320
 
321
        <p>
322
<a class="target" name="unit">
323
<h2>Complex unit tests with multiple browsers</h2>
324
</a>
325
</p>
326
            <p>
327
                Anything that could be done in a
328
                <a href="web_tester_documentation.html">WebTestCase</a> can
329
                now be done in a <a href="unit_tester_documentation.html">UnitTestCase</a>.
330
                This means that we can freely mix domain object testing with the
331
                web interface...
332
<pre>
333
<strong>
334
class TestOfRegistration extends UnitTestCase {
335
    function testNewUserAddedToAuthenticator() {</strong>
336
        $browser = &amp;new SimpleBrowser();
337
        $browser-&gt;get('http://my-site.com/register.php');
338
        $browser-&gt;setField('email', 'me@here');
339
        $browser-&gt;setField('password', 'Secret');
340
        $browser-&gt;click('Register');
341
        <strong>
342
        $authenticator = &amp;new Authenticator();
343
        $member = &amp;$authenticator-&gt;findByEmail('me@here');
344
        $this-&gt;assertEqual($member-&gt;getPassword(), 'Secret');
345
    }
346
}</strong>
347
</pre>
348
                While this may be a useful temporary expediency, I am not a fan
349
                of this type of testing.
350
                The testing has cut across application layers, make it twice as
351
                likely it will need refactoring when the code changes.
352
            </p>
353
            <p>
354
                A more useful case of where using the browser directly can be helpful
355
                is where the <span class="new_code">WebTestCase</span> cannot cope.
356
                An example is where two browsers are needed at the same time.
357
            </p>
358
            <p>
359
                For example, say we want to disallow multiple simultaneous
360
                usage of a site with the same username.
361
                This test case will do the job...
362
<pre>
363
class TestOfSecurity extends UnitTestCase {
364
    function testNoMultipleLoginsFromSameUser() {<strong>
365
        $first = &amp;new SimpleBrowser();
366
        $first-&gt;get('http://my-site.com/login.php');
367
        $first-&gt;setField('name', 'Me');
368
        $first-&gt;setField('password', 'Secret');
369
        $first-&gt;click('Enter');
370
        $this-&gt;assertEqual($first-&gt;getTitle(), 'Welcome');
371
 
372
        $second = &amp;new SimpleBrowser();
373
        $second-&gt;get('http://my-site.com/login.php');
374
        $second-&gt;setField('name', 'Me');
375
        $second-&gt;setField('password', 'Secret');
376
        $second-&gt;click('Enter');
377
        $this-&gt;assertEqual($second-&gt;getTitle(), 'Access Denied');</strong>
378
    }
379
}
380
</pre>
381
                You can also use the <span class="new_code">SimpleBrowser</span> class
382
                directly when you want to write test cases using a different
383
                test tool than SimpleTest.
384
            </p>
385
 
386
    </div>
387
<div class="copyright">
388
            Copyright<br>Marcus Baker, Jason Sweat, Perrick Penet 2004
389
        </div>
390
</body>
391
</html>