Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript simulate right click through code

I am writing some UI tests using Selenium and i have a JavaScript Tree control, using the Dojo toolkit.

I have implemented a context menu for each node of the tree using the examples that Dojo provide, but I need the Selenium test to "invoke" the right click on the tree node, but I cannot get this to work. The tests simply do not simulate the right-click event through JavaScript, and the context menu does not show up.

Has anyone had any experience in invoking the right click on a context menu using Dojo and Selenium? Or have any ideas as to how to do it?

like image 718
Mark Avatar asked Jan 11 '09 22:01

Mark


People also ask

How to simulate right click in JavaScript?

Depending on what you're doing, you may wish to trigger a mouse down and then a mouse up, which could go like this: $('#recover'). trigger({ type: 'mousedown', which: 3 }). trigger({ type: 'mouseup', which: 3 });

How do I simulate a mouse click on my website?

click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.


1 Answers

try this instead, reason what things didn't quite work is that the context menu is in fact bound to the oncontextmenu event.

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);     } } 
like image 93
leiyou Avatar answered Sep 21 '22 07:09

leiyou