Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

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 provides, 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?

1 Answer

0 votes
by (62.9k points)

You can fire a mouse event like is shown here, and make it a right-click by setting the button or which property to 2 (documented here)As there are two properties for finding out which mouse button has been clicked: which and button. Please note that these properties donot invariably work on a click event. To safely discover a button you have got to use the mousedown or mouseup events.

which is an old Netscape property. The left button produces a value of 1, the middle button (mouse wheel) produces a value of 2, the right button produces a value of 3. No issues, except its meager support (and the very fact that it’s conjointly used for key detection)

Perhaps this code will work:

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);

  }

}

Browse Categories

...