|
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
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Save the screenshot into a local buffer |
|
47
|
|
|
*/ |
|
48
|
|
|
public function takeScreenshot() |
|
49
|
|
|
{ |
|
50
|
|
|
try { |
|
51
|
|
|
$this->screenshots[] = $this->mink->getSession()->getScreenshot(); |
|
52
|
|
|
} catch (\Exception $e) { |
|
53
|
|
|
$this->output->writeln($e->getMessage()); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getImage() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->config->shouldCombineImages() ? $this->getCombinedImage() : $this->getLastImage(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
private function getCombinedImage() |
|
69
|
|
|
{ |
|
70
|
|
|
$im = new \Imagick(); |
|
71
|
|
|
|
|
72
|
|
|
foreach ($this->screenshots as $screenshot) { |
|
73
|
|
|
$im->readImageBlob($screenshot); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/* Append the images into one */ |
|
77
|
|
|
$im->resetIterator(); |
|
78
|
|
|
$combined = $im->appendImages(true); |
|
79
|
|
|
|
|
80
|
|
|
/* Output the image */ |
|
81
|
|
|
$combined->setImageFormat("png"); |
|
82
|
|
|
|
|
83
|
|
|
return (string) $combined; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
private function getLastImage() |
|
90
|
|
|
{ |
|
91
|
|
|
return end($this->screenshots); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Remove previous images |
|
96
|
|
|
*/ |
|
97
|
|
|
public function reset() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->screenshots = []; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|