tanaka's Programming Memo

プログラミングについてのメモ。

PHPUnit_Seleniumでスクリーンショット(動作確認)

画面のレンダリングが成功しているかを確認するために、必要な場面でスクリーンショットを保存したい。

方法

  • $this->currentScreenshot()でPNG画像を文字列化したものが得られる。
  • $this->currentScreenshot()の文字列を、PHPのfile_put_contents()関数を使って保存する。

PHPUnit_Seleniumでページを表示して、カレントディレクトリにscshot.pngという名前で保存するテストコード。

<?php
class scshotTest extends PHPUnit_Extensions_Selenium2TestCase
{
	protected function setUp()
	{
		$this->setBrowser('firefox');
		$this->setBrowserUrl('http://www.google.co.jp');
	}
	
	public function testView()
	{
		$this->url('http://www.google.co.jp');
		file_put_contents("./scshot.png", $this->currentScreenshot());
	}
}
?>