时间:2013-05-23 13:06:55

标签: c# firefox selenium webdriver selenium-webdriver

我在Windows 7上使用的是Firefox 21和C#WebDriver 2.33。我很困惑为什么以下测试失败(我只是检查我的配置设置是否正确)。这在其他浏览器中传递。

[Test]
public void FirefoxDriverWorks()
{
    var firefoxDriver = new FirefoxDriver();
    TestGoogleStillExists(firefoxDriver);
    firefoxDriver.Quit();
}

public void TestGoogleStillExists(IWebDriver webDriver)
{
    webDriver.Navigate().GoToUrl("http://www.google.com");
    var title = webDriver.FindElement(By.CssSelector("head title"));
    Assert.That(title.Text, Is.EqualTo("Google"));
}

1 个答案:

答案 0 :(得分:5)

Selenium WebDriver的text函数只会返回页面本身对用户可见的文本。标题文本在技术上在页面上不可见(它显示在浏览器中chrome的标题部分)。

相反,Selenium WebDriver有一个可以使用return the title of a page的方法:

driver.Title;

所以你的代码变成了:

public void TestGoogleStillExists(IWebDriver webDriver)
{
    webDriver.Navigate().GoToUrl("http://www.google.com");
    var title = webDriver.Title;
    Assert.That(title, Is.EqualTo("Google"));
}