使用C#在Selenium WebDriver中断言,验证和其他命令

时间:2013-02-06 06:06:04

标签: c# c#-4.0 automated-tests selenium-webdriver

我几乎搜索了与Selenium WebDriver相关的每个论坛/网站,但仍无法找到如何使用C#在Selenium WebDriver中使用断言和验证的解决方案。

这是我的代码,我只想在下面编写的代码中添加一个示例断言。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;


namespace Healthfleet
{
    class Login
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new ChromeDriver(@"D:\Downloads");
            driver.Navigate().GoToUrl("https://test.com/");
            IWebElement username = driver.FindElement(By.Id("username"));
            IWebElement password = driver.FindElement(By.Id("password"));
            username.SendKeys("test@test.test");
            password.SendKeys("test");
            IWebElement loginButton = driver.FindElement(By.Id("Login"));
            loginButton.Click();
        }
    }
}

我需要使用断言来检查用户名是否为test,但我无法找到任何Assert类或方法。

我是否遗漏了一些包含Assert类或任何其他想法的命名空间?

2 个答案:

答案 0 :(得分:2)

NUnit需要对此进行测试。然后你必须添加它的dll,然后添加名称空间为

using NUnit.Framework;

答案 1 :(得分:2)

目前您已经编写了管理浏览器的程序。如果你要从NUnit添加断言 - 它会在失败的情况下抛出异常。

如果要创建测试,则应创建不带static void Main(string[] args)的类,但添加一些标有[Test]的方法。 我建议你特别学习xUnit-systems和NUnit的概念。