Last active
March 17, 2022 06:41
-
-
Save GeorgDangl/7f33008d2fffdef4eee932eb2a8dde90 to your computer and use it in GitHub Desktop.
Improving ASP.NET Core End-to-End Tests with Selenium Docker Images, see https://blog.dangl.me/archive/improving-aspnet-core-end-to-end-tests-with-selenium-docker-images/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:0=info@dangl-it.com | |
- 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment