1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bex\Behat\ScreenshotExtension\Service; |
4
|
|
|
|
5
|
|
|
use Behat\Mink\Mink; |
6
|
|
|
use Bex\Behat\ScreenshotExtension\Driver\ImageDriverInterface; |
7
|
|
|
use Bex\Behat\ScreenshotExtension\ServiceContainer\Config; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class is responsible for taking screenshot by using the Mink session |
12
|
|
|
* |
13
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License |
14
|
|
|
*/ |
15
|
|
|
class ScreenshotTaker |
16
|
|
|
{ |
17
|
|
|
/** @var Mink $mink */ |
18
|
|
|
private $mink; |
19
|
|
|
|
20
|
|
|
/** @var OutputInterface $output */ |
21
|
|
|
private $output; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array $screenshots |
25
|
|
|
*/ |
26
|
|
|
private $screenshots; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Config $config |
30
|
|
|
*/ |
31
|
|
|
private $config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Mink $mink |
35
|
|
|
* @param OutputInterface $output |
36
|
|
|
* @param Config $config |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Mink $mink, OutputInterface $output, Config $config) |
39
|
|
|
{ |
40
|
|
|
$this->mink = $mink; |
41
|
|
|
$this->output = $output; |
42
|
|
|
$this->config = $config; |
43
|
|
|
$this->screenshots = []; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Save the screenshot into a local buffer |
48
|
|
|
*/ |
49
|
|
|
public function takeScreenshot() |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
$this->screenshots[] = $this->mink->getSession()->getScreenshot(); |
53
|
|
|
} catch (\Exception $e) { |
54
|
|
|
$this->output->writeln($e->getMessage()); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return boolean |
60
|
|
|
*/ |
61
|
|
|
public function hasScreenshot() |
62
|
|
|
{ |
63
|
|
|
return !empty($this->screenshots); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getImage() |
70
|
|
|
{ |
71
|
|
|
return $this->config->shouldCombineImages() ? $this->getCombinedImage() : $this->getLastImage(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
private function getCombinedImage() |
78
|
|
|
{ |
79
|
|
|
$im = new \Imagick(); |
80
|
|
|
|
81
|
|
|
foreach ($this->screenshots as $screenshot) { |
82
|
|
|
$im->readImageBlob($screenshot); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/* Append the images into one */ |
86
|
|
|
$im->resetIterator(); |
87
|
|
|
$combined = $im->appendImages(true); |
88
|
|
|
|
89
|
|
|
/* Output the image */ |
90
|
|
|
$combined->setImageFormat("png"); |
91
|
|
|
|
92
|
|
|
return (string) $combined; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
private function getLastImage() |
99
|
|
|
{ |
100
|
|
|
return end($this->screenshots); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Remove previous images |
105
|
|
|
*/ |
106
|
|
|
public function reset() |
107
|
|
|
{ |
108
|
|
|
$this->screenshots = []; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|