I have a problem. I Can't find any method letting me simulate right click in tests. I'm using selenium webdriver and there is many instructions but for java. And I'm writing tests in java script. Anyone know something more about it?
1 Answer
To simulate a right click in JavaScript, have a look at JavaScript simulate right click through code
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);
}
}
8 Comments
Ruan Mendes
Please don't just link to an answer. Either close the question as a duplicate or provide a link as a comment. At the very least, provide some code in the answer.
jabussko
@rajNishKuMar thanks for trying, but is still not what I'm looking for
Ruan Mendes
@jabussko Can you explain what you are looking for? The linked answer here explains how to make the context menu display using JavaScript just as you asked. "this is still not what I'm looking for" without a thorough explanation is a poor comment. That code should work.
Ruan Mendes
That is, you should be able to run
browser.executeScript() passing it the JavaScript abovejabussko
@JuanMendes but I'm not trying to make the context menu, just right-click on website. I tried this method and selenium didn't recognize initMouseEvent at all
|
contextClickaction