最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Right Click with Nightwatch - Stack Overflow

programmeradmin0浏览0评论

I am trying to test my GUI with Nightwatch. I can not seem to find how to simulate a right click. I went through the API Reference page() and searched everywhere. Am I missing something here? Because I believe that right clicking should be one of the most basic functionalities.

I am trying to test my GUI with Nightwatch. I can not seem to find how to simulate a right click. I went through the API Reference page(http://nightwatchjs/api) and searched everywhere. Am I missing something here? Because I believe that right clicking should be one of the most basic functionalities.

Share Improve this question asked Mar 12, 2015 at 13:45 lamyaruslamyarus 1662 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Good news !!!

Since Nightwatch.js v0.6.13, you can fire a real right click :-)

"Right Click to Show ContextMenu" : function (browser) {
  browser
   .moveToElement('#targetElement')
   .mouseButtonClick('right')
   .pause(5000)
   .end();
}

EDIT: This DOESN'T WORK. I am going to leave it here anyways. It might be helpful somehow.

I found a work around. mousebuttonDown() method allows the use of left, middle and right clicks. They are assigned 0,1 and 2 respectively. So the following somehow simulates a rightclick:


"Right Click to Show ContextMenu" : function (browser) {
    browser
       .moveToElement(/*locate your element here*/)
       .mouseButtonDown(2)
       .mouseButtonUp(2)
       .end();
}

I have the same issue with selenium-webdriver ...

But right now i'm using that workaround for Nightwatch.js:

"Right Click to Show ContextMenu" : function (browser) {
     // inject script in client
     browser.execute(function(selector){
         // dispatch "context menu" event 
         $(selector).trigger('contextmenu');
         return true;
    }, ['#menu'])
    .pause(5000)
    .end();
}

(cf. Nightwatch API)

Or in pure JS :
document.querySelector('.logo').dispatchEvent(new CustomEvent('contextmenu'));

(cf. Trigger right click using pure Javascript)

And check your target webpage :

   $('#menu').on('contextmenu', function () {
      alert('context menu');
      //return false;     // cancel default menu
   });
发布评论

评论列表(0)

  1. 暂无评论