From 42edc3108ab7a92958bbb5fbe0d9e049961ef75b Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Thu, 20 Jul 2017 10:51:12 -0700 Subject: [PATCH] [emulation] fix touch emulation This patch: - fixes touch emulation - adds tests to cover basic Page.emulate References #88. --- lib/EmulationManager.js | 6 +++--- test/test.js | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/EmulationManager.js b/lib/EmulationManager.js index d73e67d0c91..85425ad617f 100644 --- a/lib/EmulationManager.js +++ b/lib/EmulationManager.js @@ -41,7 +41,7 @@ class EmulationManager { height: viewport.height, deviceScaleFactor: device.deviceScaleFactor, isMobile: device.capabilities.includes('mobile'), - hasMobile: device.capabilities.includes('touch'), + hasTouch: device.capabilities.includes('touch'), isLandscape: options.landscape || false }; } @@ -81,13 +81,13 @@ class EmulationManager { let reloadNeeded = false; if (viewport.hasTouch && !client[EmulationManager._touchScriptId]) { const source = `(${injectedTouchEventsFunction})()`; - client[EmulationManager._touchScriptId] = await client.send('Runtime.addScriptToEvaluateOnNewDocument', { source }); + client[EmulationManager._touchScriptId] = (await client.send('Page.addScriptToEvaluateOnNewDocument', { source })).identifier; reloadNeeded = true; } if (!viewport.hasTouch && client[EmulationManager._touchScriptId]) { + await client.send('Page.removeScriptToEvaluateOnNewDocument', {identifier: client[EmulationManager._touchScriptId]}); client[EmulationManager._touchScriptId] = null; - await client.send('Runtime.removeScriptToEvaluateOnNewDocument', EmulationManager._emulatingTouch); reloadNeeded = true; } diff --git a/test/test.js b/test/test.js index ff8199d2503..607c82d5df0 100644 --- a/test/test.js +++ b/test/test.js @@ -1046,6 +1046,31 @@ describe('Puppeteer', function() { })); }); + describe('Page.emulate', function() { + it('should respect viewport meta tag', SX(async function() { + await page.navigate(PREFIX + '/mobile.html'); + expect(await page.evaluate(() => window.innerWidth)).toBe(400); + await page.emulate('iPhone 6'); + expect(await page.evaluate(() => window.innerWidth)).toBe(375); + await page.setViewport({width: 400, height: 300}); + expect(await page.evaluate(() => window.innerWidth)).toBe(400); + })); + it('should enable/disable touch', SX(async function() { + await page.navigate(PREFIX + '/mobile.html'); + expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(false); + await page.emulate('iPhone 6'); + expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(true); + await page.setViewport({width: 100, height: 100}); + expect(await page.evaluate(() => 'ontouchstart' in window)).toBe(false); + })); + it('should emulate UA', SX(async function() { + await page.navigate(PREFIX + '/mobile.html'); + expect(await page.evaluate(() => navigator.userAgent)).toContain('Chrome'); + await page.emulate('iPhone 6'); + expect(await page.evaluate(() => navigator.userAgent)).toContain('Safari'); + })); + }); + describe('Page.screenshot', function() { it('should work', SX(async function() { await page.setViewport({width: 500, height: 500});