Giter Club home page Giter Club logo

playwright-mailhog's Introduction

Playwright MailHog Integration

This package provides utility fixtures for Playwright that provide an interface to easily make requests to a MailHog instance. This allows you to easily test transactional email in your applications as well as 2-factor authentication via email in authentication systems.

Installation

Use your favourite package manager to add this package to your project.

yarn add playwright-mailhog
# or
npm install --save playwright-mailhog

Upon installation extend your test object. Read more about it in Playwright docs.

// ./my-test.ts
import { test as base, PlaywrightTestArgs } from "@playwright/test";
import { MailHogFixtures, makeMailHogFixtures } from "playwright-mailhog";

// make sure to include the `MailHogFixtures` type
export const test = base.extend<MailHogFixtures>({
  // here you would also specify your own fixtures
  ...makeMailHogFixtures({
    mailhogUrl: process.env.MAILHOG_API_URL,
    basicAuthCredentials: process.env.MAILHOG_BASIC_AUTH_CREDENTIALS,
  }),
});

Usage

To use the fixtures make sure to be using the extended test object.

// ./mail.spec.ts
import { test } from "./my-test";

test("send mail", async ({ page, mhGetEmailsByRecipient }) => {
  await page.locator(".email-body").fill("TEST MESSAGE");
  await page.locator(".email-recipient").fill("[email protected]");
  await page.locator(".send-email").click();

  const messages = await mhGetEmailsByRecipient("[email protected]");

  await test.expect(messages.items[0].Raw.Data).toBe("TEST MESSAGE");
});

All MailHog fixtures are prefixed with mh. The library is typed and provides proper types for provided responses. See MailHogFixtures type for a complete list of fixtures.

At the moment only /v2/emails and /v2/search have their shortcuts implemented as fixtures. In order to make requests to other endpoints use mhApiRequest.

// ./mail.spec.ts
import { test } from "./my-test";

test("retries if fails", async ({
  page,
  mhGetEmailsByRecipient,
  mhApiRequest,
}) => {
  // enable Jim by calling `mhApiRequest`
  await mhApiRequest("post", "/v2/jim", { headers: "X-Some-header": "some value" });

  await page.locator(".email-body").fill("TEST MESSAGE");
  await page.locator(".email-recipient").fill("[email protected]");
  await page.locator(".send-email").click();

  const messages = await mhGetEmailsByRecipient("[email protected]");

  await test.expect(messages.items[0].Raw.Data).toBe("TEST MESSAGE");
});

If you want to make this package better by including more fixtures feel free to open a Pull Request with new shortcuts implemented.

Contribution

Feel free to send any suggestions in GitHub issues: comment or vote on an existing issue, open a new one or create a Pull Request with your feature.

License

MIT

playwright-mailhog's People

Contributors

yakovlev-alexey avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

playwright-mailhog's Issues

TypeScript errors when extending with Worker Fixtures

A section of the error

Types of property 'mhApiUrl' are incompatible.
      Type 'MailHogTestFixture<string>' is not assignable to type 'TestFixtureValue<string, TestFixtures & PomFixtures & MailHogFixtures & KcAdminFixtures & { assertUserExists: (credentials: Credentials) => Promise<...>; assertUserDoesNotExist: (username: string) => Promise<...>; } & ... 4 more ... & PlaywrightWorkerOptions> | [...]'.
        Type 'MailHogTestFixture<string>' is not assignable to type 'TestFixture<string, TestFixtures & PomFixtures & MailHogFixtures & KcAdminFixtures & { assertUserExists: (credentials: Credentials) => Promise<...>; assertUserDoesNotExist: (username: string) => Promise<...>; } & ... 4 more ... & PlaywrightWorkerOptions>'.
          Types of parameters 'args' and 'args' are incompatible.
            Type 'TestFixtures & PomFixtures & MailHogFixtures & KcAdminFixtures & { assertUserExists: (credentials: Credentials) => Promise<...>; assertUserDoesNotExist: (username: string) => Promise<...>; } & ... 4 more ... & PlaywrightWorkerOptions' is not assignable to type 'PlaywrightTestArgs & Partial<MailHogFixtures>'.
              Type 'TestFixtures & PomFixtures & MailHogFixtures & KcAdminFixtures & { assertUserExists: (credentials: Credentials) => Promise<...>; assertUserDoesNotExist: (username: string) => Promise<...>; } & ... 4 more ... & PlaywrightWorkerOptions' is not assignable to type 'PlaywrightTestArgs'.
                The types of 'context.exposeBinding' are incompatible between these types.

dist directory missing

See also: https://www.npmjs.com/package/playwright-mailhog/v/1.1.0?activeTab=code

# npm install --save playwright-mailhog

added 1 package, and audited 5 packages in 2s

found 0 vulnerabilities
# find node_modules/playwright-mailhog/
node_modules/playwright-mailhog/
node_modules/playwright-mailhog/LICENSE
node_modules/playwright-mailhog/README.md
node_modules/playwright-mailhog/package.json
# npm --version
9.5.1
# fgrep -i mailhog package.json 
    "playwright-mailhog": "^1.1.0"
# 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.