Funkeyfreak
Explore posts from servers❔ 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;
}
}
4 replies