C#Selenium RC-如何测试jQuery按钮单击?

C#Selenium RC-如何测试jQuery按钮单击?,c#,jquery,testing,selenium,selenium-rc,C#,Jquery,Testing,Selenium,Selenium Rc,在selenium测试中运行jQuery.click()的正确方法是什么?正在单击的元素是超链接 HTML: 硒: [Test] public void TestOutput() { selenium_local = new DefaultSelenium("localhost", 4444, "*firefox", "https://localhost"); selenium_local.Sta

在selenium测试中运行jQuery.click()的正确方法是什么?正在单击的元素是超链接

HTML:

硒:

[Test]
public void TestOutput()
{
    selenium_local = new DefaultSelenium("localhost", 4444, 
                                         "*firefox", "https://localhost");
    selenium_local.Start(); 

    selenium_local.Open("/TestPage");
    selenium_local.WaitForPageToLoad("30000");
    Assert.That(selenium_local.IsElementPresent("id=btnRun"), Is.True);

    selenium_local.Click("id=btnRun");
    Thread.Sleep(5000);

    // also tried without success:
    // selenium_local.FireEvent("id=btnRun","click");
    // selenium_local.Click("xpath=//a[@id='btnRun']");
    // selenium_local.Click("dom=document.getElementById('btnRun')");

    Assert.That(selenium.GetValue("id=output"), Is.EqualTo("hello world"));
}
当我运行测试时,按钮点击不会发生,在第二次
WaitForPageToLoad
之后,测试完成并报告失败(因为没有找到文本hello world,因为按钮点击从未发生过)

(注:我使用selenium与java,因此可能有区别,但我强烈怀疑这一点)

我有时也会遇到类似的问题,但我们在一些地方使用普通的ol'-javascript,在其他地方使用gwt,在其他地方使用mootools(不要问为什么这么多)来处理不同的ajax调用,所以我们的问题可能完全无关。但是,也许我能帮上忙

有时我必须使用
鼠标盖
,然后单击
。有时我甚至不得不做
mouseDown
然后
mouseUp
。或者两者的结合。即使在某些情况下,我们也必须重试单击


(另外,除非单击链接/按钮导致整个页面加载,否则WaitForPageToLoad将导致问题,因为它等待页面加载事件。但是,如果您仅使用JQuery,您应该能够找到一个问题)

一些JavaScript库响应鼠标向上移动事件,表示“单击”。如果是这样,MouseDown()后跟MouseUp()应该可以完成这项工作。

在jQuery代码中,您需要放入$(“#output”).html(“hello world”);抱歉,这只是一个示例,当手动单击按钮click时,它实际上会执行一些操作(加载jqgrid)。但是在测试中,单击甚至没有执行Hanks,我将首先尝试鼠标悬停+单击。我的定位器中哪一个是最好的/或者我如何判断定位器找到了超链接?我已经有自定义的ajaxStart/ajaxStop,(),所以我应该能够修改它来尝试您的计数器解决方案。鼠标悬停+单击没有帮助,但我不知道它是否正在定位元素:我使用了id=btnRun。奇怪。。。IsElementPresent(“btnRun”)是否返回true?另外,这可能太明显了,但要确保页面上没有任何重复的ID,即使它们是隐藏的。您甚至可以尝试不同的语法。比如“css=div#buttons a#btnRun”之类的。我添加了
Assert.That(selenium_local.IsElementPresent(“id=btnRun”),Is.True)并且它没有使断言失败,所以它一定在那里?只有一件物品有这个id,这很奇怪。它不会在点击时抛出异常?我想这是jquery的一个问题,我想我在这方面帮不了什么忙。我会尝试处理jquery代码,看看是否可以让它在做其他事情时工作。
$(document).ready(function () {
    $('#btnRun').click(function () {
       $("#output").html("hello world");
    });
})
[Test]
public void TestOutput()
{
    selenium_local = new DefaultSelenium("localhost", 4444, 
                                         "*firefox", "https://localhost");
    selenium_local.Start(); 

    selenium_local.Open("/TestPage");
    selenium_local.WaitForPageToLoad("30000");
    Assert.That(selenium_local.IsElementPresent("id=btnRun"), Is.True);

    selenium_local.Click("id=btnRun");
    Thread.Sleep(5000);

    // also tried without success:
    // selenium_local.FireEvent("id=btnRun","click");
    // selenium_local.Click("xpath=//a[@id='btnRun']");
    // selenium_local.Click("dom=document.getElementById('btnRun')");

    Assert.That(selenium.GetValue("id=output"), Is.EqualTo("hello world"));
}