Rayrun
← Back to Discord Forum

How to do assertions in the page context when using Playwright library?

recalcitrantmonkposted in #help-playwright
Open in Discord
recalcitrantmonk
recalcitrantmonk

I'm using the Playwright library API (not the test runner) to run tests dynamically from an external source. Unfortunately, the library doesn't support web assertions, so I'm here asking - can I do assertions at all?

const result = await page.route("**/*", async (route) => {
    const request = route.request();
    if (request.url().includes(".m3u8") || request.url().includes(".ts")) {
      await route.continue();
      const response = await request.response();
      if (response?.status() !== 200) {
        // how do I error here?
      }
    }
  });

In this case, I could throw an error, but it throws in the context of the page, not my script. What are my options here for getting a web-assertion esque equivalent?

This thread is trying to answer question "How can I perform assertions in the page context when using the Playwright library API?"

11 replies

You can use default expect assertions

recalcitrantmonk
recalcitrantmonk

@skorp32 - I'm trying to do the assertions within things like page.evaluate, which doesn't seem to be working.

recalcitrantmonk
recalcitrantmonk

because I'm using the library instead of the runner

refactoreric

Why do you want to do the assertions in page.evaluate?

page.route handlers are still just running in the context of NodeJS, not in the browser. Not sure what happens though, when throwing exceptions in such handlers.

recalcitrantmonk
recalcitrantmonk

I’m running my tests dynamically via HTTP call. I’ve since refactored to just fork the runner as a child process and continue based on the process output

Hey @recalcitrantmonk you could try simply: expect(response.ok()).toBeTruthy(); or expect(response.status()).toBe(200);

And regarding your comment:

how do I error here there is always option to just simply throw an error if you're not liking expect assertions for whatever reason

recalcitrantmonk
recalcitrantmonk

From my understand, throwing an error inside the context of the browser doesn’t bubble up into node land when using the library as web assertions are not supported

recalcitrantmonk
recalcitrantmonk

Inside the page evaluation methods themselves*

recalcitrantmonk
recalcitrantmonk

I was actually trying to understand how that worked last night and why web assertions aren’t supported in the library, but I didn’t grok it

Not sure why you stick with page.evaluate, doesn't make sense. @skorp32 already mentioned that and @refactoreric as well

You typically would want to either throw error from your test code or just assert if the API's response is 200 OK or within success HTTP codes and you're done, just don't use for that page.evaluate() and you'll be fine 😅

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.