Updating to .NET 8, updating to IHostBuilder, and running Playwright Tests within NUnit headless or headed on any OS
I've been doing not just Unit Testing for my sites but full on Integration Testing and Browser Automation Testing as early as 2007 with Selenium. Lately, however, I've been using the faster and generally more compatible Playwright. It has one API and can test on Windows, Linux, Mac, locally, in a container (headless), in my CI/CD pipeline, on Azure DevOps, or in GitHub Actions.
For me, it's that last moment of truth to make sure that the site runs completely from end to end.
I can write those Playwright tests in something like TypeScript, and I could launch them with node, but I like running end unit tests and using that test runner and test harness as my jumping off point for my .NET applications. I'm used to right clicking and "run unit tests" or even better, right click and "debug unit tests" in Visual Studio or VS Code. This gets me the benefit of all of the assertions of a full unit testing framework, and all the benefits of using something like Playwright to automate my browser.
In 2018 I was using WebApplicationFactory and some tricky hacks to basically spin up ASP.NET within .NET (at the time) Core 2.1 within the unit tests and then launching Selenium. This was kind of janky and would require to manually start a separate process and manage its life cycle. However, I kept on with this hack for a number of years basically trying to get the Kestrel Web Server to spin up inside of my unit tests.
I've recently upgraded my main site and podcast site to .NET 8. Keep in mind that I've been moving my websites forward from early early versions of .NET to the most recent versions. The blog is happily running on Linux in a container on .NET 8, but its original code started in 2002 on .NET 1.1.
Now that I'm on .NET 8, I scandalously discovered (as my unit tests stopped working) that the rest of the world had moved from IWebHostBuilder to IHostBuilder five version of .NET ago. Gulp. Say what you will, but the backward compatibility is impressive.
As such my code for Program.cs changed from this
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
to this:
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args).
ConfigureWebHostDefaults(WebHostBuilder => WebHostBuilder.UseStartup<Startup>());
Not a major change on the outside but tidies things up on the inside and sets me up with a more flexible generic host for my web app.
My unit tests stopped working because my Kestral Web Server hack was no longer firing up my server.
Here is an example of my goal from a Playwright perspective within a .NET NUnit test.
[Test]
public async Task DoesSearchWork()
{
await Page.GotoAsync(Url);
await Page.Locator("#topbar").GetByRole(AriaRole.Link, new() { Name = "episodes" }).ClickAsync();
await Page.GetByPlaceholder("search and filter").ClickAsync();
await Page.GetByPlaceholder("search and filter").TypeAsync("wife");
const string visibleCards = ".showCard:visible";
var waiting = await Page.WaitForSelectorAsync(visibleCards, new PageWaitForSelectorOptions() { Timeout = 500 });
await Expect(Page.Locator(visibleCards).First).ToBeVisibleAsync();
await Expect(Page.Locator(visibleCards)).ToHaveCountAsync(5);
}
I love this. Nice and clean. Certainly here we are assuming that we have a URL in that first line, which will be localhost something, and then we assume that our web application has started up on its own.
Here is the setup code that starts my new "web application test builder factory," yeah, the name is stupid but it's descriptive. Note the OneTimeSetUp and the OneTimeTearDown. This starts my web app within the context of my TestHost. Note the :0 makes the app find a port which I then, sadly, have to dig out and put into the Url private for use within my Unit Tests. Note that the <Startup> is in fact my Startup class within Startup.cs which hosts my app's pipeline and Configure and ConfigureServices get setup here so routing all works.
private string Url;
private WebApplication? _app = null;
[OneTimeSetUp]
public void Setup()
{
var builder = WebApplicationTestBuilderFactory.CreateBuilder<Startup>();
var startup = new Startup(builder.Environment);
builder.WebHost.ConfigureKestrel(o => o.Listen(IPAddress.Loopback, 0));
startup.ConfigureServices(builder.Services);
_app = builder.Build();
// listen on any local port (hence the 0)
startup.Configure(_app, _app.Configuration);
_app.Start();
//you are kidding me
Url = _app.Services.GetRequiredService<IServer>().Features.GetRequiredFeature<IServerAddressesFeature>().Addresses.Last();
}
[OneTimeTearDown]
public async Task TearDown()
{
await _app.DisposeAsync();
}
So what horrors are buried in WebApplicationTestBuilderFactory? The first bit is bad and we should fix it for .NET 9. The rest is actually every nice, with a hat tip to David Fowler for his help and guidance! This is the magic and the ick in one small helper class.
public class WebApplicationTestBuilderFactory
{
public static WebApplicationBuilder CreateBuilder<T>() where T : class
{
//This ungodly code requires an unused reference to the MvcTesting package that hooks up
// MSBuild to create the manifest file that is read here.
var testLocation = Path.Combine(AppContext.BaseDirectory, "MvcTestingAppManifest.json");
var json = JsonObject.Parse(File.ReadAllText(testLocation));
var asmFullName = typeof(T).Assembly.FullName ?? throw new InvalidOperationException("Assembly Full Name is null");
var contentRootPath = json?[asmFullName]?.GetValue<string>();
//spin up a real live web application inside TestHost.exe
var builder = WebApplication.CreateBuilder(
new WebApplicationOptions()
{
ContentRootPath = contentRootPath,
ApplicationName = asmFullName
});
return builder;
}
}
The first 4 lines are nasty. Because the test runs in the context of a different directory and my website needs to run within the context of its own content root path, I have to force the content root path to be correct and the only way to do that is by getting the apps base directory from a file generated within MSBuild from the (aging) MvcTesting package. The package is not used, but by referencing it it gets into the build and makes that file that I then use to pull out the directory.
If we can get rid of that "hack" and pull the directory from context elsewhere, then this helper function turns into a single line and .NET 9 gets WAY WAY more testable!
Now I can run my Unit Tests AND Playwright Browser Integration Tests across all OS's, headed or headless, in docker or on the metal. The site is updated to .NET 8 and all is right with my code. Well, it runs at least. ;)
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
Great article as always. I can make use of this code in my every day work :):)
Good to have you back.
this weblog on regular basis to obtain updated from most
recent reports.
I'm looking to start my own blog in the near future but I'm
having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design and style seems different then most blogs and I'm looking for something unique.
P.S Sorry for being off-topic but I had to ask!
blogger. I've joined your feed and look forward to seeking more of your great post.
Also, I have shared your web site in my social networks!
Engine Optimization? I'm trying to get my
blog to rank for some targeted keywords but I'm
not seeing very good success. If you know of any please share.
Kudos!
my web-site; Glen
6x6 Vehicles
Truck Nomad
Rat Rod USA
Digital Marketing
SEO & Link Building
Does running a well-established blog like yours require a massive amount work?
I'm brand new to running a blog however I do write in my diary daily.
I'd like to start a blog so I will be able to share my personal experience and views online.
Please let me know if you have any ideas or tips for new aspiring blog
owners. Thankyou!
for? you made blogging look easy. The overall look of your web site is wonderful,
as well as the content!
new internet users, who are wishing in favor of blogging.
no means found any interesting article like yours. It
is lovely worth enough for me. In my view, if all webmasters and bloggers
made excellent content as you did, the net might be much more
useful than ever before.
Anyhow, I'm definitely delighted I found it and I'll be
book-marking and checking back often!
it but, I'd like to shoot you an email. I've got some recommendations for your blog you might be interested in hearing.
Either way, great website and I look forward to seeing it expand over time.
There's a lot of folks that I think would really
appreciate your content. Please let me know.
Thanks
was extremely long) so I guess I'll just sum it up what I wrote and say, I'm thoroughly enjoying your blog.
I as well am an aspiring blog blogger but I'm still
new to everything. Do you have any tips and hints for first-time
blog writers? I'd really appreciate it.
Can I get your affiliate link to your host? I wish my web site loaded
up as quickly as yours lol
of me, as this time i am reading this great informative article here at
my residence.
What might you suggest in regards to your put up that you just made
a few days in the past? Any certain?
which will make the most significant changes. Many thanks for sharing!
for enjoyment, as this this web page conations in fact good
funny data too.
I have a blog based upon on the same topics you discuss and would really like to have you share some stories/information. I know
my viewers would appreciate your work. If you are even remotely interested, feel free to shoot me an email.
for me. Is anyone else having this issue or is it a issue on my end?
I'll check back later and see if the problem still exists.
This is the very first time I frequented your website page and
so far? I surprised with the research you made to create this particular publish incredible.
Excellent process!
here. The sketch is attractive, your authored material stylish.
nonetheless, you command get got an nervousness over that you wish be delivering the following.
unwell unquestionably come more formerly again as exactly the same nearly
a lot often inside case you shield this increase.
Many thanks!
sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She put the shell to her ear and screamed.
There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is completely off topic but I had to
tell someone!
I'm new to the blog world but I'm trying to get started and set up my own. Do you need any html coding knowledge to make
your own blog? Any help would be really appreciated!
you make blogging look easy. The overall look of your website is magnificent, as well
as the content!
about this, like you wrote the ebook in it or something. I think that you can do with a few % to
power the message house a little bit, but other than that,
that is excellent blog. A great read. I'll definitely be back.
am happy that you just shared this helpful information with us.
Please keep us up to date like this. Thank you for sharing.
these details.
Your favorite justification appeared to be on the internet the easiest thing to
take into accout of. I say to you, I definitely get irked whilst other folks think about worries that they plainly do
not recognize about. You managed to hit the nail upon the top
and outlined out the entire thing with no need side effect , other
folks could take a signal. Will likely be back to get more.
Thanks
I like what I see so i am just following you. Look forward to going over your web
page repeatedly.
its really really fastidious post on building up new webpage.
website, how could i subscribe for a blog site? The account aided me a appropriate
deal. I had been tiny bit familiar of this
your broadcast provided vivid transparent idea
across the internet. Disgrace on Google for now not positioning this submit upper!
Come on over and talk over with my site . Thanks =)
I may revisit yet again since i have saved as a favorite
it. Money and freedom is the best way to change, may you be
rich and continue to guide others.
before but after browsing through many of the articles I realized it's new to me.
Anyways, I'm definitely delighted I found it and I'll be
book-marking it and checking back frequently!
with the layout on your weblog. Is this a paid theme or did you modify it yourself?
Anyway keep up the excellent quality writing, it is rare to see a great blog like
this one today.
else could anybody get that type of information in such
an ideal means of writing? I've a presentation next week, and I'm at the search
for such info.
Comments are closed.
And the .NET backward compatibility is very remarkable