From afb096cfd7b8b087b889822831a972552ebf6694 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Tue, 11 Jul 2017 16:25:25 -0700 Subject: [PATCH] Disable DOM domain after each operation. (#71) It's very heavy to have DOM domain enabled. This patch keeps DOM domain disabled after each use. --- lib/Page.js | 86 +++++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 46 deletions(-) diff --git a/lib/Page.js b/lib/Page.js index 4789e7c3..12d723bf 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -61,8 +61,6 @@ class Page extends EventEmitter { this._screenDPI = screenDPI; /** @type {!Map} */ this._inPageCallbacks = new Map(); - /** @type {?Promise} */ - this._rootNodeIdPromise = null; this._screenshotTaskChain = Promise.resolve(); @@ -80,7 +78,6 @@ class Page extends EventEmitter { client.on('Runtime.consoleAPICalled', event => this._onConsoleAPI(event)); client.on('Page.javascriptDialogOpening', event => this._onDialog(event)); client.on('Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails)); - client.on('DOM.documentUpdated', event => this._rootNodeIdPromise = null); } /** @@ -492,25 +489,14 @@ class Page extends EventEmitter { await this._client.dispose(); } - /** - * @return {!Promise} - */ - _rootNodeId() { - if (!this._rootNodeIdPromise) { - this._rootNodeIdPromise = this._client.send('DOM.getDocument', { - depth: 0 - }).then(obj => obj.root.nodeId); - } - return this._rootNodeIdPromise; - } - /** * @param {string} selector * @param {!Promise} */ async _querySelector(selector) { + let {root} = await this._client.send('DOM.getDocument', { depth: 1 }); let {nodeId} = await this._client.send('DOM.querySelector', { - nodeId: await this._rootNodeId(), + nodeId: root.nodeId, selector }); if (!nodeId) @@ -523,28 +509,31 @@ class Page extends EventEmitter { * @param {!Promise} */ async click(selector) { - let boxModel = (await this._client.send('DOM.getBoxModel', { - nodeId: await this._querySelector(selector) - })).model.content; - let x = Math.round((boxModel[0] + boxModel[4]) / (2 * this._screenDPI)); - let y = Math.round((boxModel[1] + boxModel[5]) / (2 * this._screenDPI)); + try { + const nodeId = await this._querySelector(selector); + let boxModel = (await this._client.send('DOM.getBoxModel', { nodeId })).model.content; + let x = Math.round((boxModel[0] + boxModel[4]) / 2); + let y = Math.round((boxModel[1] + boxModel[5]) / 2); - this._client.send('Input.dispatchMouseEvent', { - type: 'mouseMoved', - x, y - }); - this._client.send('Input.dispatchMouseEvent', { - type: 'mousePressed', - button: 'left', - x, y, - clickCount: 1 - }); - await this._client.send('Input.dispatchMouseEvent', { - type: 'mouseReleased', - button: 'left', - x, y, - clickCount: 1 - }); + this._client.send('Input.dispatchMouseEvent', { + type: 'mouseMoved', + x, y + }); + this._client.send('Input.dispatchMouseEvent', { + type: 'mousePressed', + button: 'left', + x, y, + clickCount: 1 + }); + await this._client.send('Input.dispatchMouseEvent', { + type: 'mouseReleased', + button: 'left', + x, y, + clickCount: 1 + }); + } finally { + await this._client.send('DOM.disable'); + } } /** @@ -552,9 +541,12 @@ class Page extends EventEmitter { * @param {!Promise} */ async focus(selector) { - await this._client.send('DOM.focus', { - nodeId: await this._querySelector(selector) - }); + try { + const nodeId = await this._querySelector(selector); + await this._client.send('DOM.focus', { nodeId }); + } finally { + await this._client.send('DOM.disable'); + } } /** @@ -591,14 +583,16 @@ class Page extends EventEmitter { /** * @param {string} selector - * @param {!Array} filePaths + * @param {!Array} files * @return {!Promise} */ - async uploadFile(selector, ...filePaths) { - await this._client.send('DOM.setFileInputFiles', { - nodeId: await this._querySelector(selector), - files: filePaths - }); + async uploadFile(selector, ...files) { + try { + const nodeId = await this._querySelector(selector); + await this._client.send('DOM.setFileInputFiles', { nodeId, files }); + } finally { + await this._client.send('DOM.disable'); + } } }