5

I want to measure code coverage of an HTML test suite for selenium. Therefore I want to use PHPUnit in order to execute the suite, because PHPUnit has nice support for code coverage analysis.

Therefore: Is it possible to run an HTML test suite from PHPUnit?

Chris
  • 8,031
  • 10
  • 41
  • 67

2 Answers2

5

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

<?php
require_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.

Potherca
  • 13,207
  • 5
  • 76
  • 94
  • I want to keep the html test suite as stated in the question. We still have some PHP coded test cases and some HTML based test cases. – Chris Sep 06 '12 at 08:14
  • Some more digging shows it is, in fact, possible. Somehow I just never came across it. -- Updated my answer with this (for me) new information. – Potherca Sep 06 '12 at 10:00
  • That is fantastic. Will give you bounty for this answer. Thany you! – Chris Sep 06 '12 at 10:36
  • You are very welcome. If you run into trouble with Selenium coverage this might be useful as well: http://stackoverflow.com/questions/11270527/does-phpunit-selenium-code-coverage-work/12297192 – Potherca Sep 06 '12 at 10:57
0

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.

Bruce Wells
  • 626
  • 5
  • 6