@bhavinkumarvegad/playwright-email-utils
Version:
Reusable utilities for handling emails in Playwright tests from Yopmail, Gmail and other providers.
63 lines (54 loc) • 1.96 kB
text/typescript
import { test, expect } from '@playwright/test';
import { emailUtils } from '../src/emailUtils';
const gmailConfig = {
clientId: process.env.GMAIL_CLIENT_ID || '',
clientSecret: process.env.GMAIL_CLIENT_SECRET || '',
refreshToken: process.env.GMAIL_REFRESH_TOKEN || '',
};
test('Gmail API: extract details from email', async () => {
// Example date range for filtering emails
const afterTime = '2025-06-01 00:00:00'; // Start of June 1st, 2025
const beforeTime = '2025-06-01 15:05:00'; // End of June 1st, 2025
// Define extraction rules based on your email content pattern
const extractionRules = [
{
field: 'releaseNotes',
type: 'between-markers' as const,
startMarker: 'Release Notes:',
endMarker: 'End of Release Notes'
},
{
field: 'url',
type: 'regex' as const,
regex: /https?:\/\/[^\s<]+/ // This regex will find URLs in the body
}
];
// Wait and find the email with extracted details
const result = await emailUtils.getEmail.fromGmailWithDetails(
gmailConfig,
{
subject: 'New Release',
afterTime,
beforeTime
},
extractionRules
);
expect(result.email).toBeTruthy();
if (result.email) {
console.log('Email Details:');
console.log('Subject:', result.email.subject);
console.log('From:', result.email.from);
console.log('To:', result.email.to);
console.log('Full Body:', result.email.body);
console.log('\nExtracted Details:');
console.log(result.extractedDetails);
// Check if release notes were extracted (if they exist in the email)
if (result.extractedDetails.releaseNotes) {
expect(result.extractedDetails.releaseNotes.length).toBeGreaterThan(0);
}
// Check if URL was extracted
if (result.extractedDetails.url) {
expect(result.extractedDetails.url).toMatch(/^https?:\/\//);
}
}
});