UNPKG

@bhavinkumarvegad/playwright-email-utils

Version:

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

76 lines (75 loc) 3.61 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromYopmail = fromYopmail; function fromYopmail(page, inbox, subjectName, actions, deleteFlag) { return __awaiter(this, void 0, void 0, function* () { yield page.goto('https://yopmail.com/'); yield page.getByRole('textbox', { name: 'Login' }).fill(inbox); yield page.getByRole('button', { name: '' }).click(); if (subjectName) { const timeoutMs = 5 * 60 * 1000; const pollInterval = 10 * 1000; const start = Date.now(); let found = false; while (Date.now() - start < timeoutMs) { const inboxFrame = yield page.locator('iframe[name="ifinbox"]').contentFrame(); try { yield inboxFrame.getByRole('button', { name: subjectName }).waitFor({ timeout: pollInterval }); yield inboxFrame.getByRole('button', { name: subjectName }).click(); found = true; break; } catch (_a) { yield page.locator('#refresh').click(); console.log('Waiting for email with subject'); } } if (!found) { throw new Error(`Email with subject '${subjectName}' not found after 5 minutes.`); } } const results = {}; for (const act of actions) { const frame = yield page.locator('iframe[name="ifmail"]').contentFrame(); let locator; if (act.selector.type === 'role') { locator = frame.getByRole(act.selector.value.role, act.selector.value.name ? { name: act.selector.value.name } : undefined); } else if (act.selector.type === 'text') { locator = frame.getByText(act.selector.value); } else { locator = frame.locator(act.selector.value); } let result; if (act.action === 'click') { yield locator.first().click(); result = 'clicked'; } else if (act.action === 'textContent') { result = yield locator.first().textContent(); } else if (act.action === 'getAttribute') { result = yield locator.first().getAttribute(act.attributeName); } results[act.description] = result; } if (deleteFlag && subjectName) { const inboxFrame = yield page.locator('iframe[name="ifinbox"]').contentFrame(); const emailButton = inboxFrame.getByRole('button', { name: subjectName }); const checkbox = emailButton.locator('..').getByRole('checkbox'); yield checkbox.check(); yield page.locator('#delsel').click(); } return results; }); }