我正在为 ASP.NET Web 应用程序编写 GUI 测试,但 Selenium 似乎无法连接到本地主机。每次我运行测试用例时,它都会加载 chrome 浏览器,但我收到错误“ERR_CONNECTION_REFUSED”。我可以连接到本地主机进行开发,而不是测试。
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
namespace MyApplication.Tests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void ButtonMenuDimensions_Chrome()
{
try
{
String url = "http://localhost:52956";
ChromeDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl(url);
driver.Manage().Window.Maximize();
String actualHeight = driver.FindElement(By.Id("menu")).GetCssValue("height");
Console.WriteLine("Actual Height: " + actualHeight);
String expectedHeight = "450px";
String actualWidth = driver.FindElement(By.Id("menu")).GetCssValue("width");
String expectedWidth = "200px";
Assert.AreEqual(expectedHeight, actualHeight);
Assert.AreEqual(expectedWidth, actualWidth);
driver.Close();
driver.Dispose();
}
catch
{
Console.WriteLine("Error executing test case: Dimensions");
}
}
}
}
当我打开这个测试用例的输出时,我得到了
Error executing test case: Dimensions
最佳答案
我怀疑您正在 iis express 上运行该应用程序,您需要通过让 ASP 网站在 VS 中运行/调试来确保它在测试时正在运行。
如果您点击 URL http://localhost:52956不跑ASP网站还能打吗?
关于c# - Selenium 无法连接到本地主机以测试 ASP.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46622513/