首页 > 解决方案 > 测试发现器“NUnit3TestDiscoverer”加载测试时发生异常

问题描述

我构建了一个简单的 NUnit 框架来在 Visual Studio 2015 中运行 Selenium WebDriver C# 测试。

我已经安装了 NUnit v3.12、Selenium WebDriver v3.141、NUnit3TestAdapter v3.17 扩展。

如果我无法下载浏览器驱动程序,为什么会出现此错误:

******------ Discover test started ------
An exception occurred while test discoverer 'NUnit3TestDiscoverer' was loading tests. Exception: Object reference not set to an instance of an object.
========== Discover test finished: 0 found (0:00:03.1151069) ==========******

下面的代码试图导航到网页并登录到应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;


namespace eMPWR_TEST_AUTOMATION
{

    [TestFixture] // This is your Test Suite containing all you below TESTS ([Test])

    public class eMPWR_SmokeTests
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private String baseURL;
        private bool acceptNextAlert = true;

        [SetUp]
        public void SetUpTests()
        {
            driver = new FirefoxDriver();
            baseURL = "https://va--fasaoadev.my.salesforce.com";
            verificationErrors = new StringBuilder();
        }

        [TearDown]
        public void TearDownTests()
        {
            try
            {
                driver.Quit();
            }
            catch (Exception)
            {
                //ignore errors if unable to close the browser
            }

            Assert.AreEqual("", verificationErrors.ToString());
        }

        [TestCase("UserName1", "Password1")] // Using 3 scenarios instead of below variables
        [TestCase("UserName2", "Password2")]
        [TestCase("UserName3", "Password3")]
        public void eMPWR_Login(string UserName, string Password)
        {
            //String UserName = "UserName1";
            //String Password = "Password1";
            String Url = "www.login.Salesforce.com";

            IWebDriver driver = new FirefoxDriver();

            driver.Navigate().GoToUrl(Url);
            driver.FindElement(By.Id("UserName")).SendKeys(UserName);
            driver.FindElement(By.Id("Password")).SendKeys(Password);
            driver.FindElement(By.Id("Login")).Click();

            String ExpectedData = "Salesforce";
            String ActualData = "Sales Force";

            Assert.That(ExpectedData, Is.EqualTo(ActualData)); // from NUnit Framework

            driver.Close();
        }

        [Test]
        public void eMPWR_SearchSSN()
        {
            string URL = "https://va--fasaoadev.my.salesforce.com";
            string FN = "John";
            string LN = "Doe";
            string Email = "John.Doe@aol.com";
            string PWD = "F00tB@11";

            IWebDriver driver = new FirefoxDriver();

            driver.Navigate().GoToUrl(URL);
            driver.FindElement(By.CssSelector("input.submitbutn")).Click();
            driver.FindElement(By.Id("ctl00_MainContent.txtFirstName")).Clear();
            driver.FindElement(By.Id("ctl00_MainContent.txtFirstName")).SendKeys(FN);
            driver.FindElement(By.Id("ctl00_MainContent.txtLastName")).Clear();
            driver.FindElement(By.Id("ctl00_MainContent.txtLastName")).SendKeys(LN);
            driver.FindElement(By.Id("ctl00_MainContent.txtEmail")).Clear();
            driver.FindElement(By.Id("ctl00_MainContent.txtEmail")).SendKeys(Email);
            driver.FindElement(By.Id("ctl00_MainContent.txtVerifyPassword")).Clear();
            driver.FindElement(By.Id("ctl00_MainContent.txtVerifyPassword")).SendKeys(PWD);
            driver.FindElement(By.CssSelector("input.submitbutn")).Click();

            Assert.AreEqual("Login Successful", 
            driver.FindElement(By.Id("ctl00_MainContentlblTransactionResult")).Text);
        }
    }
}

标签: c#selenium-webdrivervisual-studio-2015nunit-3.0

解决方案


取自https://www.stevefenton.co.uk/2018/02/nunit-exception-occurred-test-discoverer-loading-tests/

  1. 关闭 Visual Studio。
  2. 删除以下位置的缓存文件夹:C:\Users\Your.Name\AppData\Local\Temp\VisualStudioTestExplorerExtensions\NUnit3TestAdapter.version
  3. 重新打开 Visual Studio。
  4. 构建您的项目。测试现在应该显示在测试资源管理器窗口中。

它对我有用!


推荐阅读