使用Mocha的Selenium Java Script Webdriver - 如果测试失败,则截取屏幕截图

时间:2016-03-29 20:59:29

标签: selenium-webdriver mocha

使用Selenium webdriver(Java Script)和Mocha

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

如果测试失败,我想使用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');
            });
        }
    });

运行测试后,如果条件返回true。但它没有创建截图。

1 个答案:

答案 0 :(得分:2)

请参阅https://github.com/webdriverio/webdriverio/issues/269#issuecomment-306342170 - afterTest使用browser.saveScreenshot!test.passed wdio.conf.js