如何在 Web 应用程序中测试上下文菜单功能? [英] How can I test context menu functionality in a web app?

查看:21
本文介绍了如何在 Web 应用程序中测试上下文菜单功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有上下文菜单(右键单击)的 grails 应用程序.上下文菜单是使用 Chris Domigan 的 jquery contextmenu 插件构建的.

I'm playing with a grails app that has a contextmenu (on right-click). The context menu is built using Chris Domigan's jquery contextmenu plugin.

虽然上下文菜单确实有效,但我想要进行自动化测试,但我不知道该怎么做.

While the contextmenus do actually work, I want to have automated tests, and I can't work out how to do it.

  • 我尝试过 Selenium 2.05a(即 Webdriver),但没有 rightClick 方法.
  • 我注意到 HtmlUnit 有一个右键单击方法,但我似乎无法检测到单击之前和之后 DOM 中的任何差异.

推荐答案

虽然我也希望能够在 Internet Explorer 或 Firefox 中实现,但主要用途是 HtmlUnit.很高兴 HtmlUnit HtmlElement 有一个 rightClick() 方法,但不幸的是它是 protected 因此无法从 WebDriver 包装的 HtmlUnitWebElement 访问.

While I'd like to be able to do it in Internet Explorer or Firefox as well, the main usage will be HtmlUnit. It's nice that the HtmlUnit HtmlElement has a rightClick() method, but unfortunately it's protected and so not accessible from the WebDriver wrapped HtmlUnitWebElement.

我编写了一个 hack 以使其可访问,因此现在我可以调用 rightClick(),尽管它仅在使用 HtmlUnit 运行时才有效 - 而不是 IE 或 FF.

I wrote a hack to make it accessible, and so now I can call rightClick(), although it only works if it's running with HtmlUnit - not IE or 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;
    }
}

现在我的(常规)测试如下所示:

Now my (groovy) test looks like this:

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 ...
}

这篇关于如何在 Web 应用程序中测试上下文菜单功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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