4 * This file is part of the Behat MinkExtension.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\MinkExtension\Listener;
13 use Behat\Behat\EventDispatcher\Event\AfterStepTested;
14 use Behat\Behat\EventDispatcher\Event\StepTested;
15 use Behat\Testwork\Tester\Result\ExceptionResult;
16 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18 use Behat\Mink\Exception\Exception as MinkException;
21 * Failed step response show listener.
22 * Listens to failed Behat steps and shows last response in a browser.
24 * @author Konstantin Kudryashov <ever.zet@gmail.com>
26 class FailureShowListener implements EventSubscriberInterface
32 * Initializes initializer.
35 * @param array $parameters
37 public function __construct(Mink $mink, array $parameters)
40 $this->parameters = $parameters;
46 public static function getSubscribedEvents()
49 StepTested::AFTER => array('showFailedStepResponse', -10)
54 * Shows last response of failed step with preconfigured command.
56 * Configuration is based on `behat.yml`:
58 * `show_auto` enable this listener (default to false)
59 * `show_cmd` command to run (`open %s` to open default browser on Mac)
60 * `show_tmp_dir` folder where to store temp files (default is system temp)
62 * @param AfterStepTested $event
64 * @throws \RuntimeException if show_cmd is not configured
66 public function showFailedStepResponse(AfterStepTested $event)
68 $testResult = $event->getTestResult();
70 if (!$testResult instanceof ExceptionResult) {
74 if (!$testResult->getException() instanceof MinkException) {
78 if (null === $this->parameters['show_cmd']) {
79 throw new \RuntimeException('Set "show_cmd" parameter in behat.yml to be able to open page in browser (ex.: "show_cmd: open %s")');
82 $filename = rtrim($this->parameters['show_tmp_dir'], DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.uniqid().'.html';
83 file_put_contents($filename, $this->mink->getSession()->getPage()->getContent());
84 system(sprintf($this->parameters['show_cmd'], escapeshellarg($filename)));