错误:当前上下文中不存在名称Assert

4
我编写了一些代码来验证用户能否登录到酒店预订平台,我还添加了一个验证步骤。我在 Assert.IsTrue() 方法中遇到了错误。代码中其他的东西都没问题。
在停留之前,我进行了一些搜索以寻找可能的解决方案。
以下是我在 StackOverflow 上找到的解决方案:Assert Method Error 我已将 Microsoft.VisualStudio.QualityTools.UnitTestFramework 添加到我的项目引用中,并且还添加了 using Microsoft.VisualStudio.TestTools.UnitTesting;using NUnit.Framework; 到我的使用代码部分。我得到了以下错误:

Severity Code Description Project File Line Suppression State Error CS0234 The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) PremierInn_Valid_User_Login c:\users\XXXX\onedrive\documents\visual studio 2015\Projects\PremierInn_Valid_User_Login\PremierInn_Valid_User_Login\Program.cs 5 Active

Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'NUnit' could not be found (are you missing a using directive or an assembly reference?) PremierInn_Valid_User_Login c:\users\XXXX\onedrive\documents\visual studio 2015\Projects\PremierInn_Valid_User_Login\PremierInn_Valid_User_Login\Program.cs 6 Active

删除绑定后,我仍遇到相同的错误 - Microsoft.VisualStudio.TestTools.UnitTesting; & using NUnit.Framework。
最后,以下是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.Threading.Tasks;

namespace PremierInn_Valid_User_Login {
    class Program {
        static void Main(string[] args) {

        //Instantiate Firefox Driver
        var driver = new FirefoxDriver();
        driver.Navigate().GoToUrl("https://secure.premierinn.com/en/mypremierinn/home.action");

        //Enter User Name - My Email Address
        var user = driver.FindElement(By.Id("loginForm.username"));
        user.SendKeys("abcdefg@gmail.com");

        //Enter Password - Account Pasword
        var pass = driver.FindElement(By.Id("loginForm.password"));
        pass.SendKeys("Testing123");

        //Click on Login button
        driver.FindElement(By.Id("loginForm.button")).Click();

        var loggedInHeader = driver.FindElement(By.Id("body-inner"));
        Assert.IsTrue(loggedInHeader.Displayed, "The user was able to successfully login.");

    }
    }
}

感谢你的帮助。
2个回答

2

建议:

  1. 您试图在主程序中编写单元测试,而不是在单元测试项目中进行编写?请创建单元测试项目。查看此处

  2. 您尝试同时使用 NUnit 和 Microsoft Unit 测试框架吗?请选择其中一个,而不要同时选择两个。


1
你可能需要参考NUnit Assembly文档以使用NUnit运行测试。

NUnit快速入门


非常感谢。 - OA345

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接