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