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.
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.
See https://github.com/webdriverio/webdriverio/issues/269#issuecomment-306342170 - use afterTest
and browser.saveScreenshot
if !test.passed
in wdio.conf.js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With