1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bex\Behat\ScreenshotExtension\Service; |
4
|
|
|
|
5
|
|
|
use Behat\Mink\Mink; |
6
|
|
|
use Bex\Behat\ScreenshotExtension\Driver\ImageDriverInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class is responsible for taking screenshot by using the Mink session |
11
|
|
|
* |
12
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License |
13
|
|
|
*/ |
14
|
|
|
class ScreenshotTaker |
15
|
|
|
{ |
16
|
|
|
/** @var Mink $mink */ |
17
|
|
|
private $mink; |
18
|
|
|
|
19
|
|
|
/** @var OutputInterface $output */ |
20
|
|
|
private $output; |
21
|
|
|
|
22
|
|
|
/** @var ImageDriverInterface[] $imageDrivers */ |
23
|
|
|
private $imageDrivers; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
* |
28
|
|
|
* @param Mink $mink |
29
|
|
|
* @param OutputInterface $output |
30
|
|
|
* @param ImageDriverInterface[] $imageDrivers |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Mink $mink, OutputInterface $output, array $imageDrivers) |
33
|
|
|
{ |
34
|
|
|
$this->mink = $mink; |
35
|
|
|
$this->output = $output; |
36
|
|
|
$this->imageDrivers = $imageDrivers; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Save the screenshot as the given filename |
41
|
|
|
* |
42
|
|
|
* @param string $fileName |
43
|
|
|
*/ |
44
|
|
|
public function takeScreenshot($fileName = 'failure.png') |
45
|
|
|
{ |
46
|
|
|
try { |
47
|
|
|
$screenshot = $this->mink->getSession()->getScreenshot(); |
48
|
|
|
|
49
|
|
|
foreach ($this->imageDrivers as $imageDriver) { |
50
|
|
|
$imageUrl = $imageDriver->upload($screenshot, $fileName); |
51
|
|
|
$this->printImageLocation($imageUrl); |
52
|
|
|
} |
53
|
|
|
} catch (\Exception $e) { |
54
|
|
|
$this->output->writeln($e->getMessage()); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $imageUrl |
60
|
|
|
*/ |
61
|
|
|
private function printImageLocation($imageUrl) |
62
|
|
|
{ |
63
|
|
|
$message = sprintf( |
64
|
|
|
'<comment>Screenshot has been taken. Open image at <error>%s</error></comment>', |
65
|
|
|
$imageUrl |
66
|
|
|
); |
67
|
|
|
$options = $this->output->isDecorated() ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_PLAIN; |
68
|
|
|
|
69
|
|
|
$this->output->writeln($message, $options); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|