japa / browser-client

Browser client plugin built on top of Playwright
MIT License
9 stars 1 forks source link

Question - Auto retrying assertions #1

Open jofrly opened 2 months ago

jofrly commented 2 months ago

Hi there,

I'm trying out adonisjs which uses japa. I wrote a test for deleting an element in a list view:

    await page.locator('button', { hasText: 'delete' }).nth(0).click()
    await page.assertElementsCount(page.locator('tbody tr'), 1)

It does not behave as I would expect though. I would expect the second line to retry until the expectation is met or a timeout is exceeded. However it does not attempt to retry and instead checks once immediately after the click and then fails:

  Assertion Error: expected locator('tbody tr') to have '1' elements

- Expected  - 1
+ Received  + 1

One thing that worked was adding the following line between await page.waitForLoadState('networkidle') (or waitForTimeout(100)). I don't think this should be necessary though.

The playwright assertions have built in auto retries: https://playwright.dev/docs/test-assertions#auto-retrying-assertions :await expect(locator).toHaveCount().

The only mentions of retries in the documentation regard retrying a whole test. How should I modify my code to achieve the desired behavior?

thetutlage commented 2 months ago

Yeah, I think it will be a nice addition to have auto retries for playwright assertions. Feel free to send a PR (if you think you can work on it), or else I will pick it up in some free time.

stale[bot] commented 2 weeks ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

jeppester commented 1 week ago

How would you feel about implementing the assertions with @playwright/test's expect?

It would make it rather easy to implement waiting behaviour to a many - if not all - of the assertions.

thetutlage commented 1 week ago

Yeah, this part has to be improved. Would you like to give a try and create a PR for the same?

jeppester commented 1 week ago

Yeah, this part has to be improved. Would you like to give a try and create a PR for the same?

If you don't mind that I use expect from @playwright/test to do the underlying implementation, then I'd gladly give it a shot.

thetutlage commented 1 week ago

Yeah I think we can use it. I don't see any downsides of using it

jeppester commented 1 week ago

Yeah I think we can use it. I don't see any downsides of using it

I made a draft PR for this. Let me know what you think.

solomonhawk commented 22 hours ago

👍 We ran into this recently as well. Having auto-retrying assertions (or access to @playwright/test's expect) would be super great.