Playwright unfortunately doesn't have a method like isInterSectingViewport in Puppeteer yet.(likethis)
So authors of Playwright help me in Slack community(you can find it on official site).
const result = await page.$eval(selector, async element => {
const visibleRatio: number = await new Promise(resolve => {
const observer = new IntersectionObserver(entries => {
resolve(entries[0].intersectionRatio);
observer.disconnect();
});
observer.observe(element);
// Firefox doesn't call IntersectionObserver callback unless
// there are rafs.
requestAnimationFrame(() => {});
});
return visibleRatio > 0;
});
The case in which I used this method:
I want know that after I’m click some element - I have scrolling to another element. Unfortunately boundingBox method doesn’t help in my case.
also You can add this functionality to my BasePage class
/**
* @returns {!Promise<boolean>}
*/
isIntersectingViewport(selector: string): Promise<boolean> {
return this.page.$eval(selector, async element => {
const visibleRatio: number = await new Promise(resolve => {
const observer = new IntersectionObserver(entries => {
resolve(entries[0].intersectionRatio);
observer.disconnect();
});
observer.observe(element);
// Firefox doesn't call IntersectionObserver callback unless
// there are rafs.
requestAnimationFrame(() => {});
});
return visibleRatio > 0;
});
}
P.S.
In fact, all the code except for one line is taken from the realisation of method isInterSectingViewport in GitHub Puppeteer
{"_guid": "handle@73d2b54286357906ed761446c1a5c78a", "_type": "JSHandle"}
. probably im not using it correctly. any tips? – Carcassonne