mailslurp-examples - csharp-dotnet-core2-selenium

https://github.com/mailslurp/examples

Table of Contents

csharp-dotnet-core2-selenium/csharp-dotnet-core2-selenium.sln


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleService.Tests", "ExampleService.Tests\ExampleService.Tests.csproj", "{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Debug|x64 = Debug|x64
		Debug|x86 = Debug|x86
		Release|Any CPU = Release|Any CPU
		Release|x64 = Release|x64
		Release|x86 = Release|x86
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Debug|x64.ActiveCfg = Debug|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Debug|x64.Build.0 = Debug|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Debug|x86.ActiveCfg = Debug|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Debug|x86.Build.0 = Debug|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Release|Any CPU.Build.0 = Release|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Release|x64.ActiveCfg = Release|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Release|x64.Build.0 = Release|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Release|x86.ActiveCfg = Release|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Release|x86.Build.0 = Release|Any CPU
	EndGlobalSection
EndGlobal

csharp-dotnet-core2-selenium/README.md

# .NetCore 2.1 Nunit Selenium MailSlurp example
Example MailSlurp usage using [MailSlurp NuGet Package](https://www.nuget.org/packages/mailslurp/) `.Net Core 2.1` and `NUnit` and `Selenium`. See [MailSlurp docs](/docs/csharp/) for more.


## Setup
- Install `dotnet-core-2.1` sdk and cli.
- `dotnet restore`
- `dotnet build`

## Run tests
Set `API_KEY=your-api-key` in environment variables then run `dotnet test`.

csharp-dotnet-core2-selenium/Makefile

-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

csharp-dotnet-core2-selenium/ExampleService.Tests/ExampleTest.cs

using System;
using System.Text.RegularExpressions;
using mailslurp.Api;
using mailslurp.Client;
using mailslurp.Model;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

// Example usage for MailSlurp email API plugin
namespace ExampleService.Tests
{
    
    [TestFixture]
    public class ExampleTest
    {
        
        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 static readonly string Password = "test-password";

        private static Inbox _inbox;
        private static Email _email;
        private static String _confirmationCode;
        
        [SetUpFixture]
        public class NunitSetup
        {
            // runs once before any tests
            [OneTimeSetUp]
            public void SetUp()
            {
                // 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
                Assert.NotNull(YourApiKey);
                _mailslurpConfig = new Configuration();
                _mailslurpConfig.ApiKey.Add("x-api-key", YourApiKey);
            }
        
            // runs once after all tests finished
            [OneTimeTearDown]
            public void Dispose()
            {
                // close down the browser
                _webdriver.Quit();
                _webdriver.Dispose();
            }
        }
        
        [Test, Order(1)]
        public void CanLoadPlaygroundAppInBrowser_AndClickSignUp()
        {
            // open the dummy authentication app and assert it is loaded
            _webdriver.Navigate().GoToUrl("https://playground.mailslurp.com");
            Assert.AreEqual("React App", _webdriver.Title);
            
            // can click the signup button
            _webdriver.FindElement(By.CssSelector("[data-test=sign-in-create-account-link]")).Click();
        }
        
        [Test, Order(2)]
        public void CanCreateTestEmail_AndStartSignUp()
        {
            // first create a test email account
            var inboxControllerApi = new InboxControllerApi(_mailslurpConfig);
            _inbox = inboxControllerApi.CreateInbox();
            
            // inbox has a real email address
            var emailAddress = _inbox.EmailAddress;
            
            // next fill out the sign-up form with email address and a password
            _webdriver.FindElement(By.Name("email")).SendKeys(emailAddress);
            _webdriver.FindElement(By.Name("password")).SendKeys(Password);
            
            // submit form
            _webdriver.FindElement(By.CssSelector("[data-test=sign-up-create-account-button]")).Click();
        }

        [Test, Order(3)]
        public void CanReceiveConfirmationEmail()
        {
            // now fetch the email that playground sends us
            var waitForControllerApi = new WaitForControllerApi(_mailslurpConfig);
            _email = waitForControllerApi.WaitForLatestEmail(inboxId: _inbox.Id, timeout: TimeoutMillis, unreadOnly: true);

            // verify the contents
            Assert.IsTrue(_email.Subject.Contains("Please confirm your email address"));
        }

        [Test, Order(4)]
        public void CanExtractConfirmationCode()
        {
            // 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);
            _confirmationCode = match.Groups[1].Value;
            
            Assert.AreEqual(6, _confirmationCode.Length);
        }

        [Test, Order(5)]
        public void CanConfirmUserWithEmailedCode()
        {
            // 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();
        }

        [Test, Order(6)]
        public void CanLoginWithConfirmedUser()
        {
            // 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(_inbox.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
            Assert.IsTrue(_webdriver.FindElement(By.TagName("h1")).Text.Contains("Welcome"));
        }
        
    }
}

csharp-dotnet-core2-selenium/ExampleService.Tests/ExampleService.Tests.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="mailslurp" Version="15.0.2" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    <PackageReference Include="nunit" Version="3.12.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
    <PackageReference Include="Selenium.WebDriver.GeckoDriver" Version="0.26.0.1" />
  </ItemGroup>

</Project>