-
Notifications
You must be signed in to change notification settings - Fork 9k
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
feat(elementhandle): introduce elementHandle.isIntersectingViewport() method. #2673
feat(elementhandle): introduce elementHandle.isIntersectingViewport() method. #2673
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here (e.g. What to do if you already signed the CLAIndividual signers
Corporate signers
|
I signed it! |
CLAs look good, thanks! |
Can I rerun |
@aslushnikov please check |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks promising
As part of this PR, let's change waitForSelector
implementation to use intersection observer; this way we won't have inconsistencies like this:
const e = await page.waitForSelector('div', {visible: true});
await e.isVisible(); // false
docs/api.md
Outdated
@@ -219,6 +219,7 @@ | |||
* [elementHandle.getProperties()](#elementhandlegetproperties) | |||
* [elementHandle.getProperty(propertyName)](#elementhandlegetpropertypropertyname) | |||
* [elementHandle.hover()](#elementhandlehover) | |||
* [elementHandle.isDisplayed()](#elementhandleisdisplayed) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elementHandle.isVisible()
is better since it aligns with waitForSelector
's "visible" option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will rename to isVisible
lib/ElementHandle.js
Outdated
resolve(entries[0].isIntersecting); | ||
observer.unobserve(node); | ||
}; | ||
const observer = new IntersectionObserver(callback, { root: null, rootMargin: '0px', threshold: 1.0 }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so when will the callback
be called? Is it always called initially?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it will be called initially and then unobserve from the node
test/elementhandle.spec.js
Outdated
const box = await page.$('.box'); | ||
|
||
expect(await box.isDisplayed()).toBe(false); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we also add a test that checks for the truthy value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'll add this test case soon
test/assets/hidden-and-visible.html
Outdated
@@ -0,0 +1,23 @@ | |||
<html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's use page.setContent
instead of a separate asset.
If we change waitForSelector to use intersection observer, won’t that cause elements below the fold to be considered invisible? That’s a pretty major change, and I’m not sure if it makes that feature more or less useful. “Visible” vs “would visible if you scroll down” is hard to define. And it conflicts with the original use case for this feature, which was to report elements ina carousel as invisible even though they could be scrolled to horizontally. |
@JoelEinbinder These will be considered invisible. This is a very good point though, we might need to postpone this till 2.0.
Usefulness is debatable, but API sanity will improve:
|
Add API to define is element visible in viewport via Intersection Observer References #2629
@aslushnikov change implementation of upd |
@aslushnikov please check PR |
@b-ponomarenko We discussed this PR with @JoelEinbinder one more time. Summary:
So the way forward with this:
As a part of 2.0 effort, we might want to do a better job separating "interactivity" vs "visibility", e.g. renaming |
…ElementHandle.isVisible to ElementHandle.isVisibleInViewport
@aslushnikov I canceled using Intersection Observer in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed this with @JoelEinbinder and renamed into elementHandle.isIntersectingViewport()
. Thanks for the PR!
I wouldn't be so sure that inputElement.isIntersectingViewport() is true only when an element is visible for human eyes, I have a case where element didn't yet appear as visible for eyes and |
@justnpT interesting! Can you please file a new issue with a repro for your usecase? |
Add API to define is element visible in viewport via Intersection Observer
References #2629