From 9c67ce52898e30a531782e5f25f8956264abe5c9 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 30 Apr 2020 14:13:47 -0700 Subject: [PATCH] test: simplify pausing tests (#2056) --- src/dom.ts | 8 +------ test/click.spec.js | 57 ++++++++++++---------------------------------- 2 files changed, 16 insertions(+), 49 deletions(-) diff --git a/src/dom.ts b/src/dom.ts index e54186ad505c0..c55fff8e406f2 100644 --- a/src/dom.ts +++ b/src/dom.ts @@ -239,8 +239,7 @@ export class ElementHandle extends js.JSHandle { async _performPointerAction(action: (point: types.Point) => Promise, deadline: number, options: PointerActionOptions & types.PointerActionWaitOptions & types.NavigatingActionWaitOptions = {}): Promise<'done' | 'retry'> { const { force = false, position } = options; - const skipStableCheck = (options as any).__testHookSkipStablePosition; - if (!force && !skipStableCheck) + if (!force) await this._waitForDisplayedAtStablePosition(deadline); let paused = false; @@ -248,11 +247,6 @@ export class ElementHandle extends js.JSHandle { await this._page._delegate.setActivityPaused(true); paused = true; - if (typeof skipStableCheck === 'function') - await skipStableCheck(); - else if (skipStableCheck) - await skipStableCheck; - // Scroll into view and calculate the point again while paused just in case something has moved. this._page._log(inputLog, 'scrolling into view if needed...'); await this._scrollRectIntoViewIfNeeded(position ? { x: position.x, y: position.y, width: 0, height: 0 } : undefined); diff --git a/test/click.spec.js b/test/click.spec.js index 3ef1c96a4999e..afe8823c40d2d 100644 --- a/test/click.spec.js +++ b/test/click.spec.js @@ -570,47 +570,19 @@ describe('Page.click', function() { expect(clicked).toBe(true); expect(await page.evaluate(() => window.clicked)).toBe(true); }); - it('should retry when element jumps during hit testing', async({page, server}) => { - await page.goto(server.PREFIX + '/input/animating-button.html'); - await page.evaluate(() => addButton()); - let clicked = false; - const handle = await page.$('button'); - const __testHookBeforeHitTarget = () => page.evaluate(() => { if (window.x === 0) jump(); }); - const promise = handle.click({ timeout: 0, __testHookBeforeHitTarget }).then(() => clicked = true); - expect(clicked).toBe(false); - expect(await page.evaluate(() => window.clicked)).toBe(undefined); - await page.evaluate(() => stopButton()); - await promise; - expect(clicked).toBe(true); - expect(await page.evaluate(() => window.clicked)).toBe(true); - }); it('should fail when element jumps during hit testing', async({page, server}) => { - await page.goto(server.PREFIX + '/input/animating-button.html'); - await page.evaluate(() => addButton()); - await page.evaluate(() => stopButton()); + await page.setContent(''); let clicked = false; const handle = await page.$('button'); - const __testHookBeforeHitTarget = () => page.evaluate(() => jump()); - const promise = handle.click({ timeout: 1000, __testHookBeforeHitTarget, __testHookSkipStablePosition: true }).then(() => clicked = true).catch(e => e); + const __testHookBeforeHitTarget = () => page.evaluate(() => { + const margin = parseInt(document.querySelector('button').style.marginLeft || 0) + 100; + document.querySelector('button').style.marginLeft = margin + 'px'; + }); + const promise = handle.click({ timeout: 1000, __testHookBeforeHitTarget }).then(() => clicked = true).catch(e => e); const error = await promise; expect(clicked).toBe(false); expect(await page.evaluate(() => window.clicked)).toBe(undefined); - expect(error.message).toBe('waiting for element to receive pointer events failed: timeout exceeded'); - }); - it.fail(CHROMIUM || FFOX)('should work when element jumps uncontrollably', async({page, server}) => { - // This test requires pausing the page. - await page.goto(server.PREFIX + '/input/animating-button.html'); - await page.evaluate(() => addButton()); - await page.evaluate(() => stopButton()); - const handle = await page.$('button'); - await page.evaluate(() => startJumping()); - let clicked = false; - const promise = handle.click({ timeout: 1000, __testHookSkipStablePosition: true }).then(() => clicked = true); - expect(clicked).toBe(false); - expect(await page.evaluate(() => window.clicked)).toBe(undefined); - await promise; - expect(clicked).toBe(true); - expect(await page.evaluate(() => window.clicked)).toBe(true); + expect(error.message).toContain('timeout exceeded'); }); it.fail(CHROMIUM || FFOX)('should pause animations', async({page}) => { // This test requires pausing the page. @@ -621,6 +593,7 @@ describe('Page.click', function() { } .spinner { animation: spinner 2s linear infinite; + animation-delay: 500ms; }
@@ -629,28 +602,28 @@ describe('Page.click', function() { onclick="window.clicked=true">
`); - await page.click('#target', { __testHookSkipStablePosition: new Promise(f => setTimeout(f, 100)) }); + await page.click('#target', { __testHookBeforeHitTarget: () => new Promise(f => setTimeout(f, 1000)) }); expect(await page.evaluate(() => window.clicked)).toBe(true); }); it.fail(CHROMIUM || FFOX)('should defer timers', async({page}) => { // This test requires pausing the page. await page.setContent(``); - await page.click('button', { __testHookSkipStablePosition: async () => { + await page.click('button', { __testHookBeforeHitTarget: async () => { // Schedule a timer that hides the element await page.evaluate(() => setTimeout(() => button.style.display = 'none', 0)); // Allow enough time for timer to fire - await page.waitForTimeout(100); + await page.waitForTimeout(500); }}); expect(await page.evaluate(() => window.clicked)).toBe(true); }); it.fail(CHROMIUM || FFOX)('should defer rafs', async({page}) => { // This test requires pausing the page. await page.setContent(``); - await page.click('button', { __testHookSkipStablePosition: async () => { + await page.click('button', { __testHookBeforeHitTarget: async () => { // Schedule a timer that hides the element await page.evaluate(() => requestAnimationFrame(() => button.style.display = 'none')); // Allow enough time for raf to fire - await page.waitForTimeout(100); + await page.waitForTimeout(500); }}); expect(await page.evaluate(() => window.clicked)).toBe(true); }); @@ -658,11 +631,11 @@ describe('Page.click', function() { // This test requires pausing the page. await page.goto(server.EMPTY_PAGE); await page.setContent(``); - await page.click('button', { __testHookSkipStablePosition: async () => { + await page.click('button', { __testHookBeforeHitTarget: async () => { // Fetch that would immediately delete button. page.evaluate(() => fetch(window.location.href).then(() => button.style.display = 'none')); // Allow enough time for raf to fire - await page.waitForTimeout(100); + await page.waitForTimeout(500); }}); expect(await page.evaluate(() => window.clicked)).toBe(true); });