在 Cypress 中使用 mocha-allure-reporter 无法读取 属性 'Base' 的未定义错误

Cannot read property 'Base' of undefined error using mocha-allure-reporter in Cypress

我正在尝试使用 mocha-allure-reporter in Cypress。我安装了 mochamocha-allure-reporter 作为开发依赖项,并在 cypress.json.

中提到 mocha-allure-reporter 作为记者

我尝试了 example section of mocha allure page 中引用的以下代码:

require('mocha-allure-reporter');

describe("simple test demo", () => {
  const testStep = allure.createStep("initial", () => {
    console.log("First Test")
  });

  it("simple passed test", () => {
    testStep();
  });
}

但是,我收到以下错误:

Uncaught TypeError: Cannot read property 'Base' of undefined

...在第一行本身:

require('mocha-allure-reporter')

查看控制台后,我发现错误源自 Allure 报告程序中的 - var Base = require("mocha").reporters.Base 行:

var Base = require("mocha").reporters.Base;
var Allure = require("allure-js-commons");
...
...
global.allure = new Runtime(allureReporter);

/**
 * Initialize a new `Allure` test reporter.
 *
 * @param {Runner} runner
 * @param {Object} opts mocha options
 * @api public
 */
function AllureReporter(runner, opts) {
...
...

请注意,执行完成后,将在 allure-results 目录中创建以下输出 xml 文件。

<?xml version='1.0'?>
<ns2:test-suite xmlns:ns2='urn:model.allure.qatools.yandex.ru' start='1547481439243' stop='1547481439477'>
    <name></name>
    <title></title>
    <test-cases>
        <test-case start='1547481439282' status='broken' stop='1547481439460'>
            <name>An uncaught error was detected outside of a test</name>
            <title>An uncaught error was detected outside of a test</title>
            <labels/>
            <parameters/>
            <steps/>
            <attachments/>
            <failure>
                <message>Cannot read property 'Base' of undefined

                This error originated from your test code, not from Cypress.

                When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

                Cypress could not associate this error to any specific test.

                We dynamically generated a new test to display this failure.</message>
                <stack-trace>Uncaught TypeError: Cannot read property 'Base' of undefined

                This error originated from your test code, not from Cypress.

                When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

                Cypress could not associate this error to any specific test.

                We dynamically generated a new test to display this failure.
                    at Object.&lt;anonymous> (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:15125:38)
                    at Object.98.allure-js-commons (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:15201:4)
                    at o (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:265)
                    at http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:316
                    at Object.40.mocha-allure-reporter (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:7566:1)
                    at o (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:265)
                    at r (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:431)
                    at http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:460</stack-trace>
            </failure>
        </test-case>
    </test-cases>
</ns2:test-suite>

请指导我。谢谢!

我能够使用 mocha-allure-reporter 只需安装它(连同 mocha),

npm install mocha mocha-allure-reporter

并在 package.json 中设置脚本,遵循 npm 记者的 Cypress 指南 here

"scripts": {
  ...
  "cypress:run": "cypress run --reporter mocha-allure-reporter"

注意,我认为这些记者只能使用 Cypress 'run' 命令,而不是 Cypress 'open' 命令。

输出是一个名为 'allure-results' 的文件夹,其中包含一堆 xml 文件。我想这些可以使用 Allure 框架工具显示。

示例输出文件:

<?xml version='1.0'?>
<ns2:test-suite xmlns:ns2='urn:model.allure.qatools.yandex.ru' start='1547254197911' stop='1547254201289'>
    <name>Tasks Page</name>
    <title>Tasks Page</title>
    <test-cases>
        <test-case start='1547254199721' status='passed' stop='1547254199815'>
            <name>should have a title</name>
            <title>should have a title</title>
            <labels/>
            <parameters/>
            <steps/>
            <attachments/>
        </test-case>
    </test-cases>
</ns2:test-suite>

运行 cy.task()

中的诱惑代码

要运行 allure代码,需要通过cy.task访问nodejs上下文。

例如,

/cypress/plugins/index.js

require('mocha-allure-reporter');

module.exports = (on) => {
  on('task', {
    allureTestStep () {
      const testStep = allure.createStep("initial", () => {
        console.log("First Test")
      });
      testStep()

      return null
    }
  })
}

规格

describe("simple test demo", () => {

  it("simple passed test", () => {
    cy.task('allureTestStep')
  });
})

请注意,这会在启动 Cypress 的命令 window 中生成控制台日志,而不是浏览器控制台。

但是,您可以将值从任务传回测试,具体取决于您实际尝试执行的操作(有关详细信息,请参阅文档)。