1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui\Behat;
6:
7: use Atk4\Core\WarnDynamicPropertyTrait;
8: use Behat\Behat\Context\Context as BehatContext;
9: use Behat\Behat\Hook\Scope\AfterStepScope;
10: use Behat\MinkExtension\Context\RawMinkContext;
11: use Behat\Testwork\Tester\Result\TestResult;
12:
13: class DumpContext extends RawMinkContext implements BehatContext
14: {
15: use WarnDynamicPropertyTrait;
16:
17: /**
18: * Dump current page data when step failed for CI.
19: *
20: * @AfterStep
21: */
22: public function dumpPageAfterFailedStep(AfterStepScope $event): void
23: {
24: $session = $this->getMink()->getSession();
25:
26: if ($event->getTestResult()->getResultCode() === TestResult::FAILED) {
27: if ($session->getDriver() instanceof \Behat\Mink\Driver\Selenium2Driver) {
28: echo 'Dump of failed step:' . "\n";
29: echo 'Current page URL: ' . $session->getCurrentUrl() . "\n";
30: global $dumpPageCount;
31: if (++$dumpPageCount <= 1) { // prevent huge tests output
32: // upload screenshot here if needed in the future
33: // $screenshotData = $session->getScreenshot();
34: // echo 'Screenshot URL: ' . $screenshotUrl . "\n";
35: echo 'Page source: ' . $session->getPage()->getContent() . "\n";
36: } else {
37: echo 'Page source: Source code is dumped for the first failed step only.' . "\n";
38: }
39: }
40: }
41: }
42: }
43: