Selenium Java Script Webdriver with Mocha - take a screenshot if the 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 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.

+5
source share
1 answer

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

+2
source

Source: https://habr.com/ru/post/1246022/


All Articles