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?
Asked
Active
Viewed 3,412 times
-3
-
I'm assuming you want to interact with the system context menu, not one that you implemented yourself? Also, you have to mention what you have tried, questions that don't show your existing code, expected vs actual behaviour, aren't as useful to others – Ruan Mendes May 25 '16 at 11:59
-
Ok. So first of all it's not a duplicate because I'm looking something in javascript not java. There are different method and functions and they don't work. I just can't find the method or something what should I use. There is no code to show because I just tested logging in and now I want right click on a pulpit/background/whatever of aplication – jabussko May 25 '16 at 12:22
-
Duplicate question. Refer old questions before publishing new. – Kishan Patel May 25 '16 at 12:29
-
Seriously? I checked. And try those which in javascript but it's not working. Webdriver don't recognize any of those methods. So please if you can't help or understand that code in java and javascript isn't that same just don't bother to disturb. Or maybe for you it's easy to translate java on javascript, then still you should help, because for me this link is useless – jabussko May 25 '16 at 12:36
-
Reopened so someone can try to translate the Java on [this answer](http://stackoverflow.com/a/11450187/227299) into JavaScript. I tried, but couldn't find a `contextClick` action – Ruan Mendes May 25 '16 at 13:21
-
@JuanMendes thank you – jabussko May 25 '16 at 13:31
-
You can do this with ActionSequence. Solution here: https://stackoverflow.com/a/37929528/6486717 – johndoek Jun 20 '16 at 18:46
1 Answers
2
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);
}
}

Community
- 1
- 1

Rajnish Kumar
- 2,828
- 5
- 25
- 39
-
2Please 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. – Ruan Mendes May 25 '16 at 12:09
-
@rajNishKuMar thanks for trying, but is still not what I'm looking for – jabussko May 25 '16 at 12:39
-
@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 May 25 '16 at 13:31
-
That is, you should be able to run `browser.executeScript()` passing it the JavaScript above – Ruan Mendes May 25 '16 at 13:44
-
@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 – jabussko May 25 '16 at 13:52
-
@jabussko You have to run it in the context of the browser, for example using `browser.executeScript`. You have to get better at explaining what you have tried and what went wrong and what was expected or you won't get good answers at StackOverflow. – Ruan Mendes May 25 '16 at 13:55
-
Probably I find the answer on [link](https://github.com/christian-bromann/webdriverjs) . There is method _italic_ **bold** 'rightClick(String selector, Function callback)' but when I try this I get response that is not a function or method, maybe I'm trying it wrong way? – jabussko May 25 '16 at 13:57
-
@jabussko How many times do I have to tell you that you are not expressing well what you have tried? Don't show what you have tried in a comment, it doesn't make sense out of context. Create a code block in your question showing what you have tried (with full context, that is, no undefined variables) and what actually happened along with any error messages – Ruan Mendes May 25 '16 at 14:03