This repository was archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathDebugContext.php
More file actions
86 lines (74 loc) · 2.42 KB
/
Copy pathDebugContext.php
File metadata and controls
86 lines (74 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Sanpi\Behatch\Context;
use Behat\Gherkin\Node\StepNode;
use Behat\Behat\Hook\Scope\AfterStepScope;
class DebugContext extends BaseContext
{
private $screenshotDir;
public function __construct($screenshotDir = './screenshots')
{
$this->screenshotDir = $screenshotDir;
}
/**
* Pauses the scenario until the user presses a key. Useful when debugging a scenario.
* Example: When I put a breakpoint
* Example: Then I put a breakpoint
* Example: And I put a breakpoint
*
* @Then (I )put a breakpoint
*/
public function iPutABreakpoint()
{
fwrite(STDOUT, "\033[s \033[93m[Breakpoint] Press \033[1;93m[RETURN]\033[0;93m to continue...\033[0m");
while (fgets(STDIN, 1024) == '') {}
fwrite(STDOUT, "\033[u");
return;
}
/**
* Saving a screenshot
* Example: When I save a screenshot in "logInView"
* Example: Then I save a screenshot in "logInView"
* Example: And I save a screenshot in "logInView"
*
* @When I save a screenshot in :filename
*/
public function iSaveAScreenshotIn($filename)
{
sleep(1);
$this->saveScreenshot($filename, $this->screenshotDir);
}
/**
* @AfterStep
*/
public function failScreenshots(AfterStepScope $scope)
{
if (!$scope->getTestResult()->isPassed()) {
$scenario = $this->getScenario($scope);
if ($scenario->hasTag('javascript')) {
$scenarioName = urlencode(str_replace(' ', '_', $scope->getSuite()->getName()));
$filename = sprintf('fail_%s_%s.png', time(), $scenarioName);
$this->saveScreenshot($filename, $this->screenshotDir);
}
}
}
/**
* @param AfterStepScope $scope
* @return \Behat\Gherkin\Node\ScenarioInterface
*/
private function getScenario(AfterStepScope $scope)
{
$scenarios = $scope->getFeature()->getScenarios();
foreach ($scenarios as $scenario) {
$stepLinesInScenario = array_map(
function (StepNode $step) {
return $step->getLine();
},
$scenario->getSteps()
);
if (in_array($scope->getStep()->getLine(), $stepLinesInScenario)) {
return $scenario;
}
}
throw new \LogicException('Unable to find the scenario');
}
}