Completed
Push — develop ( a749c9...6ccf59 )
by Peter
01:43
created

ScreenshotTrait::takeScreenshot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 9.4285
1
<?php
2
/**
3
 * Webino (http://webino.sk/)
4
 *
5
 * @link        https://github.com/webino/WebinoDev/ for the canonical source repository
6
 * @copyright   Copyright (c) 2014-2016 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDev\Test\Selenium;
11
12
use Yandex\Allure\Adapter\Support\AttachmentSupport;
13
14
/**
15
 * Capture test screenshots
16
 */
17
trait ScreenshotTrait
18
{
19
    use AttachmentSupport;
20
21
    /**
22
     * @return string
23
     */
24
    abstract protected function getBrowser();
25
26
    /**
27
     * @return \PHPWebDriver_WebDriverSession|WebDriver\SessionInterface
28
     */
29
    abstract protected function getSession();
30
31
    /**
32
     * @return string
33
     */
34
    protected function screenshot()
35
    {
36
        if ('htmlunit' === $this->getBrowser()) {
37
            return '';
38
        }
39
40
        return base64_decode($this->getSession()->screenshot());
41
    }
42
43
    /**
44
     * @param string|null $caption
45
     * @return $this
46
     */
47
    protected function takeScreenshot($caption = null)
48
    {
49
        $data = $this->screenshot();
50
        if (empty($data)) {
51
            return $this;
52
        }
53
54
        $path = sys_get_temp_dir() . '/' . md5(__METHOD__) . '.png';
55
        file_put_contents($path, $data);
56
        $this->addAttachment($path, $caption, 'image/png');
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param string|null $caption
63
     * @return ScreenshotTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type ScreenshotTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
64
     * @todo remove
65
     * @deprecated use takeScreenshot()
66
     */
67
    protected function attachScreenshot($caption = null)
68
    {
69
        return $this->takeScreenshot($caption);
70
    }
71
}
72