你的if语句永远不会被触发。
根据你的信息,我想说,你必须在afterEach方法中设置一个变量,表示如果你的一个测试失败。
然后,你必须在最终的方法中使用它来进行if陈述。
我使用cucumber和量角器,处理方式如下:
var failed = false;
After(function (scenario)
{
console.log("After " + scenario.pickle.name + " in " + scenario.sourceLocation.uri + ", result: " + scenario.result.status);
if (scenario.result.status === Status.FAILED)
{
//do stuff if one test failes
failed = true;
const attach = this.attach;
return browser.takeScreenshot().then(function(png)
{
return attach(new Buffer(png, "base64"), "image/png");
});
}
});
AfterAll(function(callback)
{
console.log("AfterAll");
if (failed)
{
var mailOptions = {
from: 'yourmail', // sender address (who sends)
//to: process.env.reportlist,
to: 'other mail',
subject: (process.env.COMPUTERNAME || 'testrunner') + ': WARNING! your automated test result has found error(s)', // Subject line
text: 'Your automated test has accured an error! Visit the report for more details:
/*attachments: [
{
filename: 'report.html',
path: htmlReport,
}]*/
};
transporter.sendMail(mailOptions, function(error, info)
{
if(error)
{
return console.log(error);
}
console.log('Email sent: ' + info.response);
console.log(info);
});
} else {
fs.writeFile(errorLog + "reportLog.txt", "SUCCESS");
let mailOptions = {
from: '"Mathur, Shruti" <xxx@xx.com>',
to: 'xxx@xx.com',
subject: 'Liability Management automation Report-All Test Suite Passed',
text: 'Test case completed',
html: 'Hi Team,<br><br> Test Automation for <b>Liability Management UI</b> through Protractor has been completed.All Test suites are <b>passed</b> successfully.<br>Please find the attached report for reference.',
attachments: [{
path: 'C:/Shruti/Protractor_Autodistribution/my-app/Test/report.zip'
}]
};
}