2010-07-20 51 views
4

我正在玩一個具有contextmenu的grails應用程序(右鍵單擊)。 上下文菜單使用Chris Domigan的jquery contextmenu plugin構建。如何在Web應用程序中測試上下文菜單功能?

儘管上下文菜單確實有效,但我想要進行自動化測試,但我無法弄清楚如何去做。

  • 我試過Selenium 2.05a(即Webdriver),但沒有rightClick方法。
  • 我注意到HtmlUnit有一個rightclick方法,但我似乎無法檢測到點擊之前和之後DOM之間的任何差異。

回答

0

雖然我希望能夠在Internet Explorer或Firefox中完成,但主要用法是HtmlUnit。很高興HtmlUnit HtmlElement有一個rightClick()方法,但不幸的是它的protected,因此無法從WebDriver包裝的HtmlUnitWebElement訪問。

我寫了一個黑客,使之接近,所以現在我可以打電話請右鍵單擊(),雖然它只有當它與運行的HtmlUnit工作 - 不是IE或者FF。

// Needs to be in this package to get access to the element 
package org.openqa.selenium.htmlunit; 

import com.gargoylesoftware.htmlunit.html.HtmlElement; 

public class OpenHtmlUnitWebElement extends HtmlUnitWebElement { 

    // Provide a constructor, even though we don't really need it. 
    public OpenHtmlUnitWebElement(HtmlUnitDriver parent, HtmlElement element) { 
     super(parent, element); 
    } 

    // this is the method we really want. 
    public static HtmlElement using(HtmlUnitWebElement huwe) { 
     return huwe.element; 
    } 
} 

現在我的(常規)測試看起來是這樣的:

import static org.openqa.selenium.htmlunit.OpenHtmlUnitWebElement.using 

... 

def itemWithContextMenu = driver.findElement(By.id('theId')) 
if (itemWithContextMenu instanceOf HtmlUnitWebElement) { 
    using(itemWithContextMenu).rightClick() 
    def contextMenu = driver.findElement(By.id('jqContextMenu')) 
    assert ... 
} 
5

目前有在webdriver的沒有右鍵的方法,有打開它的增強請求 - http://code.google.com/p/selenium/issues/detail?id=161

現在你可以使用鍵盤快捷鍵Shift + F10模擬元素上點右鍵:

WebElement element = driver.findElement(....); 
element.sendKeys(Keys.chord(Keys.SHIFT, Keys.F10)); 
+0

感謝ZloiAdun,但我仍在掙扎。根據我向哪個元素髮送Shift-F10,我可以看到常規的IE上下文菜單,或瀏覽器文件menubutton被選中。我無法讓我的自定義情景菜單出現。 - 約翰。 – John 2010-07-20 23:15:37

+0

也許你應該嘗試通過JavaScript顯示你的菜單?喜歡的東西 ((JavascriptExecutor)驅動程序).executeScript( 「menu.display(...)」); 我不知道你正在使用的菜單的細節,但肯定應該有一些JavaScript函數來顯示它 – 2010-07-21 08:12:49

0

,如果你使用Ruby與水豚,這應該是有用的:

module Capybara 
    module Node 
    class Element 
     def context_click 
     @session.driver.browser.action.context_click(self.native).perform 
     end 
    end 
    end 
end 
相關問題