gpt4 book ai didi

selenium - 如何使用 Selenium 启动 ASP.NET Core Web API 站点进行测试

转载 作者:行者123 更新时间:2023-12-04 16:47:12 26 4
gpt4 key购买 nike

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

当我为家庭 Controller 调试测试时,站点没有启动,Selenium 网络驱动程序失败。

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

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

最佳答案

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+。)

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

关于selenium - 如何使用 Selenium 启动 ASP.NET Core Web API 站点进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38835034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com