Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Check if element is stable #3192

Closed
DJ-Glock opened this issue Jul 28, 2020 · 7 comments
Closed

[Question] Check if element is stable #3192

DJ-Glock opened this issue Jul 28, 2020 · 7 comments
Assignees

Comments

@DJ-Glock
Copy link

Hello.
I have a set of screenshot tests. Each test finds an element, creates a screenshot and compares screenshot with master screenshot of the same element. But from time to time these may fail because screenshot is being taken before animation completed. One of possible solutions is to wait for 1-2 seconds, but I don't like this way.
Looking at https://playwright.dev/#version=v1.2.1&path=docs%2Factionability.md&q=stable I can see that some actions like hover, click perform Stable check:

Element is considered stable when it has maintained the same bounding box for at least two consecutive animation frames.

I do not want to use page.hover() because it may cause another page action like tooltip appear or something like this.
So I tried to find native PW method like waitForSelector that performs Stable check, but was unable to find.
Is there any way to do it?

@dgozman
Copy link
Contributor

dgozman commented Jul 28, 2020

Thank you for the issue. I have a few questions 😄

  • Are you taking an element screenshot or a page screenshot?
  • If there was, say elementHandle.waitForStablePosition(), on which element would you call it? What if there are multiple moving elements?
  • Does this animation happen after some action (e.g. click)?

@DJ-Glock
Copy link
Author

@dgozman

Are you taking an element screenshot or a page screenshot?

I take separate element screenshot.

If there was, say elementHandle.waitForStablePosition(), on which element would you call it? What if there are multiple moving elements?

I would call it for the element that will be used for screenshot. Like this:

await element.waitForStablePosition()
await element.screenshot(...)

I believe you are thinking about child elements that could also move. If so this is not the perfect solution. But it will work for simple elements.

Does this animation happen after some action (e.g. click)?

It happens after page loading or after some action like click.

I've just found that I did not describe an example of my case.
So my problem is that from time to time screenshots resolution of the same element do not match in different test runs. They could differ for 1-2 pixels - for example 100x50, 100x48, 100x50, 100x48 for different test runs.
I added waitForTimeout(2000) before taking a screenshot and problem has gone.
Another possible solution is to set the slowMo parameter, but it will affect tests execution time. (I did not check this by many runs to be sure, but I used slowMo=100 with puppeteer before and had no problems).

Small remark: this is not environment issue, all screenshot tests are run in docker - so OS, browser, resolution and other conditions are always the same.

@dgozman
Copy link
Contributor

dgozman commented Jul 29, 2020

Thank you for the details!

In this particular case, I think it makes sense to automatically wait for the stable position before taking an element screenshot. IIUC, it should solve your issue without any changes.

As for the page screenshot, we need something more generic, but maybe waitForStablePosition could just work. I'll think about it.

@dgozman
Copy link
Contributor

dgozman commented Aug 17, 2020

With #3501, version 1.4 will provide the following api:

await handle.waitForElementState('stable');

@dgozman dgozman closed this as completed Aug 17, 2020
@yksvatla
Copy link

Does this method available in current playwright?

@dgozman
Copy link
Contributor

dgozman commented Aug 22, 2022

@yksvatla It is, since version 1.4.

@IPRIT
Copy link

IPRIT commented Jun 19, 2023

Why not available in waitFor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants