Last year, I've blogged about automating end-to-end (E2E) tests with an ASP.NET Core application. Quickly summarized, you're setting up your environment locally via Docker and then use Seleniums ChromeDriver to automate UI tests for your web application. This worked well, but had one big drawback - when you're executing the tests, you need to have Chrome installed on the build agent or on your local machine. While most who do web development probably have Chrome available, it's a bit of a hassle with testing - you need to make sure that your Selenium version matches the current Chrome version.
Luckily, Selenium provides Docker containers with an embedded Chrome and their own RemoteWebDriver. This means to be able to access an automated browser via Selenium, you just have to spin up a container and connect to it. That's pretty great and makes versioning really easy!
Building on the previous post, you just have to follow these steps:
I'm using the selenium/standalone-chrome:3.141.59 image and make sure to assign it enough memory, as per the official recommendation.
In the test project, I simply check if the containerized Selenium driver should be used and return a RemoteWebDriver instead of a ChromeDriver. For context, I do sometimes let the tests run with my local version of Chrome and have the debugger attached, so I'm not using the headless mode there but instead can track the actions.
Finally, here's a simple method that automates the sign up process. You'll note that writing E2E tests is quite cumbersome, because you're really just describing step-by-step what's happening. But you'll be able to test your actual project in real world conditions and have it run automatically, which is pretty much worth it!
You're now able to fully test your app in any environment, all you need is Docker.