Completed
Push — develop ( ffb73f...d62956 )
by Peter
14:45
created

ScreenshotTrait::takeScreenshot()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 1
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
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-2017 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDev\Test\Selenium;
11
12
/**
13
 * Capture test screenshots
14
 */
15
trait ScreenshotTrait
16
{
17
    /**
18
     * Taken screenshots directory
19
     *
20
     * @var string
21
     */
22
    protected $screenshotsDir;
23
24
    /**
25
     * @return string
26
     */
27
    abstract protected function getBrowser();
28
29
    /**
30
     * @return \PHPWebDriver_WebDriverSession|WebDriver\SessionInterface
31
     */
32
    abstract protected function getSession();
33
34
    /**
35
     * @return string
36
     */
37
    protected function screenshot()
38
    {
39
        if ('htmlunit' === $this->getBrowser()) {
40
            return '';
41
        }
42
43
        return base64_decode($this->getSession()->screenshot());
44
    }
45
46
    /**
47
     * @param string|null $imgName
48
     * @return $this
49
     */
50
    protected function takeScreenshot($imgName = null)
51
    {
52
        $data = $this->screenshot();
53
        if (empty($data)) {
54
            return $this;
55
        }
56
57
        if (empty($this->screenshotsDir) || !file_exists($this->screenshotsDir)) {
58
            return $this;
59
        }
60
61
        if (empty($imgName)) {
62
            $imgName = count(glob($this->screenshotsDir . '/*.png')) + 1;
63
        }
64
65
        $path = $this->screenshotsDir . '/' . $imgName . '.png';
66
        file_put_contents($path, $data);
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param string|null $caption
73
     * @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...
74
     * @todo remove
75
     * @deprecated use takeScreenshot()
76
     */
77
    protected function attachScreenshot($caption = null)
78
    {
79
        return $this->takeScreenshot($caption);
80
    }
81
}
82