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:
Docker Configuration
docker-compose.yml
version: '3.4'
services:
dangl.identity:
image: danglidentity:dev
environment:
- ASPNETCORE_ENVIRONMENT=Production
- ASPNETCORE_URLS=http://+:80
- IdentityServerSettings:UseDevelopmentSigningCredentials=true
- IdentityServerSettings:Authorities:0=localhost
- DanglIconsSettings:DanglIconsApiBaseUrl=http://dangl.icons
- DanglIconsSettings:DanglIconsFrontendBaseUrl=http://localhost:44849
- DanglIconsSettings:DanglIconsApiKey=DockerE2ETestsApiKey
- InitialAdminUserEmails:[email protected]
- InitialAdminPassword=DockerE2EAdminPassword
- AllowUnsecureJwtAuthorities=true
- ConnectionStrings:DefaultConnection=Data Source=sql.data;Initial Catalog=Dangl.Identity;Integrated Security=False;User ID=SA;Password=DockerSqlPass123
depends_on:
- dangl.icons
- sql.data
ports:
- "44848:80"
dangl.icons:
image: dangl.azurecr.io/danglicons:latest
environment:
- ASPNETCORE_ENVIRONMENT=Production
- ASPNETCORE_URLS=http://+:80
- IconAppConfiguration:AppFilesRootPath=/icons/
- IconAppConfiguration:EnforceHttps=false
- IconAppConfiguration:ApiKeys:0=DockerE2ETestsApiKey
ports:
- "44849:80"
expose:
- "80"
sql.data:
image: mcr.microsoft.com/mssql/server:2017-latest
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=DockerSqlPass123
ports:
- "5434:1433"
selenium.standalone:
image: selenium/standalone-chrome:3.141.59
ports:
- "4444:4444"
shm_size: '2gb'
I'm using the selenium/standalone-chrome:3.141.59 image and make sure to assign it enough memory, as per the official recommendation.
Test Setup
E2eTestsBase.cs
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;
namespace Dangl.Identity.E2E.Tests
{
[Collection(nameof(E2eTestsCollection))]
public class E2eTestsBase : IDisposable
{
protected readonly E2eTestsFixture _e2ETestsFixture;
protected readonly RemoteWebDriver _seleniumDriver;
protected readonly string _username;
protected readonly string _email;
protected readonly string _password = "DeveloperTestPassword";
public E2eTestsBase(E2eTestsFixture e2ETestsFixture)
{
_username = Guid.NewGuid().ToString();
_email = $"{_username}@example.com";
_e2ETestsFixture = e2ETestsFixture;
_seleniumDriver = GetAndInitializeWebDriver();
}
private RemoteWebDriver GetAndInitializeWebDriver()
{
if (E2eTestsSettings.UsesSeleniumInDocker)
{
// Use a RemoteWebDriver to connect to Chrome in the Selenium Standalone Docker container
var chromeOptions = GetChromeOptions();
return new RemoteWebDriver(new Uri(E2eTestsSettings.DockerRemoteWebDriverUrl), chromeOptions);
}
else
{
// Using a regular ChromeDriver to connect to a local Chrome instance
var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var chromeOptions = GetChromeOptions();
return new ChromeDriver(assemblyLocation, chromeOptions);
}
}
private ChromeOptions GetChromeOptions()
{
var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--lang=en");
if (!Debugger.IsAttached)
{
chromeOptions.AddArgument("--headless");
chromeOptions.AddArgument("--window-size=1920,1080");
}
return chromeOptions;
}
public void Dispose()
{
_seleniumDriver.Dispose();
}
}
}
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.
Browser Automation
SignupAsync.cs
protected async Task SignupAsync()
{
_seleniumDriver.Url = E2eTestsSettings.BrowserDanglIdentityBaseUrl;
var registerButton = _seleniumDriver.FindElement(By.XPath("//a[.='Register']"));
await registerButton.ClickAndWaitAsync();
var usernameFormField = _seleniumDriver.FindElement(By.XPath("//input[@name='username']"));
var emailFormField = _seleniumDriver.FindElement(By.XPath("//input[@name='email']"));
var passwordFormField = _seleniumDriver.FindElement(By.XPath("//input[@name='password']"));
usernameFormField.SendKeys(_username);
emailFormField.SendKeys(_email);
passwordFormField.SendKeys(_password);
var acceptTosCheckbox = _seleniumDriver.FindElement(By.ClassName("mat-checkbox-inner-container"));
acceptTosCheckbox.Click();
var confirmRegisterButton = _seleniumDriver.FindElement(By.XPath("//button[.='Register' and not(@routerlink)]"));
await confirmRegisterButton.ClickAndWaitAsync();
}
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.
Happy testing!