Skip to content

Instantly share code, notes, and snippets.

@aleksblendwerk
Created March 3, 2021 08:57
Show Gist options
  • Save aleksblendwerk/9e147970aa07f7dea2abed7daf5ca857 to your computer and use it in GitHub Desktop.
Save aleksblendwerk/9e147970aa07f7dea2abed7daf5ca857 to your computer and use it in GitHub Desktop.
Codeception: Checking if an image is valid within an acceptance test

I know this is not bulletproof and a bit cumbersome but it's a start and much better than not testing them at all.

# [...]
modules:
enabled:
- WebDriver:
# [...]
- Tests\Codeception\Support\Helper\Image:
depends: PhpBrowser
# [...]
<?php
// tests/Codeception/Support/Helper/Image.php
namespace Tests\Codeception\Support\Helper;
use Tests\Codeception\Support\Helper\Abstracted\PhpBrowserDependentModule;
use Codeception\Exception\ModuleException;
class Image extends PhpBrowserDependentModule
{
protected static $imageRegEx = 'img[src="%s"]';
protected static $imageAltRegEx = 'img[alt="%s"]';
/**
* Checks if a link to an image returns a successful response and also checks if the width and height
* is larger than 0, if the image is a known format.
*
* @param $selector
* @throws ModuleException
*/
public function seeValidImage($selector)
{
$this->getModule('WebDriver')->seeElement($selector);
$url = $this->getModule('WebDriver')->grabAttributeFrom($selector, 'src');
$contents = $this->grabFile($url);
$imageInfo = getimagesizefromstring($contents);
$this->assertIsArray($imageInfo);
list($width, $height) = array_slice($imageInfo, 0, 2);
$this->assertGreaterThan(0, $width);
$this->assertGreaterThan(0, $height);
}
/**
* Checks if an image with a given URL is visible on the page (currently limited to img tags)
*
* @param $url
* @throws ModuleException
*/
public function seeImage($url)
{
// TODO: what about broken images?
$selector = sprintf(self::$imageRegEx, $url);
$this->getModule('WebDriver')->seeElement($selector);
}
/**
* Checks if an image with a given URL is not visible on the page (currently limited to img tags)
*
* @param $url
* @throws ModuleException
*/
public function dontSeeImage($url)
{
// TODO: what about broken images?
$selector = sprintf(self::$imageRegEx, $url);
$this->getModule('WebDriver')->dontSeeElement($selector);
}
/**
* Checks if an image with a given alt text is visible on the page (currently limited to img tags)
*
* @param $url
* @throws ModuleException
*/
public function seeImageWithAltText($altText)
{
// TODO: what about broken images?
$selector = sprintf(self::$imageAltRegEx, $altText);
$this->getModule('WebDriver')->seeElement($selector);
}
}
<?php
// tests/Codeception/Support/Helper/Abstracted/PhpBrowserDependentModule.php
namespace Tests\Codeception\Support\Helper\Abstracted;
use Tests\Codeception\Support\Page\Start;
use Codeception\Exception\ModuleException;
use Codeception\Lib\Interfaces\DependsOnModule;
use Codeception\Module;
use Codeception\Module\PhpBrowser;
/**
* Class PhpBrowserModule
* @package AppBundle\Tests\Codeception\Helper\Abstracted
*/
abstract class PhpBrowserDependentModule extends Module implements DependsOnModule
{
// TODO: get this directly from symfony's config...
protected const PHP_SESSION_ID = 'MOCKSESSID';
/**
* @var PhpBrowser
*/
protected $phpBrowser;
/**
* @return array
*/
public function _depends()
{
return [PhpBrowser::class => 'PhpBrowser is a mandatory dependency of this helper'];
}
/**
* @param PhpBrowser $phpBrowser
*/
public function _inject(PhpBrowser $phpBrowser)
{
$this->phpBrowser = $phpBrowser;
}
/**
* @param $url
* @return mixed|\Symfony\Component\DomCrawler\Crawler
* @throws ModuleException
* @throws \Codeception\Exception\ExternalUrlException
*/
public function grabFile($url)
{
$phpBrowser = $this->getPhpBrowser();
$contents = $phpBrowser->_request('GET', $url);
$phpBrowser->seeResponseCodeIs(200);
return $contents;
}
/**
* @return PhpBrowser
* @throws ModuleException
*/
protected function getPhpBrowser()
{
// not sure where else to put this...
// must be executed after the login via WebDriver has happened and before the first use of the PhpBrowser module
if ($this->phpBrowser->client->getInternalRequest() === null) {
$this->phpBrowser->amOnPage(Start::$url);
$this->phpBrowser->setCookie(
self::PHP_SESSION_ID,
$this->getModule('WebDriver')->grabCookie(self::PHP_SESSION_ID)
);
}
return $this->phpBrowser;
}
}
<?php
// [...]
// check if default user picture is set and visible
$profileImageUrl = $I->grabAttributeFrom($profileEditPage::$profileImageSelector, 'src');
$I->assertStringContainsString($profileEditPage::$profileImageDefaultFilename, $profileImageUrl);
$I->seeValidImage($profileEditPage::$profileImageSelector);
// [...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment