Menu

How to Handle OTP in Selenium Java with a Real SMS Number API
πŸ“°

How to Handle OTP in Selenium Java with a Real SMS Number API

DEV Community: javaΒ·WilletteΒ·12 days ago
#wSiAVgLr
#devtojava#devto#dev#test#java
Reading 0:00
15s threshold

Disclosure: I work with HeroSMS, the virtual number provider used in the examples below. Written with AI tools; code and endpoints checked against the HeroSMS API reference.

If you've tried to handle OTP in Selenium WebDriver, you know this test. It types a phone number, clicks "Send code", then sits in Thread.sleep(30000) while you read the code off your phone and type it in. That works on your own machine. In the nightly run nobody types anything, so the next explicit wait fails:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for url to contain "/welcome". Current url: "https://staging.example.com/signup/verify" (tried for 15 seconds with 500 milliseconds interval)

Enter fullscreen mode

Exit fullscreen mode

The CI runner has no SIM card. This tutorial replaces the human with an API. The test rents a real number, polls for the SMS code from Java with a deadline, types the code after an explicit wait and releases the number in finally .…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.