← Back to Discord Forum

Flaky tests due to network responses (?)

I have a test that changes the amount of 3 products in a cart (from 1 to 5) and checks the total, ... So far so good. However, it seems that my POST statements are not awaited correctly...

The code below works sometimes, sometimes it does not:

test.describe.configure({ mode: "serial" })

const changeProductAmount = async (
 page: Page,
 amount: string,
 itemCounter: number
) => {
 // Wait for the POST request that adds an item to the cart
 const [response] = await Promise.all([
     (resp) => resp.url().includes("/store/carts") && resp.status() === 200
 if (!response.ok()) {
   throw new Error(`Failed to update cart product amount`)

test.beforeEach(async ({ page }) => {
 await Signup(page)
 await SignIn(page)
 await AddToCart(page)

test("Change Cart Items", async ({ page }) => {
 await page.goto("/cart")
 await page.waitForURL("/cart")
 await test.step("Change Cart Quantities", async () => {
   // Initial Amount of 3 items
   await page.waitForTimeout(5000)

     await page.getByText("2.553,46 €", { exact: true }).first()

   await changeProductAmount(page, "5", 0)
   await changeProductAmount(page, "5", 1)
   await changeProductAmount(page, "5", 2)
     await page.getByText("12.767,30 €", { exact: true }).first()
I would expect the...
     await page.getByText("12.767,30 €", { exact: true }).first()
...to work just fine, since I am awaiting the update calls in my Promise in `changeProductAmount`. However I often get the error:

```Error: expect.toBeVisible: Browser has been closed
I can see in the UI though that the test went through. I think I am checking for the price too fast and miss an await

I am new to playwright so thanks for any help

This thread is trying to answer question "Why does the test fail when checking for the price too fast after updating the cart product amount, and how can this be fixed?"

1 reply

When I run this from the Playwright UI it passes, from VSCode it fails, from the CL it fails as well. My assumption is that the UI change in updating the total amount in the cart is not reflected fast enough after the last (third) changeProductAmount has finished. Does that make sense?

Related Discord Threads

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.