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 the test failed, I want to take a screenshot using the Mocha function:
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 the condition returned true. But this does not create a screenshot.
source share