[emulation] fix touch emulation
This patch: - fixes touch emulation - adds tests to cover basic Page.emulate References #88.
This commit is contained in:
parent
4af0911b90
commit
42edc3108a
@ -41,7 +41,7 @@ class EmulationManager {
|
|||||||
height: viewport.height,
|
height: viewport.height,
|
||||||
deviceScaleFactor: device.deviceScaleFactor,
|
deviceScaleFactor: device.deviceScaleFactor,
|
||||||
isMobile: device.capabilities.includes('mobile'),
|
isMobile: device.capabilities.includes('mobile'),
|
||||||
hasMobile: device.capabilities.includes('touch'),
|
hasTouch: device.capabilities.includes('touch'),
|
||||||
isLandscape: options.landscape || false
|
isLandscape: options.landscape || false
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -81,13 +81,13 @@ class EmulationManager {
|
|||||||
let reloadNeeded = false;
|
let reloadNeeded = false;
|
||||||
if (viewport.hasTouch && !client[EmulationManager._touchScriptId]) {
|
if (viewport.hasTouch && !client[EmulationManager._touchScriptId]) {
|
||||||
const source = `(${injectedTouchEventsFunction})()`;
|
const source = `(${injectedTouchEventsFunction})()`;
|
||||||
client[EmulationManager._touchScriptId] = await client.send('Runtime.addScriptToEvaluateOnNewDocument', { source });
|
client[EmulationManager._touchScriptId] = (await client.send('Page.addScriptToEvaluateOnNewDocument', { source })).identifier;
|
||||||
reloadNeeded = true;
|
reloadNeeded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!viewport.hasTouch && client[EmulationManager._touchScriptId]) {
|
if (!viewport.hasTouch && client[EmulationManager._touchScriptId]) {
|
||||||
|
await client.send('Page.removeScriptToEvaluateOnNewDocument', {identifier: client[EmulationManager._touchScriptId]});
|
||||||
client[EmulationManager._touchScriptId] = null;
|
client[EmulationManager._touchScriptId] = null;
|
||||||
await client.send('Runtime.removeScriptToEvaluateOnNewDocument', EmulationManager._emulatingTouch);
|
|
||||||
reloadNeeded = true;
|
reloadNeeded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
25
test/test.js
25
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() {
|
describe('Page.screenshot', function() {
|
||||||
it('should work', SX(async function() {
|
it('should work', SX(async function() {
|
||||||
await page.setViewport({width: 500, height: 500});
|
await page.setViewport({width: 500, height: 500});
|
||||||
|
Loading…
Reference in New Issue
Block a user