【问题标题】:Cypress failed email notification赛普拉斯失败的电子邮件通知
【发布时间】:2019-02-19 04:47:16
【问题描述】:

我正在使用 cypress.io 作为自动化工具,当我的 cypress 测试失败时,我需要发送电子邮件通知。 有什么办法吗?

【问题讨论】:

  • 嗨!欢迎来到堆栈溢出。这个问题非常广泛; “我该怎么做”的问题不太可能得到回答。请提供有关您的设置的一些详细信息。如果您还没有,请解决问题!当您遇到困难时,请告诉我们您尝试了什么以及遇到了什么错误。
  • @JoshuaWade 我尝试了此链接中给出的以下内容github.com/bahmutov/cypress-failed-email#readme,但我没有找到将它放在 js file.const {formEmail} = require('cypress-failed-email' ) const filename = process.argv[2] //> failed-test.json 例如 const failed = require(filename) const email = formEmail(failed) //> email 是带有文本字段的对象 {subject, plainText}跨度>

标签: email cypress


【解决方案1】:

相反,您应该使用Module API。您可以从 javascript 文件中运行 cypress 并获取运行 cypress 的结果。

检查这里。 https://github.com/cypress-io/cypress/issues/7441#issuecomment-632535986

下面是一个示例 javascript。

// e2e-run-tests.js
const cypress = require('cypress')

cypress.run({ headless: true, browser: 'chrome' })
  .then(result => {
    const testResults = JSON.stringify(result, null, 2);
    // Module API uses "if (result.failures)", that didn't worked for me. That's why I check with "totalFailed > 0"
    if (result.totalFailed > 0) {
      console.log('Failure(s) in tests! Will send an e-mail')
      sendEmail(testResults);
    }
    process.exit(0);
  })
  .catch(err => {
    console.log('Cypress didn't worked due to some error')
    process.exit(0);
  });

另外,请注意在sendEmail 方法中,您应该在电子邮件发送后调用process.exit(0);

【讨论】:

    【解决方案2】:

    您始终可以通过命令行将其链接到另一个工具。基本上,您将退出代码 (-1) 传递给 if 语句,然后运行邮件命令

    这个 SO 中有一个例子:https://serverfault.com/questions/252448/bash-script-alert-on-error

    cypress run
    if [ $? -ne 0 ]
    then
        mailx -s "FAIL" your@email.com
    

    【讨论】:

    • 我是否需要在 package.json 文件管理器或我的 testspec.js 文件中使用此代码。
    • 我会制作一个 bash 文件并从你的 package.json 脚本运行它
    • 我尝试过并测试运行,但在控制台上我收到以下错误消息 > cypress.io@3.1.0 cypress:run F:\ > test.sh npm ERR!代码 ELIFECYCLE npm 错误! errno 2 npm 错误! cypress.io@3.1.0 cypress:run: test.sh npm ERR!退出状态 2 npm ERR! npm 错误!在 cypress.io@3.1.0 cypress:run 脚本中失败。 npm 错误!这可能不是 npm 的问题。上面可能还有额外的日志输出。
    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2021-12-12
    • 1970-01-01
    相关资源
    最近更新 更多