我正在使用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; // 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); } }
只是为了好的衡量,这里有一些关于参数的doco:
var myEvt = document.createEvent('MouseEvents'); myEvt.initMouseEvent( 'click' // event type ,true // can bubble? ,true // cancelable? ,window // the event's abstract view (should always be window) ,1 // mouse click count (or event "detail") ,100 // event's screen x coordinate ,200 // event's screen y coordinate ,100 // event's client x coordinate ,200 // event's client y coordinate ,false // whether or not CTRL was pressed during event ,false // whether or not ALT was pressed during event ,false // whether or not SHIFT was pressed during event ,false // whether or not the meta key was pressed during event ,1 // indicates which button (if any) caused the mouse event (1 = primary button) ,null // relatedTarget (only applicable for mouseover/mouseout events) );