var driver = require('./world.js').getDriver();
var path = require("path");
var fs = require("fs");
var mkdirp = require('mkdirp');
var sanitize = require("sanitize-filename");
var myHooks = function () {
this.Before(function (scenario) {
this.driver.manage().window().maxsize();
});
this.After(function (scenario) {
this.driver.takeScreenshot().then(function (data) {
var now = new Date().toLocaleString().replace(/T/, ' ').replace(/:/g,'_');
var nowtime = now.split(" ");
var screenshotPath = path.join(__dirname,'../../screenshots', 'normal',nowtime[0]);
mkdirp(screenshotPath, function (err) {
if (err) console.error(err);
});
var base64Data = data.replace(/^data:image\/png;base64,/, "");
fs.writeFile(path.join(screenshotPath, nowtime[1] + sanitize(scenario.getName() + ".png").replace(/ /g, "_")), base64Data, 'base64', function (err) {
if (err) console.log(err);
});
});
if (scenario.isFailed()) {
this.driver.takeScreenshot().then(function (data) {
var decodedImage = new Buffer(data, 'base64').toString('binary');
scenario.attach(decodedImage, 'image/png');
});
}
return this.driver.manage().deleteAllCookies();
});
this.registerHandler('AfterFeatures', function (event) {
return driver.quit();
});
};
module.exports = myHooks;