Observed since 20 Oct, 19:12 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
Capture 2FA verification codes from Android notifications for automated testing with Playwright
A Node.js library for automatically capturing 2FA verification codes from Android notifications using ADB and OCR, with seamless Playwright integration for end-to-end testing.
npm install 2fa-adb-tools
npm install --save-dev @playwright/test
brew install android-platform-toolssudo apt-get install adb (Debian/Ubuntu)adb devices
# Should show your device as "device"
const { TwoFAHelper } = require('2fa-adb-tools');
const helper = new TwoFAHelper({ screenshotInterval: 3000 });
await helper.startMonitoring();
try {
const code = await helper.waitForCode(60000);
console.log(`2FA code: ${code}`);
} finally {
await helper.stopMonitoring();
}
const { test, expect } = require('@playwright/test');
const { TwoFAHelper, waitFor2FACode } = require('2fa-adb-tools');
test.describe('2FA Test', () => {
let twoFAHelper;
test.beforeAll(async () => {
twoFAHelper = new TwoFAHelper({ screenshotInterval: 3000 });
await twoFAHelper.startMonitoring();
});
test.afterAll(async () => {
await twoFAHelper.stopMonitoring();
});
test('login with 2FA', async ({ page }) => {
await page.goto('https://example.com/login');
await page.fill('#username', process.env.USERNAME);
await page.fill('#password', process.env.PASSWORD);
await page.click('#submit');
const code = await waitFor2FACode(page, twoFAHelper, {
codeInputSelector: '#code-input',
autoFill: true
});
await expect(page.locator('.success-message')).toBeVisible();
});
});
Main class for Playwright integration and simplified 2FA code management.
| Method | Description |
|---|---|
startMonitoring() | Begin monitoring for 2FA codes |
stopMonitoring() | Stop monitoring and cleanup |
waitForCode(timeout) | Wait for next code (returns Promise) |
Low-level class for direct ADB interaction.
| Event | Description |
|---|---|
code | Emitted when a verification code is found |
error | Emitted on errors |
Handles text recognition from images.
const ocr = new OCRProcessor({ languages: 'rus+eng' });
await ocr.init();
const text = await ocr.recognize('./screenshot.png');
const code = ocr.processText(text);
await ocr.terminate();
Manages screenshot capture and storage.
const sm = new ScreenshotManager({
adbPath: 'adb',
deviceId: 'device-id',
maxScreenshots: 50
});
const screenshot = await sm.takeScreenshot();
const cropped = await sm.cropScreenshot(screenshot);
sm.saveCodeScreenshot(screenshot, cropped, '123456');
{
// Monitoring
screenshotInterval: 3000, // How often to take screenshots (ms)
autoSwipe: true, // Enable/disable all swipes
// Swipe behavior
swipeAfterCode: true, // Swipe after finding a code
swipeInterval: 10000, // Auto-swipe every 10 seconds (0 = disable)
// Swipe coordinates
swipeConfig: {
startX: 150, startY: 1200, // Start position
endX: 950, endY: 1200, // End position
duration: 400 // Swipe duration (ms)
},
// OCR crop region (where codes appear)
cropConfig: {
top: 930, bottom: 100, // Vertical crop
left: 100, right: 100 // Horizontal crop
},
// Advanced
ocrLanguages: 'rus+eng', // Tesseract languages
deviceId: null, // Specific device ID
maxScreenshots: 50 // Screenshots to keep
}
swipeConfig accordinglyAdjust cropConfig to focus only on the area where 2FA codes appear in notifications.
| Issue | Solution |
|---|---|
| ADB not found | Install Android platform-tools and add to PATH |
| No devices | Run adb devices, enable USB debugging, re-authorize device |
| OCR fails | Adjust cropConfig, improve screenshot quality, check contrast |
| Swipe not working | Verify coordinates, increase duration, ensure screen is unlocked |
// Enable verbose logging
process.env.DEBUG = '2fa-adb-tools:*';
// Log events
helper.on('code', console.log);
helper.on('error', console.error);
This project was developed with assistance from AI language models (DeepSeek) for code generation, documentation, and refactoring. The core logic, architecture decisions, and final code quality were reviewed, tested and approved by a human developer.
MIT © pifpafi4 - делайте что хотите, я за код не отвечаю.
Download counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 1.0.0Registry latest | 13 Mar 2026 | 0 |
This describes our collected data; it is not a security assessment.
Observed since 20 Oct, 19:12 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:d025a7b9a983bc8fa807e6bcdc33288ef2ded6d2f40e713839a36ed77db4f2bb