Blog

What I Learned Today: Drupal Behat Breakpoints

Written by Metal Toad Staff | Oct 31, 2013 12:00:00 AM

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";
    }
}
This post is part of my challenge to never stop learning.