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

add screenshots and html dumps for debugging #33

Open
becw opened this issue Feb 14, 2017 · 0 comments
Open

add screenshots and html dumps for debugging #33

becw opened this issue Feb 14, 2017 · 0 comments
Assignees

Comments

@becw
Copy link
Member

becw commented Feb 14, 2017

Some of these things will be useful...

<?php

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\AfterStepScope;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ExpectationException;
use Drupal\DrupalExtension\Context\DrupalContext;
use Drupal\DrupalExtension\Context\MinkContext;
use Drupal\DrupalExtension\Context\RawDrupalContext;

/**
 * Defines application features from the specific context.
 */
class FeatureContext extends RawDrupalContext implements SnippetAcceptingContext
{
    /** @var String The directory to save screenshots and html to. */
    private $debug_dir;

    /**
     * @var MinkContext
     */
    private $minkContext;

    /**
     * @var DrupalContext
     */
    private $drupalContext;

    /**
     * @var array of tid's for Forums created in tests.
     */
    private $forums = [];

    /**
     * Initializes context.
     */
    public function __construct()
    {
        $this->debug_dir = __DIR__ . '/../../debug/';
    }

    /**
     * @AfterStep
     *
     * @param \Behat\Behat\Hook\Scope\AfterStepScope $event
     */
    public function printLastResponseOnError(AfterStepScope $event)
    {
        if (!$event->getTestResult()->isPassed()) {
            $this->saveHtml();
        }
    }

    /**
     * Grab the html of the page and save it.
     *
     * @Then save the html for the page
     * @Then save the html for the page with prefix :prefix
     *
     * @param string $prefix A string to prepend to the filename.
     */
    public function saveHtml($prefix = 'html') {
        $html_data = $this->getSession()->getDriver()->getContent();
        $filename = $this->debug_dir . $prefix . '-' .  time() . '.html';
        file_put_contents($filename, $html_data);
    }

    /**
     * Grab a screenshot of the page and save it.
     *
     * @Then save a screenshot of the page
     * @Then save a screenshot of the page with prefix :prefix
     *
     * @param string $prefix A string to prepend to the filename.
     */
    public function saveAScreenshot($prefix = 'screenshot')
    {
        $screenshot = $this->getSession()->getDriver()->getScreenshot();
        $filename = $this->debug_dir . $prefix . '-' . time() . '.png';
        file_put_contents($filename, $screenshot);
    }

    /**
     * @BeforeScenario
     */
    public function gatherContexts(BeforeScenarioScope $scope)
    {
        $environment = $scope->getEnvironment();
        $this->drupalContext = $environment->getContext(DrupalContext::class);
        $this->minkContext = $environment->getContext(MinkContext::class);
    }

  /**
   * Click on the element with the provided xpath query
   *
   * @When /^(?:|I )click on the element "([^"]*)"$/
   */
  public function iClickOnTheElement($arg)
  {
      $session = $this->getSession(); // get the mink session
      $element = $session->getPage()->find('css', $arg); // runs the actual query and returns the element

      // errors must not pass silently
      if (null === $element) {
          throw new \InvalidArgumentException(sprintf('Could not evaluate CSS selector: "%s"', $arg));
      }

      // ok, let's click on it
      $element->click();
  }

  /**
   * Some forms do not have a Submit button just pass the ID
   *
   * @Given /^I submit the form with id "([^"]*)"$/
   */
  public function iSubmitTheFormWithId($arg) {
      $session = $this->getSession();
      $element = $session->getPage()->find('css', $arg);
      if($element) {
          $element->submit();
      } else {
          throw new Exception('Element not found');
      }
  }

  /**
   * @param int $seconds
   *   Amount of seconds when nothing to happens.
   *
   * @Given /^(?:|I )wait (\d+) seconds$/
   */
  public function waitSeconds($seconds)
  {
    sleep($seconds);
  }
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

2 participants