如何在量角器(打字稿)中制作更具描述性的错误日志消息

How to make more descriptive error log messages in Protractor (typescript)

我们是 运行 针对生产网站的量角器(用打字稿编写)。有谁知道量角器测试失败时如何输出一些日志消息?我现在基本上只有一条 YES/NO 消息,像这样:

10) Query page accuracy test. Comparison of page data to data extracted from excel. Test begins...
  Message:
    Expected false to be truthy.
  Stack:
    Error: Failed expectation
        at C:\xampp\htdocs\kap\frontend\src\app\qa\qa.js:147:103
        at step (C:\xampp\htdocs\kap\frontend\src\app\qa\qa.js:33:23)
        at Object.next (C:\...

等 这是我们测试的行:

expect(htmlValue == excelValue || (htmlValue === "0" && excelValue == "NaN")).toBeTruthy();

如何在测试失败时添加更具描述性的消息?谢谢任何人,我是 Protractor 的新手,但我之前做过 QA 自动化测试。

您几乎可以在所有茉莉花匹配器中添加自定义消息,例如

expect(true).toBe(true, "This will be shown if the expectation will fail")

expect(htmlValue == excelValue || (htmlValue === "0" && excelValue == "NaN")).toBeTruthy(`Expected ${excelValue}` but got ${htmlValue });

您可以在 ToBeTruthy 中添加这样的描述:

expect(htmlValue == excelValue || (htmlValue === "0" && excelValue == "NaN")).toBeTruthy("description why I checked this")

如果您愿意,可以像这样显示失败的测试值:

expect(
  htmlValue == excelValue || (htmlValue === '0' && excelValue == 'NaN'),
).toBeTruthy(
  `HTML differ form Excel. HTML: ${htmlValue}; Excel: ${excelValue}`,
);

几乎所有茉莉花 Matchers 都接受 expectationFailOuput 参数:

toBe(expected: any, expectationFailOutput?: any): Promise<void>;
toEqual(expected: any, expectationFailOutput?: any): Promise<void>;
toMatch(expected: string | RegExp | Promise<string | RegExp>, expectationFailOutput?: any): Promise<void>;
toBeDefined(expectationFailOutput?: any): Promise<void>;
toBeUndefined(expectationFailOutput?: any): Promise<void>;
toBeNull(expectationFailOutput?: any): Promise<void>;
toBeTruthy(expectationFailOutput?: any): Promise<void>;
toBeFalsy(expectationFailOutput?: any): Promise<void>;
toContain(expected: any, expectationFailOutput?: any): Promise<void>;
toBeLessThan(expected: number | Promise<number>, expectationFailOutput?: any): Promise<void>;
toBeLessThanOrEqual(expected: number | Promise<number>, expectationFailOutput?: any): Promise<void>;
toBeGreaterThan(expected: number | Promise<number>, expectationFailOutput?: any): Promise<void>;
toBeGreaterThanOrEqual(expected: number | Promise<number>, expectationFailOutput?: any): Promise<void>;
toBeCloseTo(expected: number | Promise<number>, precision?: any, expectationFailOutput?: any): Promise<void>;

您将在 jasminewd2 depot

中找到 Matchers 定义

虽然建议的解决方案可能有效,但使用它们并不是一个好的做法,仅仅是因为它需要更多的输入和不可读的代码

默认框架 Jasmine 正式支持的两个选项是:

  1. 对于量角器 6+,您可以使用内置方法 .withContext() 不幸的是,Jasmine 团队没有提供太多文档。但一般语法是 expect(true).withContext("your description").toBe(false); 并且可以用于任何 Jasmine 断言,而不仅仅是 .toBeTruthy()

  2. 对于 Protractor 版本 < 6,当每个断言都有两个参数时存在未记录的功能 - 第一个是预期值,第二个 - 失败消息。例如

expect("first value").toBe("second value", "verification of some values")

导致失败并显示以下消息

- Expected 'first value' to be 'second value', 'verification of some values'.