Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Java Script Webdriver using Mocha - take screenshot if test fails

Using Selenium webdriver (Java Script) and Mocha

var assert = require('assert'),
test = require('selenium-webdriver/testing'),
until = require('selenium-webdriver').until,
webdriver = require('selenium-webdriver');

If test fails, I want to take a screen shot using after function from Mocha:

    function writeScreenshot(data, name) {
        name = name || 'ss.png';

        var screenshotPath = '/result/';

        fs.writeFileSync(screenshotPath + name, data, 'base64');
    };

    afterEach(function () {
        if (this.currentTest.state == 'failed') {
            console.log("If condition");
            driver.takeScreenshot().then(function (data) {
                writeScreenshot(data, 'failed.png');
            });
        }
    });

After running the test, if condition returned true. But it does not create a screenshot.

like image 897
SUM Avatar asked Mar 29 '16 20:03

SUM


People also ask

How does Selenium handle failed test cases?

Right click on the project and choose Refresh. On refreshing, the tester will see a test-output folder as shown below. This test-output folder comprises various files that include failed test cases as well.


1 Answers

See https://github.com/webdriverio/webdriverio/issues/269#issuecomment-306342170 - use afterTest and browser.saveScreenshot if !test.passed in wdio.conf.js

like image 133
Jakub Holý Avatar answered Oct 09 '22 14:10

Jakub Holý