Skip to content

Commit

Permalink
example-screenshpts
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmahoney committed Apr 15, 2024
1 parent c2dedcf commit 42fb6f0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .build/shortcodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ async function getFileTree(path: string): Promise<string> {
commentEnd: "//</gen>",
highlight: "javascript",
},
{
paths: await files(
"/javascript-email-screenshot/*.spec.mjs"
),
commentStart: "//<gen>",
commentEnd: "//</gen>",
highlight: "javascript",
},
{
paths: await files(
"/javascript-cypress-js/**/*.js",
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
node_modules
shortcodes
**/vendor
Expand Down
3 changes: 2 additions & 1 deletion javascript-email-screenshot/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.png
*.png
!email-screenshot.expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 17 additions & 8 deletions javascript-email-screenshot/email-screenshot.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {test} from 'vitest'
import {test, expect} from 'vitest'
import {writeFile} from 'fs/promises';
import {dirname, join} from 'path';
import {fileURLToPath} from 'url';
import Jimp from "jimp";

const __dirname = dirname(fileURLToPath(import.meta.url));
test('can take screenshot of an email', async () => {
//<gen>email_screenshot_setup
const MailSlurp = await import('mailslurp-client').then(m => m.default)
const mailslurp = new MailSlurp({apiKey: process.env.API_KEY})
//</gen>
//<gen>email_screenshot_create_email
// create an inbox
const inbox = await mailslurp.inboxController.createInboxWithOptions({
createInboxDto: {
Expand All @@ -19,21 +23,26 @@ test('can take screenshot of an email', async () => {
subject: 'Welcome to my newsletter',
body: 'Hello Jack. This email is a test.'
})
//</gen>
//<gen>email_screenshot_wait_screenshot
// wait for email to arrive
const email = await mailslurp.waitForLatestEmail(inbox.id, 120_000, true)
// take screenshot of the email
// get a screenshot of the email
const screenshot = await mailslurp.emailController.getEmailScreenshotAsBase64({
emailId: email.id,
getEmailScreenshotOptions: {
height: 480,
width: 320
}
})
const img = Buffer.from(screenshot.base64EncodedImage, 'base64')
const path = join(__dirname, 'email-screenshot.png')
//</gen>
//<gen>email_screenshot_load_image_buffer
const imgBuffer = Buffer.from(screenshot.base64EncodedImage, 'base64')
//</gen>
//<gen>email_screenshot_diff
const pathExpected = join(__dirname, 'email-screenshot.expected.png')
await writeFile(path, img)
await writeFile(pathExpected, img)
// var diff = Jimp.diff(image1, image2, threshold); // threshold ranges 0-1 (default: 0.1)

const threshold = 0.2
const diff = Jimp.diff(await Jimp.read(imgBuffer), await Jimp.read(pathExpected), threshold);
expect(diff.percent).toBeLessThan(threshold)
//</gen>
}, 120_000)

0 comments on commit 42fb6f0

Please sign in to comment.