[emulation] fix touch emulation

This patch:
- fixes touch emulation
- adds tests to cover basic Page.emulate

References #88.
This commit is contained in:
Andrey Lushnikov 2017-07-20 10:51:12 -07:00
parent 4af0911b90
commit 42edc3108a
2 changed files with 28 additions and 3 deletions

View File

@ -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;
}

View File

@ -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});