/** * Listens to "step.after" event. * * @param StepEvent $event * * @uses printStep() */ public function afterStep(StepEvent $event) { if ($this->inBackground && $this->isBackgroundPrinted) { return; } if ($this->isStepChainParent($event->getStep())) { $isStepChainParent = true; $this->inChain = false; } if (!$this->inBackground && $this->inOutlineExample) { $this->delayedStepEvents[] = $event; return; } if ($this->inChain) { $this->chainedSteps[] = $event; return; } $this->printStep($event->getStep(), $event->getResult(), $event->getDefinition(), $event->getSnippet(), $event->getException()); if (isset($this->chainedSteps) && count($this->chainedSteps) && isset($isStepChainParent) && $isStepChainParent) { foreach ($this->chainedSteps as $event) { $this->write($this->indent); $this->printStep($event->getStep(), $event->getResult(), $event->getDefinition(), $event->getSnippet(), $event->getException()); } $this->chainedSteps = []; } }
/** * Take a screenshot when a step fails * * @param StepEvent $event * * @AfterStep */ public function takeScreenshotAfterFailedStep(StepEvent $event) { if ($event->getResult() === StepEvent::FAILED) { $driver = $this->getSession()->getDriver(); $rootDir = dirname($this->getParameter('kernel.root_dir')); $filePath = $event->getLogicalParent()->getFile(); $stepStats = ['scenario_file' => substr($filePath, strlen($rootDir) + 1), 'scenario_line' => $event->getLogicalParent()->getLine(), 'scenario_label' => $event->getLogicalParent()->getTitle(), 'exception' => $event->getException()->getMessage(), 'step_line' => $event->getStep()->getLine(), 'step_label' => $event->getStep()->getText(), 'status' => 'failed']; if ($driver instanceof Selenium2Driver) { $dir = getenv('WORKSPACE'); $buildUrl = getenv('BUILD_URL'); if (false !== $dir) { $dir = sprintf('%s/app/build/screenshots', $dir); } else { $dir = '/tmp/behat/screenshots'; } $lineNum = $event->getStep()->getLine(); $filename = strstr($event->getLogicalParent()->getFile(), 'features/'); $filename = sprintf('%s.%d.png', str_replace('/', '__', $filename), $lineNum); $path = sprintf('%s/%s', $dir, $filename); $fs = new \Symfony\Component\Filesystem\Filesystem(); $fs->dumpFile($path, $driver->getScreenshot()); if (false !== $dir) { $path = sprintf('%s/artifact/app/build/screenshots/%s', $buildUrl, $filename); } $stepStats['screenshot'] = $path; $this->getMainContext()->addErrorMessage("Step {$lineNum} failed, screenshot available at {$path}"); } if ('JENKINS' === getenv('BEHAT_CONTEXT')) { echo sprintf("[1;37m##glados_step##%s##glados_step##[0m\n", json_encode($stepStats)); } } }
/** * Shows last response of failed step with preconfigured command. * Configuration is based on `behat.yml`: * * `show_auto` enable this listener (default to false) * `show_cmd` command to run (`open %s` to open default browser on Mac) * `show_tmp_dir` folder where to store temp files (default is system temp) * * @param StepEvent $event */ public function showFailedStepResponse($event) { if (StepEvent::FAILED !== $event->getResult()) { return; } if (!$event->getException() instanceof MinkException) { return; } if (null === $this->parameters['show_cmd']) { throw new \RuntimeException('Set "show_cmd" parameter in behat.yml to be able to open page in browser (ex.: "show_cmd: open %s")'); } $filename = rtrim($this->parameters['show_tmp_dir'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . uniqid() . '.html'; file_put_contents($filename, $this->mink->getSession()->getPage()->getContent()); system(sprintf($this->parameters['show_cmd'], escapeshellarg($filename))); }
/** * Return recorded events * * @return array */ public function getEvents() { $events = $this->getEventRecorder()->rip(); foreach ($events as $key => $eventTurple) { list($name, $event) = $eventTurple; if ($event instanceof StepEvent) { $event = new StepEvent($event->getStep(), $event->getLogicalParent(), new NullContext(), $event->getResult(), null, $event->getException() ? new WorkerException($event->getException()->getMessage()) : null, $event->getSnippet()); } if ($event instanceof OutlineExampleEvent) { $event = new OutlineExampleEvent($event->getOutline(), $event->getIteration(), new NullContext(), $event->getResult(), $event->isSkipped()); } if ($event instanceof ScenarioEvent) { $event = new ScenarioEvent($event->getScenario(), new NullContext(), $event->getResult(), $event->isSkipped()); } $event->setDispatcher(new NullEventDispatcher()); $events[$key] = array($name, $event); } return $events; }
/** * Listens to "step.after" event. * * @param StepEvent $event */ public function afterStep(StepEvent $event) { if ($event->hasException()) { $this->exceptions[] = $event->getException(); $this->exceptionsCount++; } ++$this->stepsCount; }
/** * Listens to "step.after" event. * * @param StepEvent $event * * @uses printStep() */ public function afterStep(StepEvent $event) { if ($this->inBackground && $this->isBackgroundPrinted) { return; } if (!$this->inBackground && $this->inOutlineExample) { $this->delayedStepEvents[] = $event; return; } $this->printStep($event->getStep(), $event->getResult(), $event->getDefinition(), $event->getSnippet(), $event->getException()); }
/** * Converts the behat-Result to the serverSide format. * * @param StepEvent $event The event containing the behat StepResult. * @return stub_php\stepResult */ public function convertResultToStepResult(StepEvent $event) { switch ($event->getResult()) { case StepEvent::PASSED: return stub_php\stepResult::SUCCESS; case StepEvent::UNDEFINED: case StepEvent::PENDING: case StepEvent::SKIPPED: return stub_php\stepResult::SKIPPED; case StepEvent::FAILED: if ($event->hasException() && !$this->_isAssertionError($event->getException())) { return stub_php\stepResult::ERROR; } else { return stub_php\stepResult::FAILURE; } default: return stub_php\stepResult::ERROR; } }
/** * Listens to "step.after" event. * * @param Behat\Behat\Event\StepEvent $event * * @uses printStep() */ public function afterStep(StepEvent $event) { $this->printStep($event->getStep(), $event->getResult(), $event->getDefinition(), $event->getSnippet(), $event->getException()); }
public function afterStep(StepEvent $event) { if (!$this->enabled) { return; } if (in_array($event->getResult(), $this->ignoredStates)) { return; } // screenshot not needed for these states $file = $event->getStep()->getParent()->getFeature()->getFile(); $line = $event->getStep()->getLine(); $filename = str_replace('/', '_', ltrim(str_replace($this->rootDir, '', $file), '/')) . '_' . $line . '.png'; /** @var BehatContext $ctx */ $ctx = $event->getContext(); $screenData = $ctx->getSession()->currentScreenshot(); file_put_contents($this->outDir . '/' . $filename, $screenData); $this->currentReport['steps'][] = array('text' => $event->getStep()->getText(), 'file' => $filename, 'result' => $event->getResult(), 'exception' => ($ex = $event->getException()) ? $ex->getMessage() : NULL); }
/** * Listens to "step.after" event. * * @param StepEvent $event */ public function afterStep(StepEvent $event) { if ($event->hasException()) { if ($event->getResult() === StepEvent::SKIPPED || $event->getResult() === StepEvent::PENDING || $event->getResult() === StepEvent::UNDEFINED) { $this->pendingCount++; } else { $this->exceptions[] = $event->getException(); $this->exceptionsCount++; $this->failureCount++; } } ++$this->stepsCount; ++$this->scenarioStepsCount; }