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([
page.waitForResponse(
(resp) => resp.url().includes("/store/carts") && resp.status() === 200
),
page.getByTestId("item-amount-input").nth(itemCounter).fill(amount),
])
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)
expect(
await page.getByText("2.553,46 €", { exact: true }).first()
).toBeVisible()
await changeProductAmount(page, "5", 0)
await changeProductAmount(page, "5", 1)
await changeProductAmount(page, "5", 2)
expect(
await page.getByText("12.767,30 €", { exact: true }).first()
).toBeVisible()
})
})
})
```
I would expect the...
```
expect(
await page.getByText("12.767,30 €", { exact: true }).first()
).toBeVisible()
```
...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?"
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 [email protected].