PHPUnit Selenium2 で phpunit.xml にリスナー指定でスクリーンショットを取得

下記の記事を見て PHPUnit_Extensions_Selenium2TestCase_ScreenshotListener はリスナーなので phpunit.xml で指定した方が良いかと思って試しました。

phpunit.xml

<?xml version="1.0" encoding="utf-8" ?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="https://raw.github.com/sebastianbergmann/phpunit/master/phpunit.xsd"
    bootstrap="./tests/bootstrap.php"
>
    <testsuites>
        <testsuite name="tests">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <listeners>
        <listener class="PHPUnit_Extensions_Selenium2TestCase_ScreenshotListener">
            <arguments>
                <string>./output/</string>
            </arguments>
        </listener>
    </listeners>
</phpunit>

が、この方法だとスクリーンショットの保存先はカレントディレクトリからの相対になります($CWD/output/ に保存される)。

なんとなく気持ち悪いので次のように PHPUnit_Extensions_Selenium2TestCase_ScreenshotListener を継承した自前のリスナーを用意しました。

ScreenshotListener.php

<?php
class ScreenshotListener extends \PHPUnit_Extensions_Selenium2TestCase_ScreenshotListener
{
    public function __construct()
    {
        parent::__construct(APPLICATION_ROOT . '/output/');
    }
}

phpunit.xml

<?xml version="1.0" encoding="utf-8" ?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="https://raw.github.com/sebastianbergmann/phpunit/master/phpunit.xsd"
    bootstrap="./tests/bootstrap.php"
>
    <testsuites>
        <testsuite name="tests">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <listeners>
        <listener class="ScreenshotListener" file="./tests/ScreenshotListener.php"/>
    </listeners>
</phpunit>

ScreenshotListener がオートロード可能なら listener 要素の file 属性は不要です。


ちなみに PHPUnit_Extensions_Selenium2TestCase_ScreenshotListener はテストケースの完全修飾クラス名をそのままファイル名にするので、名前空間を使っていると Windows でまともにスナップショットが保存されません。 (あらかじめ名前空間のディレクトリを作成しておけば大丈夫ですが・・・)