.NET SpecFlow testing using MailSlurp email accounts and Selenium
How to test .NET authentication and sign-up using real email accounts with MailSlurp and SpecFlow.
Specflow is a great end-to-end behavior driven test framework for CSharp and .NET application. It is used by many developer to test common scenarios in DotNet applications using a BDD Cucumber like syntax. With MailSlurp you can generate test email accounts during tests to test features like user sign-up end-to-end using real email addresses. In this post we will show you how!
View full code examples on GitHub.
Testing a demo app
We can use a simple React App deployed to playground.mailslurp.com for this demonstration. It has a common authentication flow that allows users to sign up with an email and password. After signing up users receive a confirmation code via email. The code can then be submitted to the app to verify the user. The user can then login and see a picture of a happy dog.
Creating a test project
The best way to get started with a Specflow project is to use VisualStudio to scaffold a project. Install the official Specflow plugin like so:
Then create a new project using the plugin:
Choose a runner when prompted: We recommend MSTest for this example.
Add dependencies
Now add Selenium and MailSlurp to your csproj
file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="mailslurp" Version="11.5.20" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Selenium.WebDriver.GeckoDriver" Version="0.26.0.1" />
<PackageReference Include="Selenium.Support" Version="3.141.0" />
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
<PackageReference Include="SpecFlow.Plus.LivingDocPlugin" Version="3.7.141" />
<PackageReference Include="SpecFlow.MsTest" Version="3.7.38" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Drivers" />
<Folder Include="Drivers\" />
<Folder Include="Hooks\" />
</ItemGroup>
</Project>
MailSlurp is free to use but you need to obtain an API Key. Create a free account to get your API Key.
Setup Selenium
Next wee need to fetch a webdriver for use with Selenium. We use make
internally so create a Makefile
like so:
-include ../.env
DRIVER_LOCATION=geckodriver
DRIVER_URL=https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz
$(DRIVER_LOCATION):
curl -s -L "$(DRIVER_URL)" | tar -xz
chmod +x $(DRIVER_LOCATION)
install:
dotnet restore
dotnet build
test: $(DRIVER_LOCATION)
API_KEY=$(API_KEY) DRIVER_PATH=$(PWD) dotnet test
Running make geckodriver
will download a webdriver for Firefox to the local directory. Running API_KEY=mailslurp-api-key make test
will run our tests and pass the driver path to our tests.
Creating Specflow features
Once install we can create features. Features use a Specflow syntax similar to Cucumber BDD. We describe the actions a user may take and the results we expect. An example for our demonstration app might look like this:
Feature: SignUp
Create a MailSlurp email address and sign up for a demo application. Receive a confirmation code by email and verify the account. Login to the web app and see a happy dog.
@signup
Scenario: User sign up and email verification
Given a user visits the demo app
And has a test email address
When the user signs up
Then they receive a confirmation code by email and can verify their account
Each step in the scenario maps to functions that we define next in our definition steps.
Binding definitions
Each feature statement we write needs to be bound to a CSharp step definition. Here is how we do it:
using System;
using System.Text.RegularExpressions;
using FluentAssertions;
using mailslurp.Api;
using mailslurp.Client;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using TechTalk.SpecFlow;
namespace SpecflowSeleniumExample.Steps
{
[Binding]
public sealed class SignUpStepDefinitions
{
private static IWebDriver _webdriver;
private static Configuration _mailslurpConfig;
// get a MailSlurp API Key free at https://app.mailslurp.com
private static readonly string YourApiKey = Environment.GetEnvironmentVariable("API_KEY", EnvironmentVariableTarget.Process);
private static readonly string DriverPath = Environment.GetEnvironmentVariable("DRIVER_PATH", EnvironmentVariableTarget.Process);
private static readonly long TimeoutMillis = 30_000L;
private readonly ScenarioContext _scenarioContext;
private const string Password = "test-password";
public SignUpStepDefinitions(ScenarioContext scenarioContext)
{
_scenarioContext = scenarioContext;
// set up the webdriver for selenium
var timespan = TimeSpan.FromMilliseconds(TimeoutMillis);
var service = string.IsNullOrEmpty(DriverPath)
? FirefoxDriverService.CreateDefaultService()
: FirefoxDriverService.CreateDefaultService(DriverPath);
_webdriver = new FirefoxDriver(service, new FirefoxOptions(), timespan);
_webdriver.Manage().Timeouts().ImplicitWait = timespan;
// configure mailslurp with API Key
YourApiKey.Should().NotBeNull();
_mailslurpConfig = new Configuration();
_mailslurpConfig.ApiKey.Add("x-api-key", YourApiKey);
}
[After]
public void After()
{
_webdriver.Quit();
_webdriver.Dispose();
}
[Given("a user visits the demo app")]
public void GivenAUserVisitsTheDemoApp()
{
_webdriver.Navigate().GoToUrl("https://playground.mailslurp.com");
_webdriver.Title.Should().Contain("React App");
// can click the signup button
_webdriver.FindElement(By.CssSelector("[data-test=sign-in-create-account-link]")).Click();
}
[Given("has a test email address")]
public void GivenHasATestEmailAddress()
{
// first create a test email account
var inboxControllerApi = new InboxControllerApi(_mailslurpConfig);
var inbox = inboxControllerApi.CreateInbox();
// inbox has a real email address
_scenarioContext.Add("emailAddress", inbox.EmailAddress);
_scenarioContext.Add("inboxId", inbox.Id);
}
[When("the user signs up")]
public void WhenTheUserSignsUp()
{
// next fill out the sign-up form with email address and a password
_webdriver.FindElement(By.Name("email")).SendKeys(_scenarioContext.Get<string>("emailAddress"));
_webdriver.FindElement(By.Name("password")).SendKeys(Password);
// submit form
_webdriver.FindElement(By.CssSelector("[data-test=sign-up-create-account-button]")).Click();
}
[Then("they receive a confirmation code by email and can verify their account")]
public void ThenTheyReceiveAConfirmationCodeByEmailAndCanVerifyTheirAccount()
{
var inboxId = _scenarioContext.Get<Guid>("inboxId");
var emailAddress = _scenarioContext.Get<string>("emailAddress");
var waitForControllerApi = new WaitForControllerApi(_mailslurpConfig);
var email = waitForControllerApi.WaitForLatestEmail(inboxId: inboxId, timeout: TimeoutMillis, unreadOnly: true);
// verify the contents
email.Subject.Should().Contain("Please confirm your email address");
// we need to get the confirmation code from the email
var rx = new Regex(@".*verification code is (\d{6}).*", RegexOptions.Compiled);
var match = rx.Match(email.Body);
var confirmationCode = match.Groups[1].Value;
confirmationCode.Length.Should().Be(6);
// fill the confirm user form with the confirmation code we got from the email
_webdriver.FindElement(By.Name("code")).SendKeys(confirmationCode);
_webdriver.FindElement(By.CssSelector("[data-test=confirm-sign-up-confirm-button]")).Click();
// load the main page again
_webdriver.Navigate().GoToUrl("https://playground.mailslurp.com");
// login with email and password (we expect it to work now that we are confirmed)
_webdriver.FindElement(By.Name("username")).SendKeys(emailAddress);
_webdriver.FindElement(By.Name("password")).SendKeys(Password);
_webdriver.FindElement(By.CssSelector("[data-test=sign-in-sign-in-button]")).Click();
// verify that user can see authenticated content
_webdriver.FindElement(By.TagName("h1")).Text.Contains("Welcome").Should().BeTrue();
}
}
}
Notice how each step binding uses an annotation to match a line in our feature file. Let's look at some of the important steps.
Using test email addresses
We can use MailSlurp's InboxControllerApi class to generate real test email accounts during each test. This means our user can have a real email account for each test. We can use the email address to sign up and receive a confirmation code.
[Given("has a test email address")]
public void GivenHasATestEmailAddress()
{
// first create a test email account
var inboxControllerApi = new InboxControllerApi(_mailslurpConfig);
var inbox = inboxControllerApi.CreateInbox();
// inbox has a real email address
_scenarioContext.Add("emailAddress", inbox.EmailAddress);
_scenarioContext.Add("inboxId", inbox.Id);
}
Receiving emails and extracting content
We expect a confirmation code to be sent to our user email address like the following:
Use the WaitForControllerApi class to wait for the latest email in the inbox we created. Then we can use a regex pattern to match the email content for a verification code.
var inboxId = _scenarioContext.Get<Guid>("inboxId");
var emailAddress = _scenarioContext.Get<string>("emailAddress");
// use the waitforcontroller to wait for the email in the inbox
var waitForControllerApi = new WaitForControllerApi(_mailslurpConfig);
var email = waitForControllerApi.WaitForLatestEmail(inboxId: inboxId, timeout: TimeoutMillis, unreadOnly: true);
// verify the contents
email.Subject.Should().Contain("Please confirm your email address");
// we need to get the confirmation code from the email
var rx = new Regex(@".*verification code is (\d{6}).*", RegexOptions.Compiled);
var match = rx.Match(email.Body);
var confirmationCode = match.Groups[1].Value;
confirmationCode.Length.Should().Be(6);
Running Specflow with MSTest
SpecFlow works with many test adapters such as NUnit and xUnit. We will use MSTest to run our tests. We can run tests with API_KEY=mailslurp-key make test
. We should see Selenium automate a browser and perform a user sign up.
Conclusion
Testing .NET user sign up flows using Specflow is easy with MailSlurp. You can create test email accounts for free and test user funciton end to end. Create a free account to get started or see the CSharp developer docs.
Related content
CSharp Email API and SMTP library
Receive email in DotNET Core using C# and MailSlurp
Golang email library for sending and reading emails
Golang Email Library for sending and receiving emails in Go over SMTP or HTTP/S.
Email for testing
Test email accounts for email testing. Alternatives to Mailinator, MailTrap, Mailosaur and more.
How to wait for Selenium to start during Codeception tests
Example tutorial for how to wait until webdriver and Selenium have started during Codeception PHP tests
How to fix the any package by glob error in dotnet
Fix .NET installation on Linux for missing dotnet dependencies.
Disposable email accounts for DotNET core 6
Create real throw-away email addresses for testing and development in .NET core. Follow this guide to get started.
DotNET Core Cake Task Runner (CSharp Makefiles for coverlet code coverage and more)
Create cross platform build scripts for DotNET Core in a way similar to Makefiles.
Create a new .NET MVC project starter (with the CLI)
DotNet starter project MVC generation using the command line interface.
Using Email APIs to send and receive emails
Generate dynamic email addresses on demand with MailSlurp - a modern email API.
Email API for email marketing and more
APIs for email marketing and social campaign testing. Send, receive, validate and test emails in code and online.
How to test an email address
Test email accounts for testing email addresses in code or online. Create fake email accounts for testing.
Mailinator alternative
Alternatives to Mailinator for test email accounts. Create real email addresses using MailSlurp
How to send an email using Powershell (Windows and cross-platform)
Use Send-MailMessage in Windows Powershell to send emails using an SMTP server or MailSlurp's free email API.
How to start selenium in a background process and wait for it to start
Spawn Selenium server process before tests start for easier acceptance testing.
CypressJS Example
Test email sign-up. password verification and more with Cypress JS and MailSlurp.
CypressJS Email Testing
Use real email accounts in CypressJS to test user sign-up, email verification, and more.
Golang mail Library (SMTP)
How to send and receive emails in Go (test email addresses).
Java JVM Examples
Test email sending and receive emails without a mail server.
TestNG Selenium Java Example
Testing user sign up in Java using TestNG and MailSlurp test email accounts
Codeception PHP acceptance testing using real email address APIs
Write acceptance tests in PHP with real email addresses using Codeception and MailSlurp
PHP Email Test Plugins: send and receive email in PHPUnit (example code)
How to send and receive emails in PHPUnit tests.
PyTest Email Testing
Send and receive email in Pytest Python tests.
Java, Selenium
Receive emails in Java test suites using MailSlurp, Junit, and Selenium.
Receive email in PHP: using MailSlurp to send and receive emails
Test email in PHP using real email addresses
Python Robot Framework email test
Python automation email testing Robotframework plugin
Testing authentication using real email addresses in Ruby with Capybara, Cucumber, and Selenium
Cucumber example project using Capybara to test user authentication using real email addresses.
Test applications with real emails using Serenity BDD, JBehave and Selenium
Email acceptance testing with Serenity and MailSlurp. Test applications with real email addresses.
Specflow user sign-up testing with MailSlurp accounts
How to test .NET authentication and sign-up using real email accounts with MailSlurp and SpecFlow.
Jest, Puppeteer
Test email accounts in React with Jest and Puppeteer. Send and receive emails in Javascript.
.NET Selenium C#
Send and receive email in DotNET Nunit tests using Selenium and MailSlurp.
Cucumber, Ruby
Generate test email accounts with Ruby and Cucumber. Test email sign-up, password verification and more.
Webdriver, JS, WDIO
Test email related processes like sign-up and verification using WDIO WebDriver and MailSlurp.
TestCafe end-to-end MFA testing for user sign-up and email verification
End-to-end testing with MailSlurp, NodeJS, and TestCafe.
CSharp Email Tutorial
SMTP mailserver testing and usage in CSharp using
Send email with CSharp using SMTP Client and MailSlurp
Create a custom SMTP client and access MailSlurp inboxes from CSharp/DotNET.
How To Test Emails Before You Send
There are many free tools to test emails before sending. This can help prevent spam warnings and increase deliverability.
Testing OTP password link username and password for 2 factor authentication (2FA)
Testing OTP password link username and password for 2 factor authentication (2FA)
Test email address
Free test email address for testing emails online with web dashboard or REST API.
How to test 2FA OTP login using SMS codes with Playwright
The ultimate guide to testing OAuth one-time-password flows with real SMS MFA. Use Playwright to automate authentication tests with programmable TXT message APIs.
Testing guide
Integration testing with disposable email accounts using CypressJS, Selenium and many other frameworks. Test OTP password login, transactional emails, notifications and more.
Testing email with Cypress test email accounts
Test email accounts for CypressJS. End-to-end testing with real email addresses using MailSlurp Cypress plugin.
Testing Webhooks
How to test HTTP webhooks using MailSlurp test hooks.
CSharp send SMTP email
How to use CSharp SMTP client to send email with MailSlurp mail server
Testing Email with Cypress JS and MailSlurp
Email testing with Cypress JS