Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit dab6507

Browse files
sapzapeOndraM
authored andcommitted
Add take element screenshot command
1 parent 0bf4f38 commit dab6507

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

lib/Remote/DriverCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class DriverCommand
149149
// W3C specific
150150
const ACTIONS = 'actions';
151151
const GET_ELEMENT_PROPERTY = 'getElementProperty';
152+
const TAKE_ELEMENT_SCREENSHOT = 'takeElementScreenshot';
152153

153154
private function __construct()
154155
{

lib/Remote/HttpCommandExecutor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ class HttpCommandExecutor implements WebDriverCommandExecutor
129129
DriverCommand::STATUS => ['method' => 'GET', 'url' => '/status'],
130130
DriverCommand::SUBMIT_ELEMENT => ['method' => 'POST', 'url' => '/session/:sessionId/element/:id/submit'],
131131
DriverCommand::SCREENSHOT => ['method' => 'GET', 'url' => '/session/:sessionId/screenshot'],
132+
DriverCommand::TAKE_ELEMENT_SCREENSHOT => [
133+
'method' => 'GET',
134+
'url' => '/session/:sessionId/element/:id/screenshot',
135+
],
132136
DriverCommand::TOUCH_SINGLE_TAP => ['method' => 'POST', 'url' => '/session/:sessionId/touch/click'],
133137
DriverCommand::TOUCH_DOWN => ['method' => 'POST', 'url' => '/session/:sessionId/touch/down'],
134138
DriverCommand::TOUCH_DOUBLE_TAP => ['method' => 'POST', 'url' => '/session/:sessionId/touch/doubleclick'],

lib/Remote/RemoteWebElement.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,27 @@ public function getID()
431431
return $this->id;
432432
}
433433

434+
/**
435+
* Take screenshot of a specific element.
436+
*
437+
* @param string $save_as The path of the screenshot to be saved.
438+
* @return string The screenshot in PNG format.
439+
*/
440+
public function takeElementScreenshot($save_as = null)
441+
{
442+
$screenshot = base64_decode(
443+
$this->executor->execute(
444+
DriverCommand::TAKE_ELEMENT_SCREENSHOT,
445+
[':id' => $this->id]
446+
)
447+
);
448+
if ($save_as) {
449+
file_put_contents($save_as, $screenshot);
450+
}
451+
452+
return $screenshot;
453+
}
454+
434455
/**
435456
* Test if two element IDs refer to the same DOM element.
436457
*

lib/WebDriverElement.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,13 @@ public function submit();
132132
* @return string The opaque ID.
133133
*/
134134
public function getID();
135+
136+
/**
137+
* Take screenshot of a specific element.
138+
*
139+
* @param string $save_as The path of the screenshot to be saved.
140+
* @return string The screenshot in PNG format.
141+
* @todo Add in next major release (BC)
142+
*/
143+
//public function takeElementScreenshot($save_as = null);
135144
}

0 commit comments

Comments
 (0)