cucumberjs:发现一个步骤结果是失败的

任务

  • 使用selenium,webdriver,cucumberjs执行自动验收testing。
  • find一个失败的验收testing场景并截取该页面
  • 除此之外,作为一个人工制品在去pipe道。

那是我今天的任务。 我已经完成了所有这些工作,包括编写场景testing,selenium安装,pipe道和其他一切需要的东西。

唯一的问题是 – 我无法弄清楚如何find一个失败的scheme步骤,并采取页面的屏幕截图。

问题详细信息 :我将以下代码放置在我的步骤定义中,这是针对每个scheme步骤运行的

//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包含浏览器启动方法。

这是我正在testing的示例function场景

 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方法的scenario 。 我尝试了cucumberjs源代码,但不能成功。 所以,我开始添加stepResult方法,每次完成一个步骤就会运行。 一个比较类似的方法。

根据文档, isFailed()方法基于步骤结果返回布尔值。 但是,无论失败还是通过,我都会得到一个false 。 我尝试了alter-ego isSuccessful() ,无论如何返回true

所以,

  1. 我可能在这里做错了什么?
  2. 我怎样才能将场景传递给after()方法?

我对TDD比较陌生,但是迄今为止它是一个非常棒的体验。

你需要的是一个后钩

创build一个文件features / support / after_hooks.js

 module.exports = function() { this.After(function (scenario, callback) { if (scenario.isFailed()) { // Do your after stuff here } callback(); }); }; 

请注意,这仅在每个function之后执行

您可以在https://github.com/cucumber/cucumber-js/blob/master/lib/cucumber/api/scenario.js中获得更多详细信息

getKeyword: function getKeyword() { return astScenario.getKeyword(); }, getName: function getName() { return astScenario.getName(); }, getDescription: function getDescription() { return astScenario.getDescription(); }, getUri: function getUri() { return astScenario.getUri(); }, getLine: function getLine() { return astScenario.getLine(); }, getTags: function getTags() { return astScenario.getTags(); }, isSuccessful: function isSuccessful() { return scenarioResult.getStatus() === Cucumber.Status.PASSED; }, isFailed: function isFailed() { return scenarioResult.getStatus() === Cucumber.Status.FAILED; }, isPending: function isPending() { return scenarioResult.getStatus() === Cucumber.Status.PENDING; }, isUndefined: function isUndefined() { return scenarioResult.getStatus() === Cucumber.Status.UNDEFINED; }, isSkipped: function isSkipped() { return scenarioResult.getStatus() === Cucumber.Status.SKIPPED; }, getException: function getException() { return scenarioResult.getFailureException(); }, getAttachments: function getAttachments() { return attachments; }, clearAttachments: function clearAttachments() { attachments = []; },