Patrick Kenny
Patrick Kenny

Reputation: 6417

How to leave the browser open when a Behat/Mink test fails

I'm using the selenium2 driver to test my Drupal site using Behat/Mink in a docker container.

Using the Selenium Standalone-Chrome container, I can watch my behat tests fail, but the problem is that as soon as they fail, the browser is closed, which makes it harder for me to see what the problem is.

I'm running my tests like this:

behat --tags '@mystuff' --config=behat-myconfig.yml --strict --stop-on-failure

Is there a way to leave the remote-controlled browser open even when a test fails?

Upvotes: 0

Views: 1012

Answers (3)

Samuel Vicent
Samuel Vicent

Reputation: 1031

You can take a screenshot whenever an error occurs using Behat hook "AfterStep". Consider having a look at the Panther Driver or DChrome Driver. Here you are a shortened example considering also non javascript tests (which are faster):

use Behat\Mink\Driver\Selenium2Driver;

/** Context Class Definition ... */

/**
 * @AfterStep
 */
public function takeScreenShotAfterFailedStep(AfterStepScope $scope)
{
    if (99 !== $scope->getTestResult()->getResultCode()) {
        return;
    }
    $this->takeAScreenShot('error');
}


private function takeAScreenShot($prefix = 'screenshot')
{
    $baseName= sprintf('PATH_FOR_YOUR_SCREENSHOTS/%s-%s', $prefix, (new \DateTime())->format('Y_m_d_H_i_s'));

    if ($this->supportsJavascript()) {
        $extension = '.png';
        $content = $this->session->getScreenshot();
    } else {
        $extension = '.html';
        $content = $this->getSession()->getPage()->getOuterHtml();
    }
    file_put_contents(sprintf('%s%s', $baseName, $extension), $content);
}

private function supportsJavascript()
{
    return $this->getSession()->getDriver() instanceof Selenium2Driver;
}

Upvotes: 0

Patrick Kenny
Patrick Kenny

Reputation: 6417

Finally I found a good solution for this: behat-fail-aid.

Add the fail aid to your FeatureContext and then run behat with the --wait-on-failure option:

the --wait-on-failure={seconds} option can be used to investigate/inspect failures in the browser.

Upvotes: 0

lauda
lauda

Reputation: 4173

By default it is not possible.

Maybe you could find some hack to do it but it is not recommended, since each scenario should be isolated and this is not a good solution at least when running some suite with multiple tests.

For one time only see if you can use the logic for printscreen and use a breakpoint instead.

Anyway, you should use a verbose (-vvv for Behat 3) output + ide debugger to debug your code.

Upvotes: 1

Related Questions