c# - Selenium 无法连接到本地主机以测试 ASP.NET

标签 c# asp.net selenium testing localhost

我正在为 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/

相关文章:

c# - 此异常处理代码需要修复

c# - MD5 哈希在 C# 和 Objective C 中不相似

c# - int++ 和页面加载问题

java - Selenium Webdriver submit() 与 click()

c# - SqlBulkCopy 不复制任何数据

c# - ASP.NET Web API CORS 不适用于 AngularJS

javascript - 在多个 GridView /表上使用相同的函数

python - 如何在不失去焦点的情况下发送按键进行输入?

python - 导入错误: No module named 'selenium' in Sublime text plugin

c# - 使用 C# 获取目录中带有 *.tif 文件掩码的文件