@bhavinkumarvegad/playwright-email-utils

中文文档
1.1.0发布于 2025/06/03MITTypeScriptCommonJS近 30 天下载 4022

Reusable utilities for handling emails in Playwright tests from Yopmail, Gmail and other providers.

原始使用文档
文档目录13 节
  1. Features
  2. Installation
  3. Gmail Setup
  4. 1. Set up Gmail API Credentials
  5. 2. Get Refresh Token
  6. Usage Examples
  7. Gmail Integration
  8. Yopmail Integration
  9. Extraction Strategies
  10. Gmail Content Extraction
  11. Yopmail Content Extraction
  12. Troubleshooting
  13. License

Playwright Email Utils

A utility package to extract and interact with emails from both disposable email services (e.g., Yopmail) and Gmail using Playwright.

Features

  • Support for multiple email providers:
    • Gmail (via Gmail API)
    • Yopmail (via web interface)
  • Extract content, click links, or get attributes dynamically
  • Built-in polling for delayed email delivery
  • Advanced email content extraction with multiple strategies
  • Optional auto-delete support
  • Easy to extend for other providers

Installation

npm install @bhavinkumarvegad/playwright-email-utils

Gmail Setup

1. Set up Gmail API Credentials

  1. Go to the Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the Gmail API for your project
  4. Create OAuth 2.0 credentials (Client ID and Client Secret)
  5. Add http://localhost to the authorized redirect URIs

2. Get Refresh Token

  1. First create a .env file in your project root with your credentials:
GMAIL_CLIENT_ID=your-client-id
GMAIL_CLIENT_SECRET=your-client-secret
  1. Configure environment variables:
npm install dotenv

Update your playwright.config.ts:

import 'dotenv/config';

export default defineConfig({
  // ... rest of your config
});
  1. Generate your refresh token:
npx get-gmail-token
  1. Follow the prompts:
    • Visit the authorization URL
    • Grant access to your Gmail account
    • Copy the code from the redirect URL
    • Paste it into the terminal
    • Add the provided configuration to your .env file

Usage Examples

Gmail Integration

import { emailUtils } from '@bhavinkumarvegad/playwright-email-utils';
import 'dotenv/config';

test('Read email from Gmail', async () => {
  const config = {
    clientId: process.env.GMAIL_CLIENT_ID!,
    clientSecret: process.env.GMAIL_CLIENT_SECRET!,
    refreshToken: process.env.GMAIL_REFRESH_TOKEN!
  };

  const email = await emailUtils.getEmail.fromGmail(config, {
    subject: 'New release on live - 1.1.0',
    from: '[email protected]',
    to: '[email protected]',
    afterTime: '2025-06-01 00:00:00',
    beforeTime: '2025-06-01 23:59:59'
  });

  if (email) {
    console.log('Subject:', email.subject);
    console.log('From:', email.from);
    console.log('Body:', email.body);
  }
});

Advanced Content Extraction

const extractionRules = [
  {
    field: 'releaseNotes',
    type: 'between-markers',
    startMarker: 'Release Notes:',
    endMarker: 'End of Release Notes'
  },
  {
    field: 'deploymentUrl',
    type: 'regex',
    regex: /https?:\/\/[^\s<]+/
  },
  {
    field: 'fullContent',
    type: 'full-text'
  }
];

const result = await emailUtils.getEmail.fromGmailWithDetails(
  config,
  { subject: 'New Release' },
  extractionRules
);

if (result.email) {
  console.log('Release Notes:', result.extractedDetails.releaseNotes);
  console.log('URL:', result.extractedDetails.deploymentUrl);
}

Yopmail Integration

const result = await emailUtils.getEmail.fromYopmail(page, 'demoInbox', 'Welcome Email', [
  {
    selector: { type: 'text', value: 'Click here' },
    action: 'getAttribute',
    attributeName: 'href',
    description: 'Reset Link'
  }
]);

Multiple Actions

const results = await emailUtils.getEmail.fromYopmail(page, 'testInbox', 'Order Confirmation', [
  // Extract text content
  {
    selector: { type: 'text', value: 'Order ID:' },
    action: 'textContent',
    description: 'Order Number'
  },
  // Click a button
  {
    selector: { type: 'role', value: { role: 'button', name: 'Track Order' }},
    action: 'click',
    description: 'Track Order Button'
  },
  // Get attribute using custom locator
  {
    selector: { type: 'locator', value: '.verification-code' },
    action: 'textContent',
    description: 'Verification Code'
  }
]);

Auto-Delete Example

const result = await emailUtils.getEmail.fromYopmail(
  page,
  'inbox123',
  'Password Reset',
  [
    {
      selector: { type: 'text', value: 'verification code is:' },
      action: 'textContent',
      description: 'Reset Code'
    }
  ],
  true // Enable auto-delete
);

Extraction Strategies

Gmail Content Extraction

When working with Gmail, you have three sophisticated extraction strategies available:

  1. Between Markers (type: 'between-markers')

    • Extracts content between specific markers in the email body
    • Ideal for structured content like release notes or verification codes
    • Example:
    {
      field: 'releaseNotes',
      type: 'between-markers',
      startMarker: 'Release Notes:',
      endMarker: 'End of Release Notes'
    }
    
  2. Regular Expression (type: 'regex')

    • Extracts content using regex patterns
    • Perfect for URLs, version numbers, codes, etc.
    • Example:
    {
      field: 'confirmationUrl',
      type: 'regex',
      regex: /https:\/\/confirm\.example\.com\/[a-zA-Z0-9-]+/
    }
    
  3. Full Text (type: 'full-text')

    • Gets the complete email body
    • Useful when you need the entire content
    • Example:
    {
      field: 'completeEmail',
      type: 'full-text'
    }
    

Gmail Extraction Rule Structure:

interface EmailExtractionRule {
  field: string;              // Name for the extracted content
  type: 'between-markers' | 'regex' | 'full-text';
  startMarker?: string;       // Required for 'between-markers'
  endMarker?: string;        // Required for 'between-markers'
  regex?: RegExp;            // Required for 'regex'
}

Yopmail Content Extraction

For Yopmail, you have different extraction methods based on UI interactions:

  1. Text Content (action: 'textContent')

    • Extracts text content from elements
    • Example:
    {
      selector: { type: 'text', value: 'Order ID:' },
      action: 'textContent',
      description: 'Order Number'
    }
    
  2. Attribute Values (action: 'getAttribute')

    • Extracts specific attributes (e.g., href, value)
    • Example:
    {
      selector: { type: 'text', value: 'Reset Password' },
      action: 'getAttribute',
      attributeName: 'href',
      description: 'Reset Link'
    }
    
  3. Click and Extract (action: 'click')

    • Clicks elements and can be combined with other actions
    • Example:
    {
      selector: { type: 'role', value: { role: 'button', name: 'View Details' }},
      action: 'click',
      description: 'Open Details'
    }
    

Yopmail Extraction Rule Structure:

interface EmailAction {
  selector: {
    type: 'text' | 'role' | 'locator';
    value: string | { role: string; name?: string };
  };
  action: 'click' | 'textContent' | 'getAttribute';
  attributeName?: string;     // Required for getAttribute
  description: string;
}

Troubleshooting

  1. Environment Variables

    • Ensure no spaces after = in .env file
    • Verify dotenv is installed and configured
    • Check environment variables are loaded
  2. Authentication Issues

    • Regenerate refresh token using npx get-gmail-token
    • Ensure all required permissions are granted
  3. Email Search Issues

    • Verify search parameters
    • Check email exists in account
    • Use retry mechanism for delayed emails

License

MIT

版本与发布记录

@bhavinkumarvegad/playwright-email-utils 共发布 3 个版本,首次发布于 2025/05/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2025/06/031.4 年前
1.0.12025/05/261.5 年前
1.0.02025/05/261.5 年前

关于 @bhavinkumarvegad/playwright-email-utils 的常见问题

@bhavinkumarvegad/playwright-email-utils 是什么?

Reusable utilities for handling emails in Playwright tests from Yopmail, Gmail and other providers.

如何安装 @bhavinkumarvegad/playwright-email-utils?

在项目目录运行 npm install @bhavinkumarvegad/playwright-email-utils,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 get-gmail-token,可以用 npx get-gmail-token 直接运行。

@bhavinkumarvegad/playwright-email-utils 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bhavinkumarvegad/playwright-email-utils 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bhavinkumarvegad/playwright-email-utils 可以商用吗?

@bhavinkumarvegad/playwright-email-utils 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bhavinkumarvegad/playwright-email-utils 的下载量有多少?

根据 npm 官方下载统计,@bhavinkumarvegad/playwright-email-utils 在 2026/10/08 – 2026/11/06 这 30 天内被下载 4,022 次,日均约 134 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bhavinkumarvegad/playwright-email-utils 还在维护吗?

最新版本 1.1.0 发布于 2025/06/03(1.4 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/05/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。