cakephp2-php8/vendors/simpletest/docs/en/browser_documentation.html
pies da79dff7d7 - merged in Brego's SimpleTest implementation, fixed some of the tests (the Folder test fails to delete one of the test directories on my system, so it's not perfectly clean yet)
- Dispatcher sets a Controller::here variable with the real URL used to access the page, so that tag generators can that use an url (linkTo and formTag for example) use the real url, not guess it from the controller and action names which often fails
- Log class works more reliably and a LogError() shortcut function was added
- Nstring class added, to store string-related functions (there are just four yet, including a random password generator and an string-to-array splitter
- SimpleTest library (with Rephlux) included in /vendors; I've tweaked SimpleScorer::inCli() function, because it didn't work on my setup, it should work everywhere now (it checks for empty REQUEST_METHOD, which should only be empty in CLI)

git-svn-id: https://svn.cakephp.org/repo/trunk/cake@248 3807eeeb-6ff5-0310-8944-8be069107fe0
2005-06-18 23:26:35 +00:00

386 lines
15 KiB
HTML

<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>