如何在 Test Cafe 错误中获取完整的堆栈跟踪

use*_*631 5 automated-tests exception stack-trace e2e-testing testcafe

我一直在使用 Test Cafe 编写一个内部测试框架,其中操作 (t.click) 和断言 (t.expect) 不是直接写在规范中,而是在其他文件中定义和聚合。

一切都很酷,直到测试不失败:在这种情况下,Test Cafe 记者在控制台中写入了断言/操作失败和相关代码片段,但我没有找到理解测试中函数调用的完整堆栈跟踪的方法归结为失败的断言。

我怎样才能确保在报告器中提供完整的堆栈跟踪,记录所有导致我的测试失败的函数调用的堆栈跟踪?

我知道原因应该与async/await如何转换为生成器有关:错误的堆栈跟踪仅显示最后执行的 await 而不是所有先前的调用。

<section> ... </section>
<section class="section--modifier">
  <h1> ... </h1>
  <div>
    ...
    <button class="section__button">
      <div class="button__label">
        <span class="label__text">Hello!</span> <-- Target of my test
      </div>
    </button>
    ...
  </div>
</section>
<section> ... </section>
Run Code Online (Sandbox Code Playgroud)
// 
// My spec file
//
import { Selector } from 'testcafe';

import {
  verifyButtonColor
} from './button';

fixture`My Fixture`
  .page`....`;

test('Test my section', async (t) => {
  const MySection = Selector('.section--modifier');
  const MyButton1 = MySection.find('.section__button');

  const MySection2 = Selector('.section--modifier2');
  const MyButton2 = MySection2.find('.section__button');

  ....
  await verifyButtonColor(t, MyButton1, 'green'); // it will fail!
  ....
  ....
  ....
  await verifyButtonColor(t, MyButton2, 'green');
});
Run Code Online (Sandbox Code Playgroud)
//
// Definition of assertion verifyButtonColor (button.js)
//
import { Selector } from 'testcafe';

import {
  verifyLabelColor
} from './label';

export async function verifyButtonColor(t, node, expectedColor) {
   const MyLabel = node.find('.button__label');
   await verifyLabelColor(t, MyLabel, expectedColor);
}
Run Code Online (Sandbox Code Playgroud)
// 
// Definition of assertion verifyLabelColor (label.js)
//
export async function verifyLabelColor(t, node, expectedColor) {
   const MyText= node.find('.label__text');
   const color = await MyText.getStyleProperty('color');

   await t.expect(color).eql(expectedColor, `Color should be ${expectedColor}, found ${color}`); // <-- it will FAIL!
}
Run Code Online (Sandbox Code Playgroud)

我在记者中没有得到的是我的测试失败了,因为“verifyLabelColor”中定义的断言失败(颜色不是绿色:(),

...
await t.expect(color).eql(expectedColor, `Color should be ${expectedColor}, found ${color}`);
...
Run Code Online (Sandbox Code Playgroud)

但在记者中,我没有证据表明由于以下调用堆栈而失败

- await verifyButtonColor(t, MyButton1, 'green');
- await verifyLabelColor(t, MyLabel, expectedColor);
- await t.expect(color).eql(expectedColor, `Color should be ${expectedColor}, found ${color}`);
Run Code Online (Sandbox Code Playgroud)

任何机构都面临类似的问题?

另一种方法是记录导致失败的选择器的“路径”,但查看 Test Cafe 文档我没有发现这样做的可能性:知道以下路径的元素上的断言失败至少可以帮助明白出了什么问题

.section--modifier .section__button .button__label .label__text
Run Code Online (Sandbox Code Playgroud)

hdo*_*val 1

该主题与 TestCafe 提案相关:拥有多个堆栈跟踪报告器,以便在测试失败时进行快速分析

同时,您可以尝试使用此记者:/testcafe-reporter-cucumber-json或者您可以开发自己的记者