Paul Phillips Paul Phillips - 2 months ago 22
Javascript Question

Can't Access Scenario Object in Hooks

Trying to screenshot when any of my steps fails by accessing the scenario object and seeing if .isSuccessful() is true or false. Doing this in my hooks.js file:

After(function (scenario) {
var status = scenario.isSuccessful();
if (!status)
driver.sleep(3000);
TakeScreenshot('./test_artifacts/img/failures/', scenario.getName(), driver);
//prints browser console logs
driver.manage().logs().get('browser').then(function (logs) {
fs.writeFile('./test_artifacts/logs/' + scenario.getName() + '.txt', stringify(logs), function (err) {
if (err)
return console.log(err);
});
})
return driver.quit();
});


However, I am currently getting the following error:

TypeError: Cannot read property 'isSuccessful' of undefined


Any idea what is going on? My step_definition files are working perfectly fine, as are my .feature files.

Edit: Here is my hooks.js file:

var selenium = require('selenium-webdriver');
var phantomjs = require('phantomjs');
var sleep = require('system-sleep');
var By = selenium.By;
var fs = require('fs');
var seleniumTimeOut = 20 * 1000;
var childProcess = require('child_process');
var stringify = require('node-stringify');
var Key = require('../../node_modules/selenium-webdriver/lib/input');
var { defineSupportCode } = require('cucumber');
require('chromedriver');

function TakeScreenshot(path, imgName, driver) {
driver.takeScreenshot().then(
function (image, err) {
fs.writeFileSync(path + imgName + (new Date).getTime().toString() + '.png', image, 'base64');
});
};

defineSupportCode(function ({ After, Before, setDefaultTimeout }) {

setDefaultTimeout(60 * 1000);

After(function (scenario) {
var status = scenario.isSuccessful();
if (!status)
driver.sleep(3000);
TakeScreenshot('./test_artifacts/img/failures/', scenario.getName(), driver);
//prints browser console logs
driver.manage().logs().get('browser').then(function (logs) {
fs.writeFile('./test_artifacts/logs/' + scenario.getName() + '.txt', stringify(logs), function (err) {
if (err)
return console.log(err);
});
})
return driver.quit();
});

Before(function () {

//set up driver instance
driver = {};
driver = new selenium.Builder()
.withCapabilities({ 'phantomjs.cli.args': ['--ignore-ssl-errors=true'] })
.forBrowser('phantomjs')
.build();
driver.manage().timeouts().implicitlyWait(10000);
driver.manage().window().setSize(1674, 1407);

//clear local storage before tests
var localStoragePath = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'] + '\\AppData\\Local\\Ofi Labs\\PhantomJS\\';
fs.readdir(localStoragePath, function (err, items) {

if (items) {
for (var i = 0; i < items.length; i++) {

var file = localStoragePath + '/' + items[i];

try {
fs.writeFileSync(file, '');
console.log('Truncated file: ' + file);
} catch (err) {
console.log('Failed to truncate file: ' + file);
}
}
}
});

//log in to site to retrieve access token before feature

driver.get("https://localhost:8000/portal/login");

sleep(4000);

driver.findElement(By.css('#app > div > div > div.col-xs-6.textColumn > button')).click();

sleep(4000);

TakeScreenshot('./test_artifacts/img/', 'Load Success', driver);

driver.findElement(By.css('#auth0-lock-container-1 > div > div.auth0-lock-center > form > div > div > div:nth-child(3) > span > div > div > div > div > div > div > div > div > div > div.auth0-lock-social-buttons-container > button > div.auth0-lock-social-button-text')).click();

TakeScreenshot('./test_artifacts/img/', 'Load Success', driver);

sleep(3000);

var input1 = driver.findElement(By.css('#userId'));
var input2 = driver.findElement(By.css('#password'));

input1.sendKeys('utenza_reseller');
input2.sendKeys('reseller2015');

TakeScreenshot('./test_artifacts/img/', 'Load Success', driver);

driver.findElement(By.css('#doLoginOAuth')).click();

TakeScreenshot('./test_artifacts/img/', 'Load Success', driver);

});

});

Answer Source

Thanks to user Chuan Qin on the cucumber-js Gitter for the following answer:

Reverted from V 3.0.0 to V 2.0.0 of Cucumber. isSuccessful still doesn't work but isFailed does work. I seem to have access to the scenario object now, while it was undefined in 3.0.0