Introduction
π Welcome to my first technical blog! In this article, I'll demonstrate how I automated OTP authentication in Selenium using Microsoft Authenticator and TOTP generation.
One of the most common challenges faced by automation engineers is handling One-Time Password (OTP) verification during login automation. As organizations increasingly adopt Two-Factor Authentication (2FA) to improve security, traditional Selenium scripts often fail when an OTP is required after entering credentials.
In this article, I'll demonstrate how to automate Time-Based One-Time Passwords (TOTP) in Selenium using Microsoft Authenticator and the OTP Java library.
The Workflow
A typical secure login process looks like this:
- Enter Username
- Enter Password
- Receive OTP from Authenticator App
- Enter OTP
- Access Application
Understanding TOTP
TOTP (Time-Based One-Time Password) is generated using a secret key and the current timestamp. Applications such as GitHub, Microsoft, and Google use this mechanism for two-factor authentication.
The good news is that if we have access to the secret setup key used during authenticator configuration, we can programmatically generate the OTP during test execution.
Demo Scenario
For this demonstration, I used GitHub login with Two-Factor Authentication enabled.
The automation flow:
Launch Browser
β
Enter Username
β
Enter Password
β
Click Sign In
β
Generate Current OTP
β
Enter OTP
β
Login Successfully
Configure Microsoft Authenticator
Before automation, enable Two-Factor Authentication in your GitHub account:
- Navigate to Settings
- Select Password and Authentication
- Enable Two-Factor Authentication
- Choose Authenticator App
- Scan the QR Code using Microsoft Authenticator
- Save the Setup Key securely
The Setup Key is the most important piece because it will be used by Selenium to generate OTPs dynamically.
Add Maven Dependency
Add the Aerogear OTP library to your pom.xml:
Click here to check latest version of the Aerogear OTP library for pom.xml:
<dependency>
<groupId>org.jboss.aerogear</groupId>
<artifactId>aerogear-otp-java</artifactId>
<version>1.0.0</version>
</dependency>
Generate OTP in Selenium
Create a TOTP object using your setup key:
Totp totp = new Totp("YOUR_SETUP_KEY");
String currentOTP = totp.now();
The now() method generates the currently valid OTP exactly like Microsoft Authenticator.
Complete Login Flow
After entering username and password:
driver.findElement(By.id("app_totp")).sendKeys(currentOTP);
Selenium automatically enters the generated OTP and completes authentication without manual intervention.
Security Best Practices
While implementing OTP automation:
- Never hardcode setup keys in source code.
- Store secrets in encrypted configuration files or secret managers.
- Use dedicated test accounts.
- Avoid using production credentials.
- Restrict access to authentication secrets.
Benefits
Using this approach:
β Fully automated login flows
β Supports CI/CD execution
β Eliminates manual OTP entry
β Improves regression testing efficiency
β Enables true end-to-end automation
Conclusion
OTP authentication no longer needs to be a blocker for test automation. By leveraging Microsoft Authenticator's setup key and the Aerogear OTP library, Selenium can dynamically generate valid OTPs during runtime and seamlessly complete secure login workflows.
This approach has helped many automation teams achieve reliable end-to-end testing while maintaining strong security controls.
Author's Note:
This is my first technical blog on DEV.to. As a QA Automation Engineer passionate about test automation and emerging technologies, I wanted to start by sharing a practical solution to a challenge many automation engineers faceβautomating OTP-based authentication. I hope this article helps fellow testers and developers simplify their automation efforts, and I look forward to sharing more real-world automation insights in future posts.
Have you automated OTP authentication in your projects? Share your experience and preferred approach in the comments below.


Top comments (0)