Skip to content

Extends Verify to enable snapshotting of emails via EmailPreviewServices.

License

Notifications You must be signed in to change notification settings

VerifyTests/Verify.EmailPreviewServices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify.EmailPreviewServices

Discussions Build status NuGet Status

Extends Verify to enable snapshotting of emails via EmailPreviewServices.

The purpose of this project is provide faster feedback when using code to generate html emails.

EmailPreviewServices is a paid service, an account is required to get an API key.

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

NuGet

Setup

Call VerifyEmailPreviewServices.Initialize at test startup.

[ModuleInitializer]
public static void Init() =>
    VerifyEmailPreviewServices.Initialize();

snippet source | anchor

The default behavior is to use an environment variable named EmailPreviewServicesApiKey for the API key.

An explicit API key can be used:

[ModuleInitializer]
public static void Init() =>
    VerifyEmailPreviewServices.Initialize("ApiKey");

snippet source | anchor

Sample html

Assume the code under test produces the following html email.

static string html =
    """
    <!DOCTYPE html>
    <html>
    <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>Test Email</title>
    </head>
    <body style="font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 20px;">
       <div style="max-width: 600px; margin: 0 auto; background-color: white; padding: 20px; border-radius: 10px;">
           <h1 style="color: #333;">Welcome to Our Service!</h1>
           <p style="color: #666; line-height: 1.6;">
               This is a test email to demonstrate the email preview functionality.
           </p>
           <a href="https://"
              style="display: inline-block; padding: 10px 20px; background-color: #007bff;
                     color: white; text-decoration: none; border-radius: 5px; margin-top: 10px;">
               Get Started
           </a>
       </div>
    </body>
    </html>
    """;

snippet source | anchor

Generating previews

[Test]
[Explicit]
public async Task GeneratePreview()
{
    var preview = new EmailPreview
    {
        Html = html,
        Devices =
            [
                Device.OutlookWebDarkModeChrome,
                Device.iPhone13
            ]
    };
    await Verify(preview);
}

snippet source | anchor

Result

For OutlookWebDarkModeChrome:

For Android9:

Performance

Generating previews takes in the range of tens of seconds. The time take can vary based on the number and type of email devices selected. For example:

  • Generating a a single device (GmailFirefox) takes ~20sec.
  • Generating previews for 5 devices (OutlookWebChrome, Outlook2019, Outlook2016, iPhone13, and GmailFirefox) takes ~25sec.

While this may seem slow compared to other tests, it is orders of magnitude faster than having to test previews manually. It is also less error prone than manual testing

Execution will timeout after 6min and throw an exception.

Splitting individual devices, or groups of devices, over multiple tests can be used to have more control of when previews are generated and achieve faster feedback.

When to run tests

Tests that execute in the 10s of seconds range can significantly slow down a test run. As such it is recommended that email preview tests are configured to be explicit in Debug mode.

[Test]
#if DEBUG
[Explicit]
#endif
public async Task GeneratePreview()
{
    var preview = new EmailPreview
    {
        Html = html,
        Devices = [Device.Outlook2019]
    };
    await Verify(preview);
}

This way developer can opt in to manually run specific previews, and on the build server previews test will allways be executed.

Icon

Preview pane designed by M. Oki Orlando from The Noun Project.

About

Extends Verify to enable snapshotting of emails via EmailPreviewServices.

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Contributors 2

  •  
  •  

Languages