简体   繁体   English

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

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

Using Selenium webdriver (Java Script) and Mocha 使用Selenium webdriver(Java Script)和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: 如果测试失败,我想使用Mocha的after函数进行屏幕截图:

    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. 运行测试后,如果条件返回true。 But it does not create a screenshot. 但它没有创建截图。

请参阅https://github.com/webdriverio/webdriverio/issues/269#issuecomment-306342170 - 如果在wdio.conf.js使用!test.passed ,请使用afterTestbrowser.saveScreenshot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ReportNG在Selenium WebDriver中拍摄失败的测试的屏幕截图 - How to take screenshot of failed test in selenium webdriver using ReportNG 无法使用HtmlUnitDriver进行screenShot [Selenium WebDriver java] - Not able to take screenShot using HtmlUnitDriver [Selenium WebDriver java] JUnit测试失败时,无法使用Java使用Selenium WebDriver截屏 - Fail to take screenshots with Selenium WebDriver using Java when a JUnit test fails Selenium / TestNG:“无法使用Selenium Webdriver拍摄失败的测试用例的屏幕截图 - Selenium / TestNG: 'Unable to take a screenshot of Failed test case using Selenium Webdriver Selenium Webdriver (node.js) 截图并保存测试结果 - Selenium Webdriver (node.js) take screenshot and save test results 截屏,然后在Selenium Webdriver中将其删除 - take screenshot then delete it in selenium webdriver 无法在 selenium webdriver 中截取屏幕截图 - Unable to take screenshot in selenium webdriver 如何使用带有 Java 的 Selenium WebDriver 获取可滚动网页的整页屏幕截图? - How to take full page Screenshot of a scrollable webpage using Selenium WebDriver with Java? 如何使用 selenium java 进行彩色截图 - how to take color screenshot using selenium java 如何在Selenium Webdriver中使用Robot Framework截屏 - How to Take screenshot using Robot Framework in selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM