forked from DigitalProducts/codeception-module-visualception
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathScreenshot.php
More file actions
executable file
·46 lines (33 loc) · 1.63 KB
/
Copy pathScreenshot.php
File metadata and controls
executable file
·46 lines (33 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
class Screenshot
{
private $webDriver;
public function __construct(\RemoteWebDriver $webDriver)
{
$this->webDriver = $webDriver;
}
public function takeScreenshot($jqueryIdentifier = "body")
{
$image = new \Imagick();
$image->readimageblob($this->webDriver->takeScreenshot());
$coords = $this->getCoordinates($jqueryIdentifier);
$image->cropImage($coords['width'], $coords['height'], $coords['offset_x'], $coords['offset_y']);
return $image;
}
private function getCoordinates($jqueryIdentifier)
{
$jQueryString = file_get_contents(__DIR__ . "/jquery.js");
$this->webDriver->executeScript($jQueryString);
$this->webDriver->executeScript('jQuery.noConflict();');
$imageCoords = array();
$elementExists = (bool)$this->webDriver->executeScript('return jQuery( "' . $jqueryIdentifier . '" ).length > 0;');
if (!$elementExists) {
throw new \Exception("The element you want to examine ('" . $jqueryIdentifier . "') was not found.");
}
$imageCoords['offset_x'] = (string)$this->webDriver->executeScript('return jQuery( "' . $jqueryIdentifier . '" ).offset().left;');
$imageCoords['offset_y'] = (string)$this->webDriver->executeScript('return jQuery( "' . $jqueryIdentifier . '" ).offset().top;');
$imageCoords['width'] = (string)$this->webDriver->executeScript('return jQuery( "' . $jqueryIdentifier . '" ).width();');
$imageCoords['height'] = (string)$this->webDriver->executeScript('return jQuery( "' . $jqueryIdentifier . '" ).height();');
return $imageCoords;
}
}