Skip to content

(webdriverio): add isStable, waitForStable commands#11772

Merged
erwinheitzman merged 15 commits intowebdriverio:mainfrom
erwinheitzman:add-stable-commands
Dec 8, 2023
Merged

(webdriverio): add isStable, waitForStable commands#11772
erwinheitzman merged 15 commits intowebdriverio:mainfrom
erwinheitzman:add-stable-commands

Conversation

@erwinheitzman
Copy link
Member

Proposed changes

Resolves #3106

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • I have added proper type definitions for new commands (if appropriate)

Reviewers: @webdriverio/project-committers

@erwinheitzman erwinheitzman marked this pull request as ready for review December 1, 2023 20:33
@erwinheitzman erwinheitzman added the PR: New Feature 🚀 PRs that contain new features label Dec 1, 2023
Copy link
Member

@BorisOsipov BorisOsipov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment.

@erwinheitzman
Copy link
Member Author

@BorisOsipov although you are right, I feel like we can already ship it as it is as it seems like a bit of an edgecase to use this on a inactive tab. What do you think?

@BorisOsipov
Copy link
Member

@erwinheitzman Sure we can. But! It seems easy to add code like this

if(document.visibilityState === 'hidden') {
	throw Error("Hey man, you are using isStable for inactive tab, don't do it dude!")
}

and forget the case forever. Instead of waiting to see when someone will come to Discord and ask about it in the next few years.

@erwinheitzman
Copy link
Member Author

I thought so but it's actually not that easy as the error doesn't seem to bubble up correctly.

Trying some workarounds for it.

@BorisOsipov
Copy link
Member

@erwinheitzman I have also played with this in another tool I am a committer of. My version was

browser.execute(script, arguments)

function fn (element) {
  if (document.visibilityState === 'hidden') {
    throw Error("Blablabla")
  }

  return new Promise((resolve) => {
    window.requestAnimationFrame(() => {
      const rect1 = element.getBoundingClientRect();
      window.requestAnimationFrame(() => {
        const rect2 = element.getBoundingClientRect();
        if (rect1.x === rect2.x &&
          rect1.y === rect2.y &&
          rect1.width === rect2.width &&
          rect1.height === rect2.height) {
          resolve(false);
        } else {
          resolve(true);
        }
      });
    });
  });
}

It also doesn't use modern JS features like for (const prop in previousPosition), and probably work in old browsers either.
You may find it useful.

@erwinheitzman
Copy link
Member Author

@erwinheitzman I have also played with this in another tool I am a committer of. My version was

browser.execute(script, arguments)

function fn (element) {
  if (document.visibilityState === 'hidden') {
    throw Error("Blablabla")
  }

  return new Promise((resolve) => {
    window.requestAnimationFrame(() => {
      const rect1 = element.getBoundingClientRect();
      window.requestAnimationFrame(() => {
        const rect2 = element.getBoundingClientRect();
        if (rect1.x === rect2.x &&
          rect1.y === rect2.y &&
          rect1.width === rect2.width &&
          rect1.height === rect2.height) {
          resolve(false);
        } else {
          resolve(true);
        }
      });
    });
  });
}

It also doesn't use modern JS features like for (const prop in previousPosition), and probably work in old browsers either. You may find it useful.

Thanks, I know I know but the problem is that it works but the the waitForStable command does not fail when the isStable command throws so I am looking into why it's not catching the error as I expect it would

@BorisOsipov
Copy link
Member

Ahh get it.

@BorisOsipov
Copy link
Member

Now I understand why you want to "ship it as is". It seems to make sense.

@erwinheitzman
Copy link
Member Author

@BorisOsipov needed to early return to immediately stop the command after the first attempt and managed to get it to work after some fiddling 👍

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me. Some suggestions on examples and code organisation. I kind of start thinking it would be nice to just have a waitForElementState rather than having so many waitFor commands.

@erwinheitzman
Copy link
Member Author

What about waitFor('displayed')?

@christian-bromann
Copy link
Member

What about waitFor('displayed')?

Let me raise a separate issue for this where we can discuss all the details.

@christian-bromann christian-bromann force-pushed the main branch 2 times, most recently from 19697d0 to c9a3908 Compare December 6, 2023 20:16
@erwinheitzman
Copy link
Member Author

@christian-bromann there seems to be a difference in CI and my machine in how the quotes are handled but the snapshot is identical to that of similar commands, do you see anything I missed/did wrong?

@erwinheitzman
Copy link
Member Author

Finally a green build lol

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great effort 👏

@erwinheitzman erwinheitzman merged commit 9580a44 into webdriverio:main Dec 8, 2023
@erwinheitzman erwinheitzman deleted the add-stable-commands branch December 8, 2023 17:18
@viktor-silakov
Copy link
Contributor

Do I understand correctly, if an element is stable for 5 frames in a row and then starts to change, this method (wait for stable) will not work?

@christian-bromann
Copy link
Member

@viktor-silakov detecting the "stability" of an element is almost impossible to get right all the times. This command is a first approach but there are plenty of use case where this might not work. If you have an example where this command fails, please provide a reproducible example and we are happy to take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: New Feature 🚀 PRs that contain new features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make click wait for animation to finish

4 participants