Skip to content

Instantly share code, notes, and snippets.

@amenk
Created April 23, 2014 09:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amenk/11208415 to your computer and use it in GitHub Desktop.
Save amenk/11208415 to your computer and use it in GitHub Desktop.
Take a screenshot of a specific part (CSS selector) in Behat
<?php
/**
* @Then /^take a screenshot of "([^"]*)" and save "([^"]*)"$/
*/
public function takeAScreenshotOfAndSave($selector, $filename)
{
$this->saveScreenshot($filename, '/tmp');
$pos = $this->getSession()->evaluateScript('$$("' . $selector . '").first().getBoundingClientRect();');
$dst_image = imagecreatetruecolor(round($pos['width']), round($pos['height']));
$src_image = imagecreatefrompng("/tmp/" . $filename);
imagecopyresampled($dst_image, $src_image,
0, 0,
round($pos['left']), round($pos['top']), round($pos['width']), round($pos['height']),
round($pos['width']), round($pos['height']));
imagepng($dst_image, "/tmp/" . $filename);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment