Node.js cucumberjs:查找步骤结果是否失败
任务:Node.js cucumberjs:查找步骤结果是否失败,node.js,selenium,selenium-webdriver,mocha.js,cucumberjs,Node.js,Selenium,Selenium Webdriver,Mocha.js,Cucumberjs,任务: 使用selenium、webdriver和cucumberjs执行自动验收测试 找到一个失败的验收测试场景并截图 将其保存为go管道中的人工制品 这是我今天的任务。我已经通过编写场景测试、selenium设置、go管道和所有其他需要的东西完成了所有这些 唯一的问题是-我无法找到一种方法来找出一个失败的场景步骤,并对页面进行屏幕截图 问题详细信息: 我在步骤定义中放置了以下代码,这些代码针对每个场景步骤运行 //file: features/setpdefinitions/common
- 使用selenium、webdriver和cucumberjs执行自动验收测试李>
- 找到一个失败的验收测试场景并截图
- 将其保存为go管道中的人工制品
//file: features/setpdefinitions/common.step.js
var commonWrapper = function commonWrapper() {
this.World = require('../support/world').World;
this.Before(function (next) {
this.initBrowser(next);
});
this.After(function (next) {
this.browser.quit(next);
});
this.StepResult(function (event, callback) {
var stepResult = event.getPayloadItem('stepResult');
console.log(stepResult.isFailed());
callback();
});
};
module.exports = commonWrapper;
World
包含浏览器启动方法
这是我正在测试的一个示例功能场景
Feature: Forgot Password
As a user of Booking My account
I want to reset my password
So that I can login to my account when I forget my password
Scenario: On unsuccessful entering invalid email id
Given I am on forgot password page
When I enter invalid email "invalidemail-someDomain.com"
And click submit button
Then I should see validation message "Please enter a valid email."
问题在于上下文数据。不知何故,我无法将场景
作为第一个参数传递给after/before方法。我尝试了cucumberjs源代码中给出的代码,但没有成功。因此,我开始添加stepResult
方法,该方法在每次完成一个步骤时都会运行。一种相对类似的方法
根据文档,isFailed()
方法根据步骤结果返回一个布尔值。但是,无论步骤失败或通过,我总是得到一个false
。我尝试了它的另一个自我issusccessful()
,不管发生什么,它都会返回一个true
所以
我对TDD比较陌生,但到目前为止,这是一次很棒的体验。你需要的是一个后钩子 创建文件features/support/after_hooks.js
module.exports = function() {
this.After(function (scenario, callback) {
if (scenario.isFailed()) {
// Do your after stuff here
}
callback();
});
};
请注意,这仅在每个功能后执行,您可以在(cucumberjs 1.x)中获得更多详细信息
创建afterhook.js并检查失败场景 您可以使用browser.saveScreenshot保存屏幕截图。 保存屏幕截图时,使用文件名的时间戳,并从版本控制中排除屏幕截图
module.exports = function() {
this.After((scenario, callback) => {
if (scenario.isFailed()) {
const d = new Date();
browser.saveScreenshot(
`./e2e/screenshots/screenshot-${d.toISOString()}.png`
);
}
callback();
});
};
对我来说也很好!
module.exports = function() {
this.After((scenario, callback) => {
if (scenario.isFailed()) {
const d = new Date();
browser.saveScreenshot(
`./e2e/screenshots/screenshot-${d.toISOString()}.png`
);
}
callback();
});
};