.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 testing

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.

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:

install specflow plugin

Then create a new project using the plugin:

create a project

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.

find 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:

email content

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.

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.