尝试运行此测试时出现以下错误:
TheNewTest : FailedSelenium.SeleniumException : 在 c:\Projects\WebDriver\trunk\dotnet\src\Selenium.Core\HttpCommandProcessor.cs 中的 Selenium.HttpCommandProcessor.DoCommand(String command, String[] args) 5000 毫秒后超时:第 100 行Class1.cs 中的 ClassLibrary1.NewTest.TheNewTest():第 66 行
这是代码:
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using Selenium;
namespace ClassLibrary1
{
    [TestFixture]
    public class NewTest
    {
        private ISelenium selenium;
        private StringBuilder verificationErrors;
        [SetUp]
        public void SetupTest()
        {
            selenium = new DefaultSelenium("localhost", 4444, "*firefox D:\\Program Files\\Mozilla Firefox\\firefox.exe",
            "http://www.google.com/");
            selenium.Start();
            verificationErrors = new StringBuilder();
        }
        [TearDown]
        public void TeardownTest()
        {
            try
            {
                selenium.Stop();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }
        [Test]
        public void TheNewTest()
        {
            // Open Google search engine.
            selenium.Open("http://www.google.com/");
            // Assert Title of page.
            Assert.AreEqual("Google", selenium.GetTitle());
            // Provide search term as "Selenium OpenQA"
            selenium.Type("q", "Selenium OpenQA");
            // Read the keyed search term and assert it.
            Assert.AreEqual("Selenium OpenQA", selenium.GetValue("q"));
            // Click on Search button.
            selenium.Click("btnG");
            // Wait for page to load.
            selenium.WaitForPageToLoad("5000");
            // Assert that "www.openqa.org" is available in search results.
            Assert.IsTrue(selenium.IsTextPresent("openqa.org"));
            // Assert that page title is - "Selenium OpenQA - Google Search"
            Assert.AreEqual("Selenium OpenQA - Google Search",
                         selenium.GetTitle());
        }
    }
}
我怀疑线路有问题,selenium.WaitForPageToLoad("5000");但我不知道如何解决。
你也可以在这里下载我的代码:http: //dl.dropbox.com/u/20422001/ClassLibrary1.zip。
请帮忙。谢谢!
编辑 1
我必须手动刷新浏览器才能通过该行selenium.WaitForPageToLoad(timeout);。我该如何解决这个问题?
编辑 2
事情开始变得越来越有趣!
我将代码更改为在 Altavista 搜索网站 (www.altavista.com) 上进行搜索,它可以正常工作!不知何故,我不知道为什么它看起来像“selenium.WaitForPageToLoad(timeout);” 不适用于 Google 搜索网站,但适用于 Altavista 搜索网站 只有我吗?我仍然很好奇,想让它也适用于谷歌的网站,请帮忙。
编辑 3:2011 年 8 月 10 日
我现在距离这个问题的最终解决方案还有一步。感谢“stuartf”指出此问题的根本原因是因为 Google 使用 Ajax 调用搜索结果。
现在,对于这个问题,我有一个“便宜的解决方案”,这是我在加载 ajax 页面时为“检测”所做的:
while (seconds < 30)
{
    //Check whether the results page is loaded.
    if (selenium.GetTitle() == "Selenium OpenQA - Google Search")
    {
        break;
    }
    Thread.Sleep(1000);
    seconds++;
}     
Assert.IsTrue(selenium.IsTextPresent("openqa.org"));       
是的,它通过检查搜索结果页面标题来工作,但在我看来它仍然不太可靠。有没有更好的方法来检查ajax页面是否加载?就像是:
if (selenium.isElementPresent("link=ajaxLink"))
问题是,我们如何找到“ajaxLink”?