Skip to content
This repository has been archived by the owner on May 10, 2018. It is now read-only.

Commit

Permalink
Correctly handle mouse event submission (fixes #29)
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 committed May 11, 2017
1 parent 12bd710 commit c81c829
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
3 changes: 0 additions & 3 deletions src/ElectronDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,9 +1008,6 @@ protected function dispatchMouseEvent($type, $x, $y, $modifiers = null, $timesta

$this->sendAndWaitWithoutResult('dispatchMouseEvent', [$params]);

usleep(10000); // FIXME Unfortunately, couldn't find a way to immediately detect location change
// One possible fix is to remove sleep from here and put it into click/dblclick/rightclick methods

$this->handleExecutionResponse('Could not dispatch mouse event: %s');
}

Expand Down
28 changes: 28 additions & 0 deletions src/Server/Preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
setWindowIdName = remote.getGlobal('setWindowIdName'),
getWindowNameFromId = remote.getGlobal('getWindowNameFromId'),
setFileFromScript = remote.getGlobal('setFileFromScript'),
setExecutionResponse = remote.getGlobal('setExecutionResponse'),
DELAY_SCRIPT_RESPONSE = remote.getGlobal('DELAY_SCRIPT_RESPONSE'),
electronWebContents = remote.getCurrentWebContents();

Expand Down Expand Up @@ -343,17 +344,44 @@
input.click();
},

/**
* @param {String} xpath
* @returns {Node}
*/
'getElementByXPath': function (xpath) {
return document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
},

/**
* @param {HTMLElement} element
* @returns {{x: number, y: number}}
*/
'getElementCenterPos': function (element) {
const rect = element.getBoundingClientRect();

return {
'x': Math.round(rect.left + (rect.width / 2)),
'y': Math.round(rect.top + (rect.height / 2))
};
},

/**
* @param {String} rdEventType
*/
'handleMouseEventOnce': function (rdEventType) {
const rdEventTypeToJsEventMap = {
'mouseMoved': 'mousemove',
'mousePressed': 'mousedown',
'mouseReleased': 'mouseup'
};

if (!rdEventTypeToJsEventMap[rdEventType]) {
throw new Error('RemoteDebug event named "' + rdEventType + '" is not supported.');
}

window.addEventListener(rdEventTypeToJsEventMap[rdEventType], function () {
setExecutionResponse({'result': true});
}, {catpure: true, once: true});
}
}
})();
39 changes: 27 additions & 12 deletions src/Server/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ Electron.app.on('ready', function() {
executeResponse = {'error': errorToString(error)};
};

/**
* @param {*} response
*/
global.setExecutionResponse = function (response) {
executeResponse = response;
};

/**
* Sets flag indicating that something caused the current page to start unloading.
* @param {boolean} value
Expand Down Expand Up @@ -845,18 +852,26 @@ Electron.app.on('ready', function() {

executeResponse = null;

currWindow.webContents.debugger.sendCommand(
'Input.dispatchMouseEvent',
params,
function (error) {
if (isEmptyObject(error)) {
executeResponse = {'result': true};
} else {
Logger.error('Could not dispatch mouse event (%j): %s', params, errorToString(error));
executeResponse = {'error': errorToString(error)};
}
}
);
currWindow.webContents
.executeJavaScript('Electron.handleMouseEventOnce(' + JSON.stringify(params.type) + ');')
.then(function () {
currWindow.webContents.debugger.sendCommand(
'Input.dispatchMouseEvent',
params,
function (error) {
if (isEmptyObject(error)) {
Logger.debug('Mouse event fired, waiting for it to be caught...');
} else {
Logger.error('Could not dispatch mouse event (%j): %s', params, errorToString(error));
executeResponse = {'error': errorToString(error)};
}
}
);
})
.catch(function (error) {
Logger.error('Could create mouse event listener (%j): %s', params, errorToString(error));
executeResponse = {'error': errorToString(error)};
});

cb();
},
Expand Down

0 comments on commit c81c829

Please sign in to comment.