From 254bc80811774007125c48ad012cc8cd90a5efec Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Wed, 11 Jul 2018 21:04:36 -0700 Subject: [PATCH] chore: cleanup ElementHandle.isIntersectingViewport (#2874) This patch: - simplifies test reusing the `offscreenbuttons.html` asset - aligns IntersectionObserver usage with the one we have for `ElementHandle._scrollIntoViewIfNeeded`. --- lib/ElementHandle.js | 22 ++++++------- test/assets/offscreenbuttons.html | 2 ++ test/elementhandle.spec.js | 55 +++++-------------------------- test/input.spec.js | 5 +-- 4 files changed, 24 insertions(+), 60 deletions(-) diff --git a/lib/ElementHandle.js b/lib/ElementHandle.js index b601cb3dd1c..ab9de81a32e 100644 --- a/lib/ElementHandle.js +++ b/lib/ElementHandle.js @@ -359,18 +359,16 @@ class ElementHandle extends JSHandle { * @returns {!Promise} */ isIntersectingViewport() { - return this.executionContext().evaluate( - node => new Promise(resolve => { - const callback = entries => { - resolve(entries[0].isIntersecting); - observer.disconnect(); - }; - const observer = new IntersectionObserver(callback); - - observer.observe(node); - }), - this - ); + return this.executionContext().evaluate(async element => { + const visibleRatio = await new Promise(resolve => { + const observer = new IntersectionObserver(entries => { + resolve(entries[0].intersectionRatio); + observer.disconnect(); + }); + observer.observe(element); + }); + return visibleRatio > 0; + }, this); } } diff --git a/test/assets/offscreenbuttons.html b/test/assets/offscreenbuttons.html index 27192336ea9..d45e2a41298 100644 --- a/test/assets/offscreenbuttons.html +++ b/test/assets/offscreenbuttons.html @@ -15,6 +15,7 @@ #btn7 { right: -70px; top: 175px; } #btn8 { right: -80px; top: 200px; } #btn9 { right: -90px; top: 225px; } + #btn10 { right: -100px; top: 250px; } @@ -26,6 +27,7 @@ +