There are cases when we need to actually "see" the image. If there is an
<img element on a page we need to check that the image inside of it is really shown, i.e. it can be obtained from src element. How can this be checked in Codeception.
There is no built in method but by using PhpBrowser module we can send requests and check responses. We can connect it to a Helper class and write a new method which will check
Let's assume our acceptance tests are using WebDriver module and this means that PhpBrowser can't be enabled without a conflict. That's why we are not going to enable it but use it only as a dependency for our helper:
Here is the example
(we assume we are testing a website url defined as %host% parameter).
Then we need to inject PhpBrowser into
// this interface points that helper can get dependencies
class Acceptance extends \Codeception\Module implements DependsOnModule
* @var \Codeception\Module\PhpBrowser
// showing alert if PhpBrowser is not declared as dependency
public function _depends()
return ['\Codeception\Module\PhpBrowser' => 'PhpBrowser is required'];
// we inject PhpBrowser
public function _inject(\Codeception\Module\PhpBrowser $browser)
$this->phpbrowser = $browser;
// here we check image
public function seeImage($locator)
// element is on page
// getting its `src` attribute
$src = $this->getModule('WebDriver')->grabAttributeFrom($locator, 'src');
// sending a request and validating it
// check its content type: it should be `image/png` or `image/jpeg` or...
$contentType = $this->phpbrowser->client->getInternalResponse()->getHeader('Content-Type');
$this->assertContains('image/', $contentType, 'it is not an image');
Now you can use it in tests like this:
If you use PhpBrowser as your main testing module you can simplify the code by accessing a module using
$this->getModule(). In current example this module was not enabled so we couldn't do it like that.
As an alternative, use Guzzle library to send requests directly and check the values without injecting PHPBrowser.
P.S. The implementation could (should) be improved in next versions of Codeception.