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.
This commit is contained in:
Pavel Feldman 2017-07-11 16:25:25 -07:00 committed by Andrey Lushnikov
parent 279cd4c9fb
commit afb096cfd7

View File

@ -61,8 +61,6 @@ class Page extends EventEmitter {
this._screenDPI = screenDPI;
/** @type {!Map<string, function>} */
this._inPageCallbacks = new Map();
/** @type {?Promise<number>} */
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<number>}
*/
_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<number>}
*/
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<string>} filePaths
* @param {!Array<string>} 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');
}
}
}