【问题标题】:Selenium cannot connect to localhost for testing ASP.NETSelenium 无法连接到 localhost 以测试 ASP.NET
【发布时间】:2018-03-19 06:00:24
【问题描述】:

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

【问题讨论】:

    标签: c# asp.net selenium testing localhost


    【解决方案1】:

    我怀疑你是在 iis express 上运行应用程序,你需要通过让 ASP 网站在 VS 中运行/调试来确保它在测试时正在运行。

    如果你在没有运行 ASP 网站的情况下点击 URL http://localhost:52956,你还能点击它吗?

    【讨论】:

    • 非常感谢!我认为测试能够自行启动 IIS 服务器。不幸的是,VS 不允许您在网站运行/调试时运行测试,因此您必须加载两个不同的 Visual Studio 实例,一个用于启动服务器,另一个用于运行测试。
    • 肯定有办法让单元测试运行网站吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2012-02-12
    • 2016-11-05
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多