From a24cec20f91101541c758c7390b5bb82e8f0e23a Mon Sep 17 00:00:00 2001 From: JoelEinbinder Date: Mon, 24 Jul 2017 16:14:32 -0700 Subject: [PATCH] Fix clicking offscreen horizontally (#105) This patch starts clicking in the center of the element area which is visible in a viewport. Fixes #103. --- lib/FrameManager.js | 4 ++-- test/test.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/FrameManager.js b/lib/FrameManager.js index 0170c857ca2..872b7c817f8 100644 --- a/lib/FrameManager.js +++ b/lib/FrameManager.js @@ -332,8 +332,8 @@ class Frame { element.scrollIntoViewIfNeeded(); let rect = element.getBoundingClientRect(); return { - x: (rect.left + rect.right) / 2, - y: (rect.top + rect.bottom) / 2 + x: (Math.max(rect.left, 0) + Math.min(rect.right, window.innerWidth)) / 2, + y: (Math.max(rect.top, 0) + Math.min(rect.bottom, window.innerHeight)) / 2 }; }, selector); if (!center) diff --git a/test/test.js b/test/test.js index 52ab9ffb40d..469c74aa46e 100644 --- a/test/test.js +++ b/test/test.js @@ -911,6 +911,16 @@ describe('Puppeteer', function() { await page.click('#button-80'); expect(await page.$('#button-80', button => button.textContent)).toBe('clicked'); })); + it('should click a partially obscured button', SX(async function() { + await page.navigate(PREFIX + '/input/button.html'); + await page.$('button', button => { + button.textContent = 'Some really long text that will go offscreen'; + button.style.position = 'absolute'; + button.style.left = '368px'; + }); + await page.click('button'); + expect(await page.evaluate(() => window.result)).toBe('Clicked'); + })); it('should select the text with mouse', SX(async function(){ await page.navigate(PREFIX + '/input/textarea.html'); await page.focus('textarea');