I need a JavaScript code to make mouse double click by itself. I will use it inside my Java code . This one is a selenium project for testing-purposes but there is not any way t
To make Mouse Double Click
you can write a script and pass it to the executeScript()
method as follows :
Script :
String jsDoubleClick =
"var target = arguments[0]; " +
"var offsetX = arguments[1]; " +
"var offsetY = arguments[2]; " +
"var rect = target.getBoundingClientRect(); " +
"var cx = rect.left + (offsetX || (rect.width / 2)); " +
"var cy = rect.top + (offsetY || (rect.height / 2)); " +
" " +
"emit('mousedown', {clientX: cx, clientY: cy, buttons: 1}); " +
"emit('mouseup', {clientX: cx, clientY: cy}); " +
"emit('mousedown', {clientX: cx, clientY: cy, buttons: 1}); " +
"emit('mouseup', {clientX: cx, clientY: cy}); " +
"emit('click', {clientX: cx, clientY: cy, detail: 2}); " +
" " +
"function emit(name, init) { " +
"target.dispatchEvent(new MouseEvent(name, init)); " +
"} " ;
Invoking the script through executeScript()
from your @Test
:
new Actions(driver).moveToElement(myElem, posX, posY).perform();
((JavascriptExecutor)driver).executeScript(jsDoubleClick, myElem, posX, posY);
Using JQuery:
$(selector).dblclick()
As Mozfet says JQuery ! it's so easy with JQuery !
$("#myId").trigger('dblclick');
then you listen for this double click
$("#myId").on('dblclick',function(){
// do it !
});
// you can event make you own event
$("#myId").trigger('retrieve');
then you listen for this custom event
$("#myId").on('retrieve',function(){
// do it !
});
I often use it in datatables : I've got a popover (a small menu on the left td of each row) if the user choose "open modal" i then trigger a double click which goes to the table which is already waiting for a double click from the user on the row. So i don't need to implement 2 events