質問

Seleniumを使用していくつかのUIテストを書いており、Dojoツールキットを使用してJavaScriptツリーコントロールを持っています。

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

他のヒント

適切な測定のために、ここにパラメーターのドコを示します:

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

すばらしい質問!

調査を行ったところ、がここに表示されます button または which プロパティを2(ここに記載)。

おそらくこのコードは機能します:

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

  var RIGHT_CLICK_BUTTON_CODE = 2; // the same for FF and IE

  evt.initMouseEvent('click', 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);
  }
}

コンテキストメニューが表示される場所を気にしない場合は、これがより正しいバージョンです

function fireContextMenu(el) {
  var evt = el.ownerDocument.createEvent("HTMLEvents")
  evt.initEvent('contextmenu', true, true) // bubbles = true, cancelable = true

  if (document.createEventObject) {
    return el.fireEvent('oncontextmenu', evt)
  }
  else {
    return !el.dispatchEvent(evt)
  }
}

その場合、前のものを使用し、IEでの動作を修正し、screenX、screenY、clientX、clientYなどを適切に設定する必要があります

FirefoxとChromeでこれを試していますが、contextmenuイベントをディスパッチしても、ブラウザがコンテキストメニューを開くことはありません。 oncontextmenuのコールバックが発生するため、イベントがトリガーされますが、コンテキストメニューがまだありません。上記のすべてのコードサンプルを使用したため、誰もがアイデアを持っていますか?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top