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}
*/
async click(selector) {
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 * this._screenDPI));
let y = Math.round((boxModel[1] + boxModel[5]) / (2 * this._screenDPI));
let center = await this.evaluate(selector => {
let node = document.querySelector(selector);
if (!node)
return null;
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', {
type: 'mouseMoved',
x, y
@ -531,9 +539,6 @@ class Page extends EventEmitter {
x, y,
clickCount: 1
});
} finally {
await this._client.send('DOM.disable');
}
}
/**
@ -541,12 +546,15 @@ class Page extends EventEmitter {
* @param {!Promise}
*/
async focus(selector) {
try {
const nodeId = await this._querySelector(selector);
await this._client.send('DOM.focus', { nodeId });
} finally {
await this._client.send('DOM.disable');
}
let success = await this.evaluate(selector => {
let node = document.querySelector(selector);
if (!node)
return false;
node.focus();
return true;
}, selector);
if (!success)
throw new Error('No node found for selector: ' + selector);
}
/**