Playwright 电子邮件 Utils
用 Playwright 从一次性电子邮件服务(例如 Yopmail) 和 Gmail 中提取和交互邮件的功能包 .
特征
- 支持多个电子邮件提供者 :
- Gmail( 通过 Gmail API)
- Yopmail( 通过网络界面)
- 提取内容、 点击链接或动态获取属性
- 为延迟发送电子邮件而建的投票
- 有多个策略的高级电子邮件内容提取
- 可选自动删除支持
- 方便其他提供者扩展
安装
npm install @bhavinkumarvegad/playwright-email-utils
Gmail 设置
1. 设置 Gmail API 证书
- 转到 谷歌云控台
- 创建新工程或选择现有工程
- 为您的工程启用 Gmail API
- 创建 OAuth 2. 0 证书( 身份识别和客户端保密)
- 将 http://localhost 添加到授权重定向的 URI
2. 刷新托肯语Name
- 首先创建一个
.env在工程根中带有证书的文件 :
GMAIL_CLIENT_ID=your-client-id
GMAIL_CLIENT_SECRET=your-client-secret
- 配置环境变量 :
npm install dotenv
更新您的 playwright.config.ts数字 :
import 'dotenv/config';
export default defineConfig({
// ... rest of your config
});
- 生成刷新符号 :
npx get-gmail-token
- 跟随提示:
- 访问授权 URL
- 允许访问您的 Gmail 账户
- 从重定向 URL 复制代码
- 把它粘入终端
- 将提供的配置添加到您的.env 文件
用法实例
Gmail 整合
基本电子邮件搜索
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);
}
});
高级内容提取
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 整合
获取链接
const result = await emailUtils.getEmail.fromYopmail(page, 'demoInbox', 'Welcome Email', [
{
selector: { type: 'text', value: 'Click here' },
action: 'getAttribute',
attributeName: 'href',
description: 'Reset Link'
}
]);
多个动作
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'
}
]);
自动删除示例
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
);
采掘业战略
Gmail 内容提取
在与Gmail合作时,您有三种复杂的提取策略:
-
在标记之间 (单位:千美元)
type: 'between-markers'(中文(简体) )- 提取电子邮件正文中特定标记之间的内容
- 对结构化内容的理想,如发布说明或核查代码
- 示例
{ field: 'releaseNotes', type: 'between-markers', startMarker: 'Release Notes:', endMarker: 'End of Release Notes' } -
正则表达式 (单位:千美元)
type: 'regex'(中文(简体) )- 使用正则图案提取内容
- 适合URL,版本编号,代码等.
- 示例
{ field: 'confirmationUrl', type: 'regex', regex: /https:\/\/confirm\.example\.com\/[a-zA-Z0-9-]+/ } -
全文 (单位:千美元)
type: 'full-text'(中文(简体) )- 获取完整的电子邮件正文
- 需要全部内容时有用
- 示例
{ field: 'completeEmail', type: 'full-text' }
Gmail 提取规则结构 :
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 内容提取
对于Yopmail,您有基于UI相互作用的不同提取方法:
-
文本内容 (单位:千美元)
action: 'textContent'(中文(简体) )- 从元素中提取文本内容
- 示例
{ selector: { type: 'text', value: 'Order ID:' }, action: 'textContent', description: 'Order Number' } -
属性值 (单位:千美元)
action: 'getAttribute'(中文(简体) )- 提取特定属性( 如 href, 值)
- 示例
{ selector: { type: 'text', value: 'Reset Password' }, action: 'getAttribute', attributeName: 'href', description: 'Reset Link' } -
单击并提取 (单位:千美元)
action: 'click'(中文(简体) )- 点击元素并可以与其他动作结合
- 示例
{ selector: { type: 'role', value: { role: 'button', name: 'View Details' }}, action: 'click', description: 'Open Details' }
Yopmail 提取规则结构 :
interface EmailAction {
selector: {
type: 'text' | 'role' | 'locator';
value: string | { role: string; name?: string };
};
action: 'click' | 'textContent' | 'getAttribute';
attributeName?: string; // Required for getAttribute
description: string;
}
解决问题
-
环境变量
- 保证之后没有空格
=在.env文件中 - 校验
dotenv安装和配置 - 检查环境变量已装入
- 保证之后没有空格
-
认证问题
- 使用
npx get-gmail-token - 确保给予所有必要的许可
- 使用
-
电子邮件搜索问题
- 校验搜索参数
- 检查账户中存在的电子邮件
- 对延迟的邮件使用重试机制
许可证
麻省理工学院