javascript selenium dojo mouseevent

JavaScript simula clic derecho en el código



selenium dojo (5)

Estoy escribiendo algunas pruebas de UI usando Selenium y tengo un control de árbol JavaScript, usando el kit de herramientas Dojo.

He implementado un menú contextual para cada nodo del árbol usando los ejemplos que proporciona Dojo, pero necesito la prueba de Selenium para "invocar" el 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 al invocar el clic derecho en un menú contextual usando Dojo y Selenium? ¿O tienes alguna idea sobre cómo hacerlo?


Aquí hay una versión más correcta si no te 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, reparar su comportamiento en IE y rellenar la pantalla X, screenY, clientX, clientY etc. apropiadamente


Estoy intentando esto en firefox y cromo, pero enviar el evento contextmenu no hace que el navegador abra el menú contextual. El evento se desencadena porque se activa mi devolución de llamada para oncontextmenu, pero el menú contextual sigue sin aparecer. ¿Alguien tiene una idea, porque utilicé todas las muestras de código de arriba?


Gran pregunta!

Investigué un poco, y parece que puede activar un evento de mouse como se muestra aquí , y hacer un clic con el button derecho estableciendo el button o which propiedad en 2 ( documentada 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); } }


Solo para una buena medida, aquí hay un poco de doco 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) );


prueba esto en su lugar, por lo que las cosas no funcionaron del todo es que el menú contextual está de hecho 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); } }