Testing one-time passwords (OTPs) can be such a pain, right? Constantly flipping between your app and SMS inbox, hunting for codes... It’s time we fixed that. Let’s talk about automating the whole process using tools that just get the job done. And yeah, we’ll use temporary phone numbers too, but we’ll keep it chill and focus on real value for your dev workflow.
Here’s how you can level up your OTP game with tools like Playwright, Puppeteer, Selenium, and a bit of creativity. No matter your stack, you’ll find some nuggets here.
Grab some temporary phone numbers programmatically, snag the OTPs via API, and automate the rest. No more back-and-forth, no more drama. You’ll look like a wizard during sprint reviews.
There are plenty of services out there for temporary phone numbers, so why pick Quackr.io? Here’s the deal:
We’ve used Quackr.io because it ticks all these boxes. You’re welcome to try others, but if you’re looking for something dependable, Quackr.io is a great starting point.
Here’s how you can build an OTP automation script. Think of it as a starting point—make it yours.
First, sign up with a service that provides temporary phone numbers (we’re using Quackr.io). Grab your API key, and let’s roll.
If you’re rocking Node.js, you’ll need a few packages:
npm install playwright axios dotenv
Here’s an example using Playwright and Axios. Feel free to adapt it for your setup.
const { chromium } = require('playwright'); const axios = require('axios'); require('dotenv').config(); (async () => { const browser = await chromium.launch(); const page = await browser.newPage(); // Fetch a temporary phone number const phoneResponse = await axios.get('https://quackr.io/api/phones', { headers: { 'Authorization': `Bearer ${process.env.API_KEY}` } }); const phoneNumber = phoneResponse.data.number; console.log(`Using phone number: ${phoneNumber}`); // Simulate your app’s signup flow await page.goto('https://yourapp.com/signup'); await page.fill('#phone-input', phoneNumber); await page.click('#send-otp'); // Wait for the OTP let otp; while (!otp) { const messages = await axios.get(`https://quackr.io/api/messages?phone=${phoneNumber}`, { headers: { 'Authorization': `Bearer ${process.env.API_KEY}` } }); const otpMessage = messages.data.find(msg => msg.body.includes('Your OTP is')); if (otpMessage) { otp = otpMessage.body.match(/\d{6}/)[0]; console.log(`Got OTP: ${otp}`); } else { console.log('Waiting for OTP...'); await new Promise(resolve => setTimeout(resolve, 2000)); } } // Enter the OTP and complete verification await page.fill('#otp-input', otp); await page.click('#verify-otp'); console.log('OTP verification done!'); await browser.close(); })();
Use a .env file for your API keys. Example:
API_KEY=your_api_key_here
You can extend this setup for:
This is just scratching the surface. Automating OTPs can save so much time and make your tests feel polished. If you’ve got ideas to make this even better, I’d love to hear them. Got any cool tricks for handling OTPs? Drop them below—let’s learn from each other!
The above is the detailed content of Automate One-Time Password (OTP) Testing: A Casual Guide. For more information, please follow other related articles on the PHP Chinese website!