Use getBoundingClientRect instead of DOM.BoxModel (#76)

This patch re-implements `page.click()` and `page.focus()` using
`page.evaluate`
This commit is contained in:
JoelEinbinder 2017-07-13 18:32:34 -07:00 committed by Andrey Lushnikov
parent 56619baa64
commit 4bd855c66b

View File

@ -509,12 +509,20 @@ class Page extends EventEmitter {
* @param {!Promise} * @param {!Promise}
*/ */
async click(selector) { async click(selector) {
try { let center = await this.evaluate(selector => {
const nodeId = await this._querySelector(selector); let node = document.querySelector(selector);
let boxModel = (await this._client.send('DOM.getBoxModel', { nodeId })).model.content; if (!node)
let x = Math.round((boxModel[0] + boxModel[4]) / (2 * this._screenDPI)); return null;
let y = Math.round((boxModel[1] + boxModel[5]) / (2 * this._screenDPI)); let rect = node.getBoundingClientRect();
return {
x: (rect.left + rect.right) / 2,
y: (rect.top + rect.bottom) / 2
};
}, selector);
if (!center)
throw new Error('No node found for selector: ' + selector);
let x = Math.round(center.x / this._screenDPI);
let y = Math.round(center.y / this._screenDPI);
this._client.send('Input.dispatchMouseEvent', { this._client.send('Input.dispatchMouseEvent', {
type: 'mouseMoved', type: 'mouseMoved',
x, y x, y
@ -531,9 +539,6 @@ class Page extends EventEmitter {
x, y, x, y,
clickCount: 1 clickCount: 1
}); });
} finally {
await this._client.send('DOM.disable');
}
} }
/** /**
@ -541,12 +546,15 @@ class Page extends EventEmitter {
* @param {!Promise} * @param {!Promise}
*/ */
async focus(selector) { async focus(selector) {
try { let success = await this.evaluate(selector => {
const nodeId = await this._querySelector(selector); let node = document.querySelector(selector);
await this._client.send('DOM.focus', { nodeId }); if (!node)
} finally { return false;
await this._client.send('DOM.disable'); node.focus();
} return true;
}, selector);
if (!success)
throw new Error('No node found for selector: ' + selector);
} }
/** /**