PHP in Action Rotating Header Image

Tips for web testing

I just started listing the techniques I've learned when writing tests to exercise the web interface of a PHP application. This is from my experience and my personal preferences; it's not the final word or necessarily right for everyone.

The idea of being able to record and play back tests, as you can do with Selenium, is tempting. But I want to be able to program the tests. Programming in this context means anything from simple conditionals and loops to advanced test patterns (see http://xunitpatterns.com). That way I can achieve a much more sophisticated test organization, and more flexible and robust tests.

Use SimpleTest's Web tester if you can

It's the simplest and most direct way to write web tests. But if you need to test an interface that involves advanced JavaScript stuff such as Ajax, you something that has JavaScript capability. Selenium can do that and the simple things as well, but it needs to run an actual web browser. To me that's overkill for testing a plain web interface.

Some simple JavaScript can be handled by ignoring and bypassing it in SimpleTest. For example: if you have a select box that submits a form, you can emulate that action by using submitFormById() in SimpleTest.

Test the web output using regular expressions

Regular expressions make tests more tolerant and less likely to break when web page details change. assertPattern in SimpleTest tests the HTML source of the page. For example:

$this->assertPattern('/ );
[/code]

The regex matches both class="admin" and class=admin. The latter syntax may be obsolete, but since it occurs in the wild, you may have to take it into account anyway. The i modifier means case insensitive.

Use element IDs or names to test links, forms and fields

The point of IDs is to identify elements, and they are suited to the purpose. They can be made unique, and they can be left unchanged when the text seen by the user changes. That goes for form and field names, too, but they're slightly less reliable since they have other work to do as well.

Log HTTP requests in the application

If the tests fail for reasons that seem hard to fathom, write the contents of the request variables to a log file. Then you can compare the test requests to the ones your browser generates when you test manually.

Share/Save/Bookmark

One Comment

  1. keyur patel says:

    nice article, thanks

Leave a Reply