在Codeception验收测试中测试图像

时间:2014-01-13 15:38:50

标签: image codeception

我正在为我们的内部CMS设置一系列自动化测试。

此时此刻,我希望能够测试我们的测试页面中是否包含某些图像。

如果图像存在,我可以查看将显示的标题......

$I->see('Image Caption');

...但我真的希望能够检查图像本身的存在......

$I->seeImage('/path_to/image_file.jpg'); // I MADE THAT METHOD UP

...或者至少是img标签......

$I->seeSourceCode('<img src="/path_to/image_file.jpg"'); // I MADE THAT METHOD UP TOO

有谁知道怎么做?

1 个答案:

答案 0 :(得分:7)

seeElement()可能符合您的需要,例如:
$I->seeElement('//img[@src="/pixel.jpg"]');
see doc helper
并且,为了使测试更具可读性,我建议将实际调用包装在{{3}}中的seeElement()中并在测试中使用类似的内容 $I->seeImageWithSource('/pixel.jpg')

public function seeImageWithSource($image_url)
{
    $phpBrowser = $this->getModule('PhpBrowser');
    $phpBrowser->seeElement('//img[@src="'.$image_url.'"]');
}