Passed
Pull Request — master (#247)
by benjamin
02:26
created

DebugContext::saveScreenshot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Behatch\Context;
4
5
use Behat\Gherkin\Node\StepNode;
6
use Behat\Behat\Hook\Scope\AfterStepScope;
7
use Behat\Mink\Exception\UnsupportedDriverActionException;
8
9
class DebugContext extends BaseContext
10
{
11
    /**
12
     * @var string
13
     */
14
    private $screenshotDir;
15
16
    public function __construct($screenshotDir = '.')
17
    {
18
        $this->screenshotDir = $screenshotDir;
19
    }
20
21
    /**
22
     * Pauses the scenario until the user presses a key. Useful when debugging a scenario.
23
     *
24
     * @Then (I )put a breakpoint
25
     */
26
    public function iPutABreakpoint()
27
    {
28
        fwrite(STDOUT, "\033[s    \033[93m[Breakpoint] Press \033[1;93m[RETURN]\033[0;93m to continue...\033[0m");
29
        while (fgets(STDIN, 1024) == '') {
0 ignored issues
show
Unused Code introduced by
This while loop is empty and can be removed.

This check looks for while loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
30
        }
31
        fwrite(STDOUT, "\033[u");
32
33
        return;
34
    }
35
36
    /**
37
     * Saving a screenshot
38
     *
39
     * @When I save a screenshot in :filename
40
     */
41
    public function iSaveAScreenshotIn($filename)
42
    {
43
        sleep(1);
44
        $this->saveScreenshot($filename, $this->screenshotDir);
45
    }
46
47
    /**
48
     * @AfterStep
49
     */
50
    public function failScreenshots(AfterStepScope $scope)
51
    {
52
        if ($scope->getTestResult()->isPassed()) {
53
            return;
54
        }
55
56
        $suiteName      = urlencode(str_replace(' ', '_', $scope->getSuite()->getName()));
57
        $featureName    = urlencode(str_replace(' ', '_', $scope->getFeature()->getTitle()));
58
59
        if ($this->getBackground($scope)) {
60
            $scenarioName   = 'background';
61
        } else {
62
            $scenario       = $this->getScenario($scope);
63
            $scenarioName   = urlencode(str_replace(' ', '_', $scenario->getTitle()));
64
        }
65
66
        $filename = sprintf('fail_%s_%s_%s_%s.png', time(), $suiteName, $featureName, $scenarioName);
67
        $this->saveScreenshot($filename, $this->screenshotDir);
68
    }
69
70
    /**
71
     * @param AfterStepScope $scope
72
     * @return \Behat\Gherkin\Node\ScenarioInterface
73
     */
74
    private function getScenario(AfterStepScope $scope)
75
    {
76
        $scenarios = $scope->getFeature()->getScenarios();
77
        foreach ($scenarios as $scenario) {
78
            $stepLinesInScenario = array_map(
79
                function (StepNode $step) {
80
                    return $step->getLine();
81
                },
82
                $scenario->getSteps()
83
            );
84
            if (in_array($scope->getStep()->getLine(), $stepLinesInScenario)) {
85
                return $scenario;
86
            }
87
        }
88
89
        throw new \LogicException('Unable to find the scenario');
90
    }
91
92
    /**
93
     * @param AfterStepScope $scope
94
     * @return \Behat\Gherkin\Node\BackgroundNode|bool
95
     */
96
    private function getBackground(AfterStepScope $scope)
97
    {
98
        $background = $scope->getFeature()->getBackground();
99
        if(!$background){
100
            return false;
101
        }
102
        $stepLinesInBackground = array_map(
103
            function (StepNode $step) {
104
                return $step->getLine();
105
            },
106
            $background->getSteps()
107
        );
108
        if (in_array($scope->getStep()->getLine(), $stepLinesInBackground)) {
109
            return $background;
110
        }
111
112
        return false;
113
    }
114
115
    public function saveScreenshot($filename = null, $filepath = null)
116
    {
117
        try {
118
            parent::saveScreenshot($filename, $filepath);
119
        } catch (UnsupportedDriverActionException $e) {
120
            return;
121
        }
122
    }
123
}
124