JavaScript模拟右键单击代码 [英] JavaScript simulate right click through code

查看:182
本文介绍了JavaScript模拟右键单击代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium编写一些UI测试,并使用Dojo工具包进行JavaScript Tree控件。



我使用Dojo提供的示例实现了树的每个节点的上下文菜单,但是我需要Selenium测试来调用树上的右键单击节点,但我不能让这个工作。测试根本不会通过JavaScript模拟右键单击事件,并且上下文菜单不显示。



有人有任何经验调用右键单击使用Dojo和Selenium的上下文菜单?或者有什么想法如何做?



感谢您提供的任何帮助。



标记

解决方案

尝试这样做,因为什么事情不起作用是上下文菜单实际上绑定到oncontextmenu事件。

  function contextMenuClick(element){
var evt = element.ownerDocument.createEvent('MouseEvents');

var RIGHT_CLICK_BUTTON_CODE = 2; //对于FF和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);
}
}


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

Thanks for any help that you can give.

Mark

解决方案

try this instead, reason what things didn't quite work is that the context menu is in fact bound to the oncontextmenu event.

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

这篇关于JavaScript模拟右键单击代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆