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

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

问题描述

我正在使用 Selenium 编写一些 UI 测试,并且我有一个使用 Dojo 工具包的 JavaScript 树控件.

I am writing some UI tests using Selenium and i have a JavaScript Tree control, using the Dojo toolkit.

我已经使用 Dojo 提供的示例为树的每个节点实现了一个上下文菜单,但是我需要 Selenium 测试来调用"树节点上的右键单击,但是我无法让它工作.测试根本没有通过 JavaScript 模拟右键单击事件,也没有显示上下文菜单.

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.

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

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?

推荐答案

试试这个吧,事情不太奏效的原因是上下文菜单实际上绑定到 oncontextmenu 事件.

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天全站免登陆