Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record scenario screenshots for every step #69

Closed
xurizaemon opened this issue Jun 28, 2021 · 1 comment
Closed

Record scenario screenshots for every step #69

xurizaemon opened this issue Jun 28, 2021 · 1 comment

Comments

@xurizaemon
Copy link
Contributor

xurizaemon commented Jun 28, 2021

As a means to share step workflows with testers, I knocked together this extremely crude addition to ScreenshotContext this morning. It captures a screenshot of each step of a tagged scenario (probably erring on the side of too many screenshots, really), and means a tester can see the output frame by frame.

From here it would not be far to output a video artefact.

  use FeatureTrait, ScenarioTrait;

  /**
   * Record every step with a screenshot, if tagged with 'screenshot-vcr'.
   *
   * @AfterStep
   */
  public function recordStepScreenshots($event)
  {
    $tagName = 'screenshot-vcr';
    /** @var \Behat\Behat\Hook\Scope\BeforeStepScope $event */
    if (!$this->getScenario()->hasTag($tagName) && !$this->getFeature()->hasTag($tagName)) {
      return;
    }
    $driver = $this->getSession()->getDriver();
    $stepText = $event->getStep()->getText();
    $stepLine = $event->getFeature()->getFile() . '-' . $event->getStep()->getLine();
    $fileName = $this->makeFileName('html', preg_replace('/[^a-zA-Z0-9-]+/', '-', $stepLine . '-' . $stepText));
    try {
      $data = $driver->getContent();
    } catch (DriverException $exception) {
      // Do not do anything if the driver does not have any content - most
      // likely the page has not been loaded yet.
      return;
    }
    $this->saveScreenshotData($fileName, $data);
    try {
      $data = $driver->getScreenshot();
      // Preserve filename, but change the extension - this is to group
      // content and screenshot files together by name.
      $fileName = substr($fileName, 0, -1 * strlen('html')).'png';
      $this->saveScreenshotData($fileName, $data);
    } catch (UnsupportedDriverActionException $exception) {
      // Nothing to do here - drivers without support for screenshots
      // simply do not have them created.
    }
  }

Any thoughts on the approach @AlexSkrypnyk ? I'd love to contribute this in a way that makes sense alongside the rest of the project, or perhaps you know of existing solutions I'm not aware of.

Happy to turn into a PR if you want, but I don't feel it's at that level of quality yet 😁

@xurizaemon
Copy link
Contributor Author

Closing for drevops/behat-screenshot#40 which is in the correct project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant