mailslurp-examples - javascript-cypress-js-open-email

https://github.com/mailslurp/examples

Table of Contents

javascript-cypress-js-open-email/package.json

{
  "name": "cypress-js-open-email",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "cypress run"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "cypress": "^13.7.1",
    "mailslurp-client": "^15.18.2"
  }
}

javascript-cypress-js-open-email/cypress.config.js

const { defineConfig } = require("cypress");

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      // implement node event listeners here
    },
  },
});

javascript-cypress-js-open-email/Makefile

-include ../.env

node_modules:
	npm install

docker:
	docker build -t cypress --build-arg API_KEY=$(API_KEY) . && docker run cypress

dev: node_modules
	CYPRESS_API_KEY=$(API_KEY) npm run dev

test: node_modules
	CYPRESS_API_KEY=$(API_KEY) npm run test

javascript-cypress-js-open-email/cypress/support/e2e.js

// ***********************************************************
// This example support/e2e.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')

javascript-cypress-js-open-email/cypress/support/commands.js

// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })

javascript-cypress-js-open-email/cypress/fixtures/example.json

{
  "name": "Using fixtures to represent data",
  "email": "hello@cypress.io",
  "body": "Fixtures are a great way to mock data for responses to routes"
}

javascript-cypress-js-open-email/cypress/e2e/open-email.cy.js

import { MailSlurp } from 'mailslurp-client';

describe('open an email', () => {
  it('can open emails', () => {
    cy.then(() => {
      cy.log('Configure mailslurp')
      const mailslurp = new MailSlurp({ apiKey: Cypress.env('API_KEY') })
      cy.wrap(mailslurp).as('mailslurp')
    });
    
    cy.then(function () {
      cy.log('Create an inbox')
      return this.mailslurp.inboxController.createInboxWithDefaults()
    }).then(inbox => {
      cy.wrap(inbox.id).as('inboxId')
      cy.wrap(inbox.emailAddress).as('emailAddress')
    });

    cy.then(function () {
      cy.log('Send email')
      return this.mailslurp.sendEmail(this.inboxId, {
        to: [this.emailAddress],
        subject: 'Test email',
        body: 'This is a test'
      })
    });

    cy.then(function () {
      cy.log('Wait for email')
      return this.mailslurp.waitForLatestEmail(this.inboxId, 120_000, true)
    }).then(email => {
      expect(email.subject).to.contain('Test email')
      cy.wrap(email.id).as('emailId')
    });

    cy.then(function () {
      return this.mailslurp.emailController.getEmailPreviewURLs({ emailId: this.emailId })
    }).then(previewUrls => {
      cy.visit(previewUrls.plainHtmlBodyUrl)
      cy.get('body').contains('This is a test')
    })
  })
})