如何在Playwright中进行错误断言?

huangapple go评论69阅读模式
英文:

How to assert for errors in Playwright?

问题

I'm going nuts trying to assert for a known TimeoutError. I'm currently testing for the Hidden Layers scenario from UI Testing Playground in Playwright Node.js and I would like to know if it's possible to have the TimeoutError not to fail the test, as this is the expectation. I'm pretty new in automating with both Playwright and Typescript.

我正在疯狂地尝试断言一个已知的TimeoutError。我目前正在使用Playwright Node.js测试UI测试播放场中的隐藏层场景,我想知道是否可能使TimeoutError不导致测试失败,因为这是期望的结果。我在使用Playwright和Typescript自动化方面还相对新手。

I have tried multiple methods even making the clickGreenButton() method throw the error, but it seems the expect() function does not catch it at all.

我尝试了多种方法,甚至使clickGreenButton()方法抛出错误,但似乎**expect()**函数根本没有捕获它。

Method inside the HiddenLayersPage.ts:

HiddenLayersPage.ts文件中的方法:

    async clickGreenButton() {
        await this.greenButton.click()
    }

Code inside the spec file which is supposed to check that the second click would not be successful as the element to be clicked becomes hidden:

规范文件中的代码,该代码应该检查第二次点击不会成功,因为要点击的元素变为隐藏状态:

await hiddenLayersPage.clickGreenButton();
expect(async () => await hiddenLayersPage.clickGreenButton()).toThrow();
英文:

I'm going nuts trying to assert for a known TimeoutError. I'm currently testing for the Hidden Layers scenario from UI Testing Playground in Playwright Node.js and I would like to know if it's possible to have the TimeoutError not to fail the test, as this is the expectation. I'm pretty new in automating with both Playwright and Typescript.

I have tried multiple methods even making the clickGreenButton() method throw the error, but it seems the expect() function does not catch it at all.

Method inside the HiddenLayersPage.ts:

    async clickGreenButton() {
        await this.greenButton.click()
    }

Code inside the spec file which is supposed to check that the second click would not be successful as the element to be clicked becomes hidden:

await hiddenLayersPage.clickGreenButton();
expect(async () => await hiddenLayersPage.clickGreenButton()).toThrow();

答案1

得分: 0

这是因为expect正在运行您的函数,但由于它是async,它返回了一个承诺(虽然被拒绝),而不是一个错误/异常。

所以语法略有不同。您只需将承诺传递给expect,告诉它使用.rejects来解析承诺,然后调用toThrow,如下所示:

await expect(hiddenLayersPage.clickGreenButton()).rejects.toThrow();

请注意await的用法/位置,因为您需要等待异步匹配器,但不需要等待函数,因为您希望等待承诺。

英文:

This is happening because expect is running your function, but being async it’s getting a promise (albeit rejected) back, rather than an error/exception.

So the syntax is a little different. You just need to pass expect the resulting promise, tell it to unwrap the promise with .rejects, and then call toThrow, like so:

await expect(hiddenLayersPage.clickGreenButton()).rejects.toThrow();

Notice the usage/placement of await, since you need to await the async matcher, but not await the function since you want the promise.

huangapple
  • 本文由 发表于 2023年2月14日 03:45:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定