JavaScript simula el código de clic derecho
-
10-07-2019 - |
Pregunta
Estoy escribiendo algunas pruebas de IU usando Selenium y tengo un control de JavaScript Tree, usando el kit de herramientas Dojo.
He implementado un menú contextual para cada nodo del árbol utilizando los ejemplos que proporciona Dojo, pero necesito la prueba de Selenium para invocar " haga clic derecho en el nodo del árbol, pero no puedo hacer que esto funcione. Las pruebas simplemente no simulan el evento de clic derecho a través de JavaScript, y el menú contextual no aparece.
¿Alguien ha tenido alguna experiencia invocando el clic derecho en un menú contextual usando Dojo y Selenium? ¿O tienes alguna idea sobre cómo hacerlo?
Solución
intente esto, porque lo que no funcionó del todo es que el menú contextual está vinculado al evento oncontextmenu.
function contextMenuClick(element){
var evt = element.ownerDocument.createEvent('MouseEvents');
var RIGHT_CLICK_BUTTON_CODE = 2; // the same for FF and IE
evt.initMouseEvent('contextmenu', true, true,
element.ownerDocument.defaultView, 1, 0, 0, 0, 0, false,
false, false, false, RIGHT_CLICK_BUTTON_CODE, null);
if (document.createEventObject){
// dispatch for IE
return element.fireEvent('onclick', evt)
}
else{
// dispatch for firefox + others
return !element.dispatchEvent(evt);
}
}
Otros consejos
Solo por si acaso, aquí hay un poco de información sobre los parámetros:
var myEvt = document.createEvent('MouseEvents');
myEvt.initMouseEvent(
'click' // event type
,true // can bubble?
,true // cancelable?
,window // the event's abstract view (should always be window)
,1 // mouse click count (or event "detail")
,100 // event's screen x coordinate
,200 // event's screen y coordinate
,100 // event's client x coordinate
,200 // event's client y coordinate
,false // whether or not CTRL was pressed during event
,false // whether or not ALT was pressed during event
,false // whether or not SHIFT was pressed during event
,false // whether or not the meta key was pressed during event
,1 // indicates which button (if any) caused the mouse event (1 = primary button)
,null // relatedTarget (only applicable for mouseover/mouseout events)
);
¡Gran pregunta!
Investigué un poco y parece que puedes activar un evento de mouse como se muestra aquí , y haga clic con el botón derecho del mouse configurando el botón
o which
en 2 ( documentado aquí ).
Quizás este código funcione:
function rightClick(element){
var evt = element.ownerDocument.createEvent('MouseEvents');
var RIGHT_CLICK_BUTTON_CODE = 2; // the same for FF and IE
evt.initMouseEvent('click', true, true,
element.ownerDocument.defaultView, 1, 0, 0, 0, 0, false,
false, false, false, RIGHT_CLICK_BUTTON_CODE, null);
if (document.createEventObject){
// dispatch for IE
return element.fireEvent('onclick', evt)
}
else{
// dispatch for firefox + others
return !element.dispatchEvent(evt);
}
}
Aquí hay una versión más correcta si no le importa dónde se activa el menú contextual
function fireContextMenu(el) {
var evt = el.ownerDocument.createEvent("HTMLEvents")
evt.initEvent('contextmenu', true, true) // bubbles = true, cancelable = true
if (document.createEventObject) {
return el.fireEvent('oncontextmenu', evt)
}
else {
return !el.dispatchEvent(evt)
}
}
Si lo hace, es posible que tengamos que usar el anterior, arreglar su comportamiento en IE y llenar apropiadamente la pantalla X, pantalla Y, cliente X, cliente, etc.
Estoy intentando esto en Firefox y Chrome, pero el envío del evento contextmenu no hace que el navegador abra el menú contextual. El evento se activa porque mi devolución de llamada para oncontextmenu está activada, pero todavía falta el menú contextual. ¿Alguien tiene una idea, porque usé todos los ejemplos de código de arriba?