Skip to content

Commit 63ea088

Browse files
authored
Merge pull request #35 from krumedia/element-screenshot
Support for taking screenshots of elements
2 parents 107a6e6 + 0f65824 commit 63ea088

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
@@ -741,6 +741,20 @@ public function _saveScreenshot($filename)
741741
}
742742
}
743743

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

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