【问题标题】:Capture pass/fail of Protractor test case for custom reporting捕获量角器测试用例的通过/失败以进行自定义报告
【发布时间】:2019-04-17 20:04:40
【问题描述】:

我最近在我们公司为我们的 Angular 应用程序设置了 Protractor 测试 - 并且正在寻找一种简单的方法来捕获规范类中每个场景的通过/失败状态。有没有一种简单的方法可以做到这一点?我试过弄乱jasmine-spec-reporter,但也许我在那里遗漏了一些东西来提取我需要的数据。任何帮助将不胜感激。

我尝试过这样的事情:

let currentSpec = jasmine.getEnv().currrentSpec, passed = currentSpec.results().passed();

但我总是遇到类似的问题

当前规格未指定

如果可能的话,理想情况下,我希望在没有 jasmine 报告的情况下捕获通过或失败。

【问题讨论】:

    标签: javascript angularjs typescript protractor automated-tests


    【解决方案1】:

    你也在用这个吗?: https://www.npmjs.com/package/protractor-html-reporter-2

    为了更好地描述错误并将它们添加到 jasmine 报告器中: https://www.npmjs.com/package/jasmine2-custom-message

    【讨论】:

    • 我已经调查过了,但无法做我想做的事。基本上我希望每个“it”块都有一个 AfterEach,就像 if (result.status == 'failed') { // 将结果发送到我们的 api 报告的代码 }
    【解决方案2】:

    您正在寻找的实际上是specDone 而不是afterEach。您要么需要修改当前正在使用的报告器的specDone 函数,要么构建适合您需要的自定义报告器。

    https://jasmine.github.io/2.1/custom_reporter.html#section-specDone

    创建您的自定义报告器:

    // myReporter.js
    module.exports = {
      specDone: (result) => {
        // do stuff...
      }
    }
    

    然后在你的量角器配置中你会得到这样的东西:

    const myReporter = require('myReporter');
    
    // other config properties
    
    onPrepare: function() {
      jasmine.getEnv().addReporter(myReporter);        
    }
    

    【讨论】:

    • 看起来 specDone 适用于整个规范文件。我希望它根据每个“it”块进行更新,但看起来我仍然需要一些报告依赖性。
    • 它应该为每个“it”块运行。文档第一行:specDone is invoked when an it and its associated beforeEach and afterEach functions have been run.
    • 好吧,这是有道理的.. 仍然不确定在哪里指定 specDone 部分。在 spec 文件还是 conf 文件中?
    • 这就是我现在让 myReporter = { specDone: function (result) { if (result.failedExpectations.length > 0) { TEST_STATUS = "FAIL"; } else { TEST_STATUS = "PASS"; } } }; afterEach(function (myReporter) { home.reportResults(PROJECT_KEY, TEST_CASE_KEY, TEST_CYCLE_KEY, TEST_STATUS, startTime); });`
    • 这真的有效吗?您可能应该创建一个单独的myReporter.js 文件,然后在量角器配置的onPrepare 中设置它,类似于其他报告器的配置方式。我会用一个例子来更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 2018-08-06
    相关资源
    最近更新 更多