source: trunk/server/www/vendors/simpletest/docs/en/browser_documentation.html @ 6

Last change on this file since 6 was 6, checked in by sander, 11 years ago

Added SimpleTest? test framework

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