Skip to content

Commit 0f65824

Browse files
committed
Support for taking screenshots of elements
Signed-Off-By: Matthias Blümel <matthias.bluemel@krumedia.com>
1 parent f719edc commit 0f65824

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"require": {
2222
"php": ">=5.6.0 <9.0",
2323
"codeception/codeception": "^4.0",
24-
"php-webdriver/webdriver": "^1.6.0"
24+
"php-webdriver/webdriver": "^1.8.0"
2525
},
2626
"suggest": {
2727
"codeception/phpbuiltinserver": "Start and stop PHP built-in web server for your tests"

src/Codeception/Module/WebDriver.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,20 @@ public function _saveScreenshot($filename)
739739
}
740740
}
741741

742+
public function _saveElementScreenshot($selector, $filename)
743+
{
744+
if (!isset($this->webDriver)) {
745+
$this->debug('WebDriver::_saveElementScreenshot method has been called when webDriver is not set');
746+
return;
747+
}
748+
try {
749+
$this->matchFirstOrFail($this->webDriver, $selector)->takeElementScreenshot($filename);
750+
} catch (\Exception $e) {
751+
$this->debug('Unable to retrieve element screenshot from Selenium : ' . $e->getMessage());
752+
return;
753+
}
754+
}
755+
742756
public function _findElements($locator)
743757
{
744758
return $this->match($this->webDriver, $locator);
@@ -789,6 +803,34 @@ public function makeScreenshot($name = null)
789803
$this->debugSection('Screenshot Saved', "file://$screenName");
790804
}
791805

806+
/**
807+
* Takes a screenshot of an element of the current window and saves it to `tests/_output/debug`.
808+
*
809+
* ``` php
810+
* <?php
811+
* $I->amOnPage('/user/edit');
812+
* $I->makeElementScreenshot('#dialog', 'edit_page');
813+
* // saved to: tests/_output/debug/edit_page.png
814+
* $I->makeElementScreenshot('#dialog');
815+
* // saved to: tests/_output/debug/2017-05-26_14-24-11_4b3403665fea6.png
816+
* ```
817+
*
818+
* @param $name
819+
*/
820+
public function makeElementScreenshot($selector, $name = null)
821+
{
822+
if (empty($name)) {
823+
$name = uniqid(date("Y-m-d_H-i-s_"));
824+
}
825+
$debugDir = codecept_log_dir() . 'debug';
826+
if (!is_dir($debugDir)) {
827+
mkdir($debugDir, 0777);
828+
}
829+
$screenName = $debugDir . DIRECTORY_SEPARATOR . $name . '.png';
830+
$this->_saveElementScreenshot($selector, $screenName);
831+
$this->debugSection('Screenshot Saved', "file://$screenName");
832+
}
833+
792834
public function makeHtmlSnapshot($name = null)
793835
{
794836
if (empty($name)) {

tests/web/WebDriverTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,21 @@ public function testScreenshot()
135135
@unlink(\Codeception\Configuration::outputDir().'testshot.png');
136136
}
137137

138+
public function testElementScreenshot()
139+
{
140+
$this->module->amOnPage('/');
141+
@unlink(\Codeception\Configuration::outputDir().'testelementshot.png');
142+
$testName="debugTestElement";
143+
144+
$this->module->makeElementScreenshot('#area4', $testName);
145+
$this->assertFileExists(\Codeception\Configuration::outputDir().'debug/'.$testName.'.png');
146+
@unlink(\Codeception\Configuration::outputDir().'debug/'.$testName.'.png');
147+
148+
$this->module->_saveElementScreenshot('#area4', \Codeception\Configuration::outputDir().'testshot.png');
149+
$this->assertFileExists(\Codeception\Configuration::outputDir().'testshot.png');
150+
@unlink(\Codeception\Configuration::outputDir().'testelementshot.png');
151+
}
152+
138153
public function testSnapshot()
139154
{
140155
$this->module->amOnPage('/');

0 commit comments

Comments
 (0)