https://github.com/mailslurp/examples
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31129.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpecflowSeleniumExample", "SpecflowSeleniumExample.csproj", "{C5AEE6A2-545A-403F-90BB-D063BB6349BA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C5AEE6A2-545A-403F-90BB-D063BB6349BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C5AEE6A2-545A-403F-90BB-D063BB6349BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C5AEE6A2-545A-403F-90BB-D063BB6349BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C5AEE6A2-545A-403F-90BB-D063BB6349BA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CF2982D0-E831-43D2-968E-380F69B4D3E3}
EndGlobalSection
EndGlobal
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="mailslurp" Version="15.0.2" />
<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>
# CSharp .NET 5.0 SpecFlow Email Test example
Example for testing a demonstration app using MailSlurp, SpecFlow, MsTest, and Selenium.
## Run tests
`dotnet restore`
`dotnet build`
`API_KEY=your-mailslurp-key dotnet test`
-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
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();
}
}
}
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by SpecFlow (https://www.specflow.org/).
// SpecFlow Version:3.7.0.0
// SpecFlow Generator Version:3.7.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
#region Designer generated code
#pragma warning disable
namespace SpecflowSeleniumExample.Features
{
using TechTalk.SpecFlow;
using System;
using System.Linq;
[System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "3.7.0.0")]
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute()]
public partial class SignUpFeature
{
private static TechTalk.SpecFlow.ITestRunner testRunner;
private Microsoft.VisualStudio.TestTools.UnitTesting.TestContext _testContext;
private string[] _featureTags = ((string[])(null));
#line 1 "SignUp.feature"
#line hidden
public virtual Microsoft.VisualStudio.TestTools.UnitTesting.TestContext TestContext
{
get
{
return this._testContext;
}
set
{
this._testContext = value;
}
}
[Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute()]
public static void FeatureSetup(Microsoft.VisualStudio.TestTools.UnitTesting.TestContext testContext)
{
testRunner = TechTalk.SpecFlow.TestRunnerManager.GetTestRunner();
TechTalk.SpecFlow.FeatureInfo featureInfo = new TechTalk.SpecFlow.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "Features", "SignUp", "Create a MailSlurp email address and sign up for a demo application. Receive a co" +
"nfirmation code by email and verify the account. Login to the web app and see a " +
"happy dog.", ProgrammingLanguage.CSharp, ((string[])(null)));
testRunner.OnFeatureStart(featureInfo);
}
[Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute()]
public static void FeatureTearDown()
{
testRunner.OnFeatureEnd();
testRunner = null;
}
[Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute()]
public virtual void TestInitialize()
{
if (((testRunner.FeatureContext != null)
&& (testRunner.FeatureContext.FeatureInfo.Title != "SignUp")))
{
global::SpecflowSeleniumExample.Features.SignUpFeature.FeatureSetup(null);
}
}
[Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute()]
public virtual void TestTearDown()
{
testRunner.OnScenarioEnd();
}
public virtual void ScenarioInitialize(TechTalk.SpecFlow.ScenarioInfo scenarioInfo)
{
testRunner.OnScenarioInitialize(scenarioInfo);
testRunner.ScenarioContext.ScenarioContainer.RegisterInstanceAs<Microsoft.VisualStudio.TestTools.UnitTesting.TestContext>(_testContext);
}
public virtual void ScenarioStart()
{
testRunner.OnScenarioStart();
}
public virtual void ScenarioCleanup()
{
testRunner.CollectScenarioErrors();
}
[Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
[Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute("User sign up and email verification")]
[Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute("FeatureTitle", "SignUp")]
[Microsoft.VisualStudio.TestTools.UnitTesting.TestCategoryAttribute("signup")]
public virtual void UserSignUpAndEmailVerification()
{
string[] tagsOfScenario = new string[] {
"signup"};
System.Collections.Specialized.OrderedDictionary argumentsOfScenario = new System.Collections.Specialized.OrderedDictionary();
TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("User sign up and email verification", null, tagsOfScenario, argumentsOfScenario, this._featureTags);
#line 5
this.ScenarioInitialize(scenarioInfo);
#line hidden
bool isScenarioIgnored = default(bool);
bool isFeatureIgnored = default(bool);
if ((tagsOfScenario != null))
{
isScenarioIgnored = tagsOfScenario.Where(__entry => __entry != null).Where(__entry => String.Equals(__entry, "ignore", StringComparison.CurrentCultureIgnoreCase)).Any();
}
if ((this._featureTags != null))
{
isFeatureIgnored = this._featureTags.Where(__entry => __entry != null).Where(__entry => String.Equals(__entry, "ignore", StringComparison.CurrentCultureIgnoreCase)).Any();
}
if ((isScenarioIgnored || isFeatureIgnored))
{
testRunner.SkipScenario();
}
else
{
this.ScenarioStart();
#line 6
testRunner.Given("a user visits the demo app", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Given ");
#line hidden
#line 7
testRunner.And("has a test email address", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "And ");
#line hidden
#line 8
testRunner.When("the user signs up", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "When ");
#line hidden
#line 9
testRunner.Then("they receive a confirmation code by email and can verify their account", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Then ");
#line hidden
}
this.ScenarioCleanup();
}
}
}
#pragma warning restore
#endregion
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