繁体   English   中英

如何让我的 Selenium 测试在 Visual Studio 中运行?

[英]How can I get my Selenium test to run in Visual Studio?

我正在尝试开发脚本来测试我编写的应用程序。 我创建的第一个脚本是一个简单的登录、验证几件事并注销的脚本。 我可以让它出现在测试资源管理器中,但由于某种原因它不会运行。 我检查了 output 日志并得到了几个错误,但不确定它们指向哪里。

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Threading;

namespace UnitTestProject1
{

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl(@"http://localhost/github/restaurant/login.php"); //nav to page
            Thread.Sleep(2000); // Implicit wait to ensure page is loaded
            Assert.IsTrue(driver.FindElement(By.XPath("//*[@id='username']")).Displayed); //look for username field
            IWebElement userField = driver.FindElement(By.XPath("//*[@id='username']")); //mapping userfield to username
            userField.SendKeys("admin"); //typing admin into userfield
            Assert.IsTrue(driver.FindElement(By.XPath("//*[@id='password']")).Displayed); //looking for password field
            IWebElement pwField = driver.FindElement(By.XPath("//*[@id='password']")); //mapping pwField to password
            pwField.SendKeys("password"); //typing password into pwField
            driver.FindElement(By.XPath("//*[@id='login']")).Click(); //clicking login btn
            Thread.Sleep(1000); // Implicit wait to ensure page is loaded
            Assert.IsTrue(driver.FindElement(By.XPath("//*[@id='logout']")).Displayed); //look for logout button
            driver.FindElement(By.XPath("//*[@id='logout']")).Click(); //clicking logout button
            Thread.Sleep(1000); //implicit wait
        }
    }

}

那是我的脚本,我得到的错误如下。 有没有人知道脚本的问题是什么?

System.ArgumentException: Illegal characters in path.
   at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
   at System.IO.Path.Combine(String path1, String path2)
   at     Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.DotnetHostHelper.TryGetExecutablePath(String executableBaseName, String& executablePath)
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.DotnetHostHelper.GetDotnetPath()
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Hosting.DotnetTestHostManager.GetTestHostProcessStartInfo(IEnumerable`1 sources, IDictionary`2 environmentVariables, TestRunnerConnectionInfo connectionInfo)
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable`1 sources, String runSettings)
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler)
[6/15/2020 2:36:11.594 PM] ========== Discovery aborted: 0 tests found (0:00:00.0241822) ==========

从“TEMP”文件夹中删除 specflow 文件,重建解决方案并重试。 看看结果如何。

这似乎是测试运行程序无法考虑在您的程序集中执行测试的问题。 这可能是由于 Visual Studio 未使用测试适配器,尝试通过 Nuget 包显式包含它。

在此处输入图像描述

或者,为了证明您的代码没有缺陷,您可以使用如下所示的 mstest 控制台执行测试:

在此处输入图像描述

您需要使用句柄 /testcontainer:"{path to test assembly}" 指定测试容器(将.dll 指向您的项目,其中包含位于 bin 目录下的测试)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM