【问题标题】:How to start ASP.NET Core Web API site for testing using Selenium如何启动 ASP.NET Core Web API 站点以使用 Selenium 进行测试
【发布时间】:2016-12-14 13:44:41
【问题描述】:

我正在尝试使用 selenium 为我的 ASP.NET Core Web Api 解决方案编写自动化 UI 测试。 我的主要问题 - 是否有任何解决方案可以为 Selenium Web 驱动程序运行我的网站?

当我对家庭控制器进行调试测试时,站点没有启动,并且 Selenium Web 驱动程序失败。

我找到了一个解决方案 - 是 run IIS Express in TestInitialize,但也许还有更多解决方案存在?

附:刚刚面临的另一个解决方案是run dotnet.exe。对我来说似乎更清楚。

【问题讨论】:

标签: selenium asp.net-core asp.net-core-1.0 ui-testing


【解决方案1】:

dotnet run 可以很好地启动应用程序,但是当我使用 Selenium 进行测试时,我希望对某些服务有更多的控制权(例如跳过启动后台服务、bypass authentication 等)。

这是我正在使用的一个类。我试图坚持接近WebApplicationFactory 模式。

// tests/MyApp.Tests/SeleniumServerFactory.cs

using System;
using System.IO;
using System.Linq;

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Hosting;

namespace MyApp.Tests
{
    /// <summary>
    /// Fixture class to start a server instance to do browser/UI tests.
    /// </summary>
    public class SeleniumServerFactory<TStartup> : IDisposable
        where TStartup : class
    {
        private readonly IHost _host;

        public Uri BaseAddress { get; }

        public SeleniumServerFactory()
        {
            IHostBuilder builder = Host.CreateDefaultBuilder()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    // Copied from Program.cs
                    // ...
                    webBuilder.UseStartup<TStartup>();

                    // Assumes your project is in src/MyApp.
                    // This can be updated based on your solution layout.
                    var relativePathToProject = Path.Join("src", "MyApp");
                    webBuilder.UseSolutionRelativeContentRoot(relativePathToProject);

                    // Use a dynamic port to prevent overlapping tests from breaking
                    webBuilder.UseUrls("http://127.0.0.1:0");
                });

            // Apply your configuration overrides below to the builder.
            ConfigureServices(builder);

            // Start the host in the background.
            // Shut it down in the Dispose method below.
            _host = builder.Build();
            _host.Start();

            // Store base address so that tests can pass it to the browser.
            BaseAddress = new Uri(_host.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>().Addresses.First());
        }

        public void ConfigureServices(IWebHostBuilder builder)
        {
            // Add your authentication overrides, etc. here ...
        }

        public void Dispose()
        {
            _host.Dispose();
        }
    }

}

然后我将这个类用作我的 xUnit 测试中的夹具:

// tests/MyApp.Tests/Browser/SeleniumTests.cs

using System;

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using Xunit;

namespace MyApp.Tests
{
    public class SeleniumTests
        : IClassFixture<SeleniumServerFactory<Startup>>
    {
        private readonly Uri _baseAddress;

        public SeleniumTests(SeleniumServerFactory<Startup> factory)
        {
            _baseAddress = factory.BaseAddress;
        }

        [Fact]
        public void TitleShouldStartWithAppName()
        {
            using browser = new FirefoxDriver();
            browser.Navigate().GoToUrl(_baseAddress);
            Assert.StartsWith("MyApp - ", browser.Title);
        }
    }
}

(我大致基于 Scott Hanselman's article。但是,这仅涵盖 ASP.NET Core 2.1,并且对 TestServer 类进行了一些更改,使其不再适用于 ASP.NET Core 3.0+,所以我更新了我的示例以使用 ASP.NET Core 3.0+。)

可能有一些更好的抽象可以做,但到目前为止这对我有用。要深入了解,请参阅:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2020-03-31
    • 2021-04-28
    • 2018-11-20
    • 1970-01-01
    • 2023-02-24
    • 2010-12-01
    相关资源
    最近更新 更多