【问题标题】:Does/Should playwright assertions return a booleanDoes/Should playwright assertions return a boolean
【发布时间】:2022-12-01 20:40:05
【问题描述】:

This is more of a general question to raise my understanding. Does a playwright assertion, like expect, return a boolean? When I debug it, like

let bar = await expect(true).toBeTruthy();

bar ends up being undefined. Can someone explain why this is so?

【问题讨论】:

  • Typically assertions (in most frameworks/languages) either throw an error (fail) or return nothing (pass). Evidently itdoesn'treturn a boolean, because you've checked that; what makes you think itshould?

标签: playwright assertion


【解决方案1】:

In general, a playwright assertion like expect does not return a boolean value. Instead, it is used to check whether a given value matches some expected condition, and if not, it will throw an error. In the example you provided, expect(true).toBeTruthy() will simply check whether the value true is truthy (i.e., evaluates to true when converted to a boolean) and will throw an error if it is not.

As for why the variable bar is undefined, it is because you are not assigning it any value. In the line let bar = await expect(true).toBeTruthy();, the await keyword is used to wait for the expect statement to complete before continuing with the rest of the code. Since expect does not return a value, bar will remain undefined.

If you want to store the result of an expect statement in a variable, you can use a try/catch block to catch any errors that are thrown by the assertion. For example:

let bar;

try {
  await expect(true).toBeTruthy();
  bar = true;
} catch (err) {
  bar = false;
}

In this code, if the expect statement passes (i.e., the value true is truthy), then bar will be assigned the value true. If the expect statement fails (i.e., the value true is not truthy), then bar will be assigned the value false

【讨论】:

    猜你喜欢
    • 2019-05-01
    • 1970-01-01
    • 2019-09-30
    • 2022-12-01
    • 1970-01-01
    • 2016-03-02
    • 2018-09-19
    • 2016-05-02
    • 1970-01-01
    相关资源
    最近更新 更多