Is it possible to run HTML test suite from PHPUnit? Is it possible to run HTML test suite from PHPUnit? selenium selenium

Is it possible to run HTML test suite from PHPUnit?


Short answer

Running individual HTML test files is not a problem, running HTML suites files however does not seem to work. As long as you put all the HTML test files from a suit in a directory by themselves you can just run runSelenese($folderName)

Long answer

I had no idea that running Selenium HTML files directly was even possible until I did some more digging.

What I used to do is convert/export them first with the Selenium IDE PHP Formatter plugin for Firefox.

Apparently this is not necessary. All the way at the bottom of Chapter 17. PHPUnit and Selenium the manual states:

Using the runSelenese($filename) method, you can also run a Selenium test from its Selenese/HTML specification. Furthermore, using the static attribute $seleneseDirectory, you can automatically create test objects from a directory that contains Selenese/HTML files. The specified directory is recursively searched for .htm files that are expected to contain Selenese/HTML. Example 17.5 shows an example.

Example 17.5: Use a directory of Selenese/HTML files as tests

<?phprequire_once 'PHPUnit/Extensions/SeleniumTestCase.php';class SeleneseTests extends PHPUnit_Extensions_SeleniumTestCase{    public static $seleneseDirectory = '/path/to/files';}?>

Once you have your Tests in PHPUnit you can use it to get code coverage from the Selenium Server.

Again, from the manual:

PHPUnit_Extensions_SeleniumTestCase can collect code coverage information for tests run through Selenium:

  1. Copy PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php into your webserver's document root directory.
  2. In your webserver's php.ini configuration file, configure PHPUnit/Extensions/SeleniumTestCase/prepend.php and PHPUnit/Extensions/SeleniumTestCase/append.php as the auto_prepend_file and auto_append_file, respectively.
  3. In your test case class that extends PHPUnit_Extensions_SeleniumTestCase, use
    protected $coverageScriptUrl = 'http://host/phpunit_coverage.php';
    to configure the URL for the phpunit_coverage.php script.

As this can be a bit of a hassle to set up, I've always just used the Page Coverage plugin to get an insight into HTML page coverage.


Unit testing PHP HTML output is a great idea. More people should do it, but turns out it has not been that simple in the past.

I had the same problem and can up with this: https://packagist.org/packages/phpfui/html-unit-tester It can validate HTML and CSS from a URL, file, or string snippet.

To do what you want, you should set up a local web server that your tests can hit for your application. You probably have this already, but make sure it works for your local env and any server you run tests on.

Then you can use the assertValidUrl method with a path to your local server. Since you are not comparing to a template, but just testing that the HTML is valid, you can quickly find recently introduced HTML errors.

Hope this helps.