What I Learned Today: Drupal Behat Breakpoints

Today I learned that the Drupal Behat Extension provides an extremely useful step definition for debugging. Breakpoints!

Then I break

It allows you to stop at a certain point in the test scenario so you can take a closer look at what is going on in the browser. When you are ready to move on, you just hit enter in your terminal window.

We were already saving snapshots of failed steps, but when this isn't enough; throw in a breakpoint!

/**
 * Take screenshot when step fails.
 * Works only with Selenium2Driver.
 *
 * @AfterStep
 */
public function takeScreenshotAfterFailedStep($event)
{
    if (4 === $event->getResult()) {
        $driver = $this->getSession()->getDriver();
        if (!($driver instanceof Selenium2Driver)) {
            //throw new UnsupportedDriverActionException('Taking screenshots is not supported by %s, use Selenium2Driver instead.', $driver);
            return;
        }
        $step = $event->getStep();
        $stepLine = $step->getLine();
        $fileName = '/tmp/stepAtLine' . $stepLine . '.png';
        $screenshot = $driver->getWebDriverSession()->screenshot();
        file_put_contents($fileName, base64_decode($screenshot));
        echo "Saved Screenshot To $fileName \n";
        $fileName = '/tmp/stepAtLine' . $stepLine .'.html';
        $source = $driver->getWebDriverSession()->source();
        file_put_contents($fileName, $source);
        echo "Saved Source To $fileName\n";
    }
}

Comments

That looks really cool, but I don't see how your examples connect. Could you give a more fleshed out example behat script that uses a breakpoint and takes a screenshot?

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Ready for transformation?