-3

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?

7
  • 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 Commented May 25, 2016 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 Commented May 25, 2016 at 12:22
  • Duplicate question. Refer old questions before publishing new. Commented May 25, 2016 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 Commented May 25, 2016 at 12:36
  • Reopened so someone can try to translate the Java on this answer into JavaScript. I tried, but couldn't find a contextClick action Commented May 25, 2016 at 13:21

1 Answer 1

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);
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

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.
@rajNishKuMar thanks for trying, but is still not what I'm looking for
@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.
That is, you should be able to run browser.executeScript() passing it the JavaScript above
@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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.