❔ integrate an UI test with playwright on a Blazor Server

Can someone help me? I try to use this code to integrate an UI test with playwright on a blazor server. I know how to use playwright, but I am kind of lost when it comes to the integration part.
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using PlaywrightSharp;
using Xunit;

// ReSharper disable once ClassNeverInstantiated.Global
public class WebServerFixture : IAsyncLifetime, IDisposable
{
private readonly IHost host;
private IPlaywright Playwright { get; set; }
public IBrowser Browser { get; private set; }
public string BaseUrl { get; } = $"https://localhost:{GetRandomUnusedPort()}";

public WebServerFixture()
{
host = Program
.CreateHostBuilder(null)
.ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseStartup<Startup>();
webBuilder.UseUrls(BaseUrl);
// optional to set path to static file assets
// webBuilder.UseContentRoot();
})
.ConfigureServices(configure => {
// override any services
})
.Build();
}

public async Task InitializeAsync()
{
Playwright = await PlaywrightSharp.Playwright.CreateAsync();
Browser = await Playwright.Chromium.LaunchAsync();
await host.StartAsync();
}

public async Task DisposeAsync()
{
await host.StopAsync();
host?.Dispose();
Playwright?.Dispose();
}

public void Dispose()
{
host?.Dispose();
Playwright?.Dispose();
}

private static int GetRandomUnusedPort()
{
var listener = new TcpListener(IPAddress.Any, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using PlaywrightSharp;
using Xunit;

// ReSharper disable once ClassNeverInstantiated.Global
public class WebServerFixture : IAsyncLifetime, IDisposable
{
private readonly IHost host;
private IPlaywright Playwright { get; set; }
public IBrowser Browser { get; private set; }
public string BaseUrl { get; } = $"https://localhost:{GetRandomUnusedPort()}";

public WebServerFixture()
{
host = Program
.CreateHostBuilder(null)
.ConfigureWebHostDefaults(webBuilder => {
webBuilder.UseStartup<Startup>();
webBuilder.UseUrls(BaseUrl);
// optional to set path to static file assets
// webBuilder.UseContentRoot();
})
.ConfigureServices(configure => {
// override any services
})
.Build();
}

public async Task InitializeAsync()
{
Playwright = await PlaywrightSharp.Playwright.CreateAsync();
Browser = await Playwright.Chromium.LaunchAsync();
await host.StartAsync();
}

public async Task DisposeAsync()
{
await host.StopAsync();
host?.Dispose();
Playwright?.Dispose();
}

public void Dispose()
{
host?.Dispose();
Playwright?.Dispose();
}

private static int GetRandomUnusedPort()
{
var listener = new TcpListener(IPAddress.Any, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
}
2 Replies
Funkeyfreak
FunkeyfreakOP2y ago
I found this integration, which was using the old program.cs file which looked like this:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
So this code uses the program.cs function „createhostbuilder“, which no longer exists. Someone know how I can fix this part? I really appreciate any help. Is this the solution?
host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(webBuilder =>...
host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(webBuilder =>...
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server