Roll chromium to r486981. (#87)

This patch rolls chromium to r486981 and prepares
for the introduction of device emulation.
This commit is contained in:
Pavel Feldman 2017-07-17 12:15:06 -07:00 committed by Andrey Lushnikov
parent 9eb46d613d
commit 4581ada210
4 changed files with 49 additions and 81 deletions

View File

@ -44,18 +44,15 @@ class Navigator {
let loadEventFired = new Promise(fulfill => this._client.once('Page.loadEventFired', fulfill)).then(() => null); let loadEventFired = new Promise(fulfill => this._client.once('Page.loadEventFired', fulfill)).then(() => null);
let watchdog = new Promise(fulfill => this._maximumTimer = setTimeout(fulfill, this._maxTime)).then(() => new Error('Navigation Timeout Exceeded: ' + this._maxTime + 'ms exceeded')); let watchdog = new Promise(fulfill => this._maximumTimer = setTimeout(fulfill, this._maxTime)).then(() => new Error('Navigation Timeout Exceeded: ' + this._maxTime + 'ms exceeded'));
// Await for the command to throw exception in case of illegal arguments.
try { try {
// Await for the command to throw exception in case of illegal arguments.
await this._client.send('Page.navigate', {url: this._url, referrer: this._referrer}); await this._client.send('Page.navigate', {url: this._url, referrer: this._referrer});
} catch (e) {
this._cleanup();
throw e;
}
const error = await Promise.race([certificateError, watchdog, this._waitUntil === 'load' ? loadEventFired : networkIdle]); const error = await Promise.race([certificateError, watchdog, this._waitUntil === 'load' ? loadEventFired : networkIdle]);
this._cleanup();
if (error) if (error)
throw error; throw error;
} finally {
this._cleanup();
}
} }
/** /**

View File

@ -35,13 +35,11 @@ class Page extends EventEmitter {
client.send('Runtime.enable', {}), client.send('Runtime.enable', {}),
client.send('Security.enable', {}), client.send('Security.enable', {}),
]); ]);
let screenDPIExpression = helper.evaluationString(() => window.devicePixelRatio);
let {result:{value: screenDPI}} = await client.send('Runtime.evaluate', { expression: screenDPIExpression, returnByValue: true });
let userAgentExpression = helper.evaluationString(() => window.navigator.userAgent); let userAgentExpression = helper.evaluationString(() => window.navigator.userAgent);
let {result:{value: userAgent}} = await client.send('Runtime.evaluate', { expression: userAgentExpression, returnByValue: true }); let {result:{value: userAgent}} = await client.send('Runtime.evaluate', { expression: userAgentExpression, returnByValue: true });
let frameManager = await FrameManager.create(client); let frameManager = await FrameManager.create(client);
let networkManager = new NetworkManager(client, userAgent); let networkManager = new NetworkManager(client, userAgent);
let page = new Page(client, frameManager, networkManager, screenDPI); let page = new Page(client, frameManager, networkManager);
// Initialize default page size. // Initialize default page size.
await page.setViewportSize({width: 400, height: 300}); await page.setViewportSize({width: 400, height: 300});
return page; return page;
@ -51,14 +49,12 @@ class Page extends EventEmitter {
* @param {!Connection} client * @param {!Connection} client
* @param {!FrameManager} frameManager * @param {!FrameManager} frameManager
* @param {!NetworkManager} networkManager * @param {!NetworkManager} networkManager
* @param {number} screenDPI
*/ */
constructor(client, frameManager, networkManager, screenDPI) { constructor(client, frameManager, networkManager) {
super(); super();
this._client = client; this._client = client;
this._frameManager = frameManager; this._frameManager = frameManager;
this._networkManager = networkManager; this._networkManager = networkManager;
this._screenDPI = screenDPI;
/** @type {!Map<string, function>} */ /** @type {!Map<string, function>} */
this._inPageCallbacks = new Map(); this._inPageCallbacks = new Map();
@ -260,20 +256,17 @@ class Page extends EventEmitter {
* @return {!Promise<!Response>} * @return {!Promise<!Response>}
*/ */
async navigate(url, options) { async navigate(url, options) {
const referrer = this._networkManager.httpHeaders()['referer'];
/** @type {!Map<string, !Response>} */ /** @type {!Map<string, !Response>} */
const responses = new Map(); const responses = new Map();
const onResponse = response => responses.set(response.url, response); const onResponse = response => responses.set(response.url, response);
const navigator = new Navigator(this._client, url, this._networkManager.httpHeaders().referer, options); const navigator = new Navigator(this._client, url, referrer, options);
try {
this._networkManager.on(NetworkManager.Events.Response, onResponse); this._networkManager.on(NetworkManager.Events.Response, onResponse);
try {
await navigator.navigate(); await navigator.navigate();
} catch (e) { } finally {
this._networkManager.removeListener(NetworkManager.Events.Response, onResponse); this._networkManager.removeListener(NetworkManager.Events.Response, onResponse);
throw e;
} }
this._networkManager.removeListener(NetworkManager.Events.Response, onResponse);
const response = responses.get(this.mainFrame().url()); const response = responses.get(this.mainFrame().url());
console.assert(response); console.assert(response);
return response; return response;
@ -283,25 +276,9 @@ class Page extends EventEmitter {
* @param {!{width: number, height: number}} size * @param {!{width: number, height: number}} size
* @return {!Promise} * @return {!Promise}
*/ */
async setViewportSize(size) { setViewportSize(size) {
this._viewportSize = size; this._viewportSize = size;
let width = size.width; return this._resetDeviceEmulation();
let height = size.height;
let zoom = this._screenDPI;
return Promise.all([
this._client.send('Emulation.setDeviceMetricsOverride', {
width,
height,
deviceScaleFactor: 1,
scale: 1 / zoom,
mobile: false,
fitWindow: false
}),
this._client.send('Emulation.setVisibleSize', {
width: Math.floor(width / zoom),
height: Math.floor(height / zoom),
})
]);
} }
/** /**
@ -311,6 +288,17 @@ class Page extends EventEmitter {
return this._viewportSize; return this._viewportSize;
} }
_resetDeviceEmulation() {
const width = this._viewportSize.width;
const height = this._viewportSize.height;
const deviceScaleFactor = 1;
const mobile = false;
const screenOrientation = { angle: 0, type: 'portraitPrimary' };
return Promise.all([
this._client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation }),
]);
}
/** /**
* @param {function()} pageFunction * @param {function()} pageFunction
* @param {!Array<*>} args * @param {!Array<*>} args
@ -371,48 +359,31 @@ class Page extends EventEmitter {
} }
/** /**
* @param {string} screenshotType * @param {string} format
* @param {!Object=} options * @param {!Object=} options
* @return {!Promise<!Buffer>} * @return {!Promise<!Buffer>}
*/ */
async _screenshotTask(screenshotType, options) { async _screenshotTask(format, options) {
if (options.clip) { if (options.fullPage) {
await Promise.all([ let metrics = await this._client.send('Page.getLayoutMetrics');
this._client.send('Emulation.setVisibleSize', { const width = Math.ceil(metrics.contentSize.width);
width: Math.ceil(options.clip.width / this._screenDPI), const height = Math.ceil(metrics.contentSize.height);
height: Math.ceil(options.clip.height / this._screenDPI), await this._client.send('Emulation.resetPageScaleFactor');
}), const mobile = false;
this._client.send('Emulation.forceViewport', { const deviceScaleFactor = 1;
x: options.clip.x / this._screenDPI, const landscape = false;
y: options.clip.y / this._screenDPI, const screenOrientation = landscape ? { angle: 90, type: 'landscapePrimary' } : { angle: 0, type: 'portraitPrimary' };
scale: 1, await this._client.send('Emulation.setDeviceMetricsOverride', { mobile, width, height, deviceScaleFactor, screenOrientation });
})
]);
} else if (options.fullPage) {
let response = await this._client.send('Page.getLayoutMetrics');
await Promise.all([
this._client.send('Emulation.setVisibleSize', {
width: Math.ceil(response.contentSize.width / this._screenDPI),
height: Math.ceil(response.contentSize.height / this._screenDPI),
}),
this._client.send('Emulation.forceViewport', {
x: 0,
y: 0,
scale: 1,
})
]);
}
let result = await this._client.send('Page.captureScreenshot', {
fromSurface: true,
format: screenshotType,
quality: options.quality
});
if (options.clip || options.fullPage) {
await Promise.all([
this.setViewportSize(this.viewportSize()),
this._client.send('Emulation.resetViewport')
]);
} }
let clip = options.clip ? Object.assign({}, options['clip']) : undefined;
if (clip)
clip.scale = 1;
let result = await this._client.send('Page.captureScreenshot', { format, quality: options.quality, clip });
if (options.fullPage)
await this.setViewportSize(this.viewportSize());
let buffer = new Buffer(result.data, 'base64'); let buffer = new Buffer(result.data, 'base64');
if (options.path) if (options.path)
fs.writeFileSync(options.path, buffer); fs.writeFileSync(options.path, buffer);
@ -523,8 +494,8 @@ class Page extends EventEmitter {
}, selector); }, selector);
if (!center) if (!center)
throw new Error('No node found for selector: ' + selector); throw new Error('No node found for selector: ' + selector);
let x = Math.round(center.x / this._screenDPI); let x = Math.round(center.x);
let y = Math.round(center.y / this._screenDPI); let y = Math.round(center.y);
this._client.send('Input.dispatchMouseEvent', { this._client.send('Input.dispatchMouseEvent', {
type: 'mouseMoved', type: 'mouseMoved',
x, y x, y

View File

@ -27,7 +27,7 @@
"ws": "^3.0.0" "ws": "^3.0.0"
}, },
"puppeteer": { "puppeteer": {
"chromium_revision": "485143" "chromium_revision": "486981"
}, },
"devDependencies": { "devDependencies": {
"commonmark": "^0.27.0", "commonmark": "^0.27.0",

View File

@ -221,7 +221,7 @@ describe('Puppeteer', function() {
await page.waitFor('*'); await page.waitFor('*');
fail('Failed waitFor did not throw.'); fail('Failed waitFor did not throw.');
} catch (e) { } catch (e) {
expect(e.message).toBe('Evaluation failed: document.querySelector is not a function'); expect(e.message).toContain('Evaluation failed: document.querySelector is not a function');
} }
})); }));
}); });