Previous Next

Scriptable browser

SimpleTest's web browser component can be used not just outside of the WebTestCase class, but also independently of the SimpleTest framework itself.

Table of Contents

The Scriptable Browser

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 PHP web site...

  1. <?php
  2. require_once('simpletest/browser.php');
  4. $browser &new SimpleBrowser();
  5. $browser->get('');
  6. $browser->click('reporting bugs');
  7. $browser->click('statistics');
  8. $page $browser->click('PHP 5 bugs only');
  9. preg_match('/status=Open.*?by=Any.*?(\d+)<\/a>/'$page$matches);
  10. print $matches[1];
  11. ?>
There are simpler methods to do this particular example in PHP of course. For example you can just use the PHP file() 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.

Methods such as periodic scraping 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.

All of the navigation methods used in the WebTestCase.html are present in the SimpleBrowser class, but the assertions are replaced with simpler accessors. Here is a full list of the page navigation methods...

addHeader($header) Adds a header to every fetch
useProxy($proxy, $username, $password) Use this proxy from now on
head($url, $parameters) Perform a HEAD request
get($url, $parameters) Fetch a page with GET
post($url, $parameters) Fetch a page with POST
click($label) Clicks visible link or button text
clickLink($label) Follows a link by label
clickLinkById($id) Follows a link by attribute
getUrl() Current URL of page or frame
getTitle() Page title
getContent() Raw page or frame
getContentAsText() HTML removed except for alt text
retry() Repeat the last request
back() Use the browser back button
forward() Use the browser forward button
authenticate($username, $password) Retry page or frame after a 401 response
restart($date) Restarts the browser for a new session
ageCookies($interval) Ages the cookies by the specified time
setCookie($name, $value) Sets an additional cookie
getCookieValue($host, $path, $name) Reads the most specific cookie
getCurrentCookieValue($name) Reads cookie for the current context
The methods SimpleBrowser::useProxy() and SimpleBrowser::addHeader() are special. Once called they continue to apply to all subsequent fetches.

Navigating forms is similar to the form navigation.html WebTestCase form navigation...

setField($label, $value) Sets all form fields with that label or name
setFieldByName($name, $value) Sets all form fields with that name
setFieldById($id, $value) Sets all form fields with that id
getField($label) Accessor for a form element value by label tag and then name
getFieldByName($name) Accessor for a form element value using name attribute
getFieldById($id) Accessor for a form element value
clickSubmit($label) Submits form by button label
clickSubmitByName($name) Submits form by button attribute
clickSubmitById($id) Submits form by button attribute
clickImage($label, $x, $y) Clicks an input tag of type image by title or alt text
clickImageByName($name, $x, $y) Clicks an input tag of type image by name
clickImageById($id, $x, $y) Clicks an input tag of type image by ID attribute
submitFormById($id) Submits by the form tag attribute
At the moment there aren't many methods to list available links and fields.
isClickable($label) Test to see if a click target exists by label or name
isSubmit($label) Test for the existence of a button with that label or name
isImage($label) Test for the existence of an image button with that label or name
getLink($label) Finds a URL from it's label
getLinkById($label) Finds a URL from it's ID attribute
getUrls() Lists available links in the current page
This will be expanded in later versions of SimpleTest.

Frames are a rather esoteric feature these days, but SimpleTest has retained support for them.

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.

getFrames() A dump of the current frame structure
getFrameFocus() Current frame label or index
setFrameFocusByIndex($choice) Select a frame numbered from 1
setFrameFocus($name) Select frame by label
clearFrameFocus() Treat all the frames as a single page
When focused on a single frame, the content will come from that frame only. This includes links to click and forms to submit.

What went wrong?

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

setConnectionTimeout($timeout) Close the socket on overrun
getUrl() Url of most recent page fetched
getRequest() Raw request header of page or frame
getHeaders() Raw response header of page or frame
getTransportError() Any socket level errors in the last fetch
getResponseCode() HTTP response of page or frame
getMimeType() Mime type of page or frame
getAuthentication() Authentication type in 401 challenge header
getRealm() Authentication realm in 401 challenge header
getBaseUrl() Base url only of most recent page fetched
setMaximumRedirects($max) Number of redirects before page is loaded anyway
setMaximumNestedFrames($max) Protection against recursive framesets
ignoreFrames() Disables frames support
useFrames() Enables frames support
ignoreCookies() Disables sending and receiving of cookies
useCookies() Enables cookie support
The methods SimpleBrowser::setConnectionTimeout() SimpleBrowser::setMaximumRedirects(), SimpleBrowser::setMaximumNestedFrames(), SimpleBrowser::ignoreFrames(), SimpleBrowser::useFrames(), SimpleBrowser::ignoreCookies() and SimpleBrowser::useCokies() 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 SimpleBrowser::setFrameFocus() and you can then use SimpleBrowser::getRequest(), etc to see what happened.

Complex unit tests with multiple browsers

Anything that could be done in a WebTestCase.html can now be done in a UnitTestCase.html. This means that we could freely mix domain object testing with the web interface...

  1. class TestOfRegistration extends UnitTestCase {
  2.     function testNewUserAddedToAuthenticator({
  3.         $browser new SimpleBrowser();
  4.         $browser->get('');
  5.         $browser->setField('email''me@here');
  6.         $browser->setField('password''Secret');
  7.         $browser->click('Register');
  9.         $authenticator new Authenticator();
  10.         $member $authenticator->findByEmail('me@here');
  11.         $this->assertEqual($member->getPassword()'Secret');
  12.     }
  13. }
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.

A more useful case of where using the browser directly can be helpful is where the WebTestCase cannot cope. An example is where two browsers are needed at the same time.

For example, say we want to disallow multiple simultaneous usage of a site with the same username. This test case will do the job...

  1. class TestOfSecurity extends UnitTestCase {
  2.     function testNoMultipleLoginsFromSameUser({
  3.         $first_attempt new SimpleBrowser();
  4.         $first_attempt->get('');
  5.         $first_attempt->setField('name''Me');
  6.         $first_attempt->setField('password''Secret');
  7.         $first_attempt->click('Enter');
  8.         $this->assertEqual($first_attempt->getTitle()'Welcome');
  10.         $second_attempt new SimpleBrowser();
  11.         $second_attempt->get('');
  12.         $second_attempt->setField('name''Me');
  13.         $second_attempt->setField('password''Secret');
  14.         $second_attempt->click('Enter');
  15.         $this->assertEqual($second_attempt->getTitle()'Access Denied');
  16.     }
  17. }
You can also use the SimpleBrowser class directly when you want to write test cases using a different test tool than SimpleTest, such as PHPUnit.

Previous Next

Documentation generated on Sun, 31 Oct 2010 16:30:50 -0500 by phpDocumentor 1.4.3