Simple Test documentation for testing HTML forms

When a page is fetched by the WebTestCase using get() or post() the page content is automatically parsed. This results in any form controls that are inside <form> tags being available from within the test case. For example, if we have this snippet of HTML...


    
    

]]>
Which looks like this...

We can navigate to this code, via the LastCraft site, with the following test... function testDefaultValue() { $this->get('http://www.lastcraft.com/form_testing_documentation.php'); $this->assertField('a', 'A default'); } } ]]> Immediately after loading the page all of the HTML controls are set at their default values just as they would appear in the web browser. The assertion tests that a HTML widget exists in the page with the name "a" and that it is currently set to the value "A default". As usual, we could use a pattern expectation instead if a fixed string.

We could submit the form straight away, but first we'll change the value of the text field and only then submit it... get('http://www.my-site.com/'); $this->assertField('a', 'A default'); $this->setField('a', 'New value'); $this->click('Go'); } } ]]> Because we didn't specify a method attribute on the form tag, and didn't specify an action either, the test case will follow the usual browser behaviour of submitting the form data as a GET request back to the same location. SimpleTest tries to emulate typical browser behaviour as much as possible, rather than attempting to catch missing attributes on tags. This is because the target of the testing framework is the PHP application logic, not syntax or other errors in the HTML code. For HTML errors, other tools such as HTMLTidy should be used.

If a field is not present in any form, or if an option is unavailable, then WebTestCase::setField() will return false. For example, suppose we wish to verify that a "Superuser" option is not present in this form...

Select type of user to add:

]]>
Which looks like...

Select type of user to add:

The following test will confirm it... $this->get('http://www.lastcraft.com/form_testing_documentation.php'); $this->assertFalse($this->setField('type', 'Superuser')); } } ]]> The selection will not be changed on a failure to set a widget value.

Here is the full list of widgets currently supported...

  • Text fields, including hidden and password fields.
  • Submit buttons including the button tag, although not yet reset buttons
  • Text area. This includes text wrapping behaviour.
  • Checkboxes, including multiple checkboxes in the same form.
  • Drop down selections, including multiple selects.
  • Radio buttons.
  • Images.

Although most standard HTML widgets are catered for by SimpleTest's built in parser, it is unlikely that JavaScript will be implemented anytime soon.

SimpleTest can cope with two types of multivalue controls: Multiple selection drop downs, and multiple checkboxes with the same name within a form. The multivalue nature of these means that setting and testing are slightly different. Using checkboxes as an example...


    Create privileges allowed:
    
Retrieve privileges allowed:
Update privileges allowed:
Destroy privileges allowed:
]]>
Which renders as...

Create privileges allowed:
Retrieve privileges allowed:
Update privileges allowed:
Destroy privileges allowed:

If we wish to disable all but the retrieval privileges and submit this information we can do it like this... function testDisableNastyPrivileges() { $this->get('http://www.lastcraft.com/form_testing_documentation.php'); $this->assertField('crud', array('c', 'r', 'u', 'd')); $this->setField('crud', array('r')); $this->click('Enable Privileges'); } } ]]> Instead of setting the field to a single value, we give it a list of values. We do the same when testing expected values. We can then write other test code to confirm the effect of this, perhaps by logging in as that user and attempting an update.

Raw posting

If you want to test a form handler, but have not yet written or do not have access to the form itself, you can create a form submission by hand. function testAttemptedHack() { $this->post( 'http://www.my-site.com/add_user.php', array('type' => 'superuser')); $this->assertNoText('user created'); } } ]]> By adding data to the WebTestCase::post() method, we are attempting to fetch the page as a form submission.

Changing form values and successfully Submitting a simple form Handling widgets with multiple values by setting lists. Raw posting when you don't have a button to click. SimpleTest project page on SourceForge. SimpleTest download page on LastCraft. The developer's API for SimpleTest gives full detail on the classes and assertions available. software development, php programming for clients, customer focused php, software development tools, acceptance testing framework, free php scripts, architecture, php resources, HTMLUnit, JWebUnit, php testing, unit test resource, web testing