How to Handle OTP Verification on Test Automation
One of the challenging situations to handle automation tests is the case of handling the functionality outside the browser/framework. Two of the main challenges of test automation in terms of application with auth such as accessing the SMS and email.
When there is a way to read a SMS to receive through programmatic way, it will help to complete the steps in terms of registration or login. There are some online tools that will help us in achieving to send or receiving messages.
One of the tools we are going to see is Twilio. Lets see the advantages and disadvantages of Twilio.
Advantages
- No need for a physical sim card to access the code/OTP.
- Can get access to any country service provider.
- We have integration with multiple languages and framework support.
- SMS can be sent/accessed in an API call.
- SMS can be sent through a static number.
Disadvantages
- Pricing is involved because of the service provider.
- Limited customized option in Twilio.
How to create an account in Twilio here. Once the account is created they are given free credit of $15 to try.
We need to create an account and set up the phone number. On open source, we have a number generated by Twilio and in case of a private number, we can purchase it.
Running an endpoint to check the messages received for the particular account.
Method : GET
https://api.twilio.com/2010-04-01/Accounts/<AccountSID>/Messages.json
We need to set the basic auth with username and password with the values of Account SID and Auth Token.
username: Account SID
password: Auth Token
We can have multiple parameters to filter the result based on to, from and datesend, etc.
Twillio supports multiple languages. Here are some of the languages supported on Twillio. For your reference here
- Node.JS
- Python
- C#
- Go
- PHP
- Ruby
- Twilio-cli (Inbuild command line)
- Curl
Cypress code to get the message in response to the code.
describe("Get Twillio message from the response", () => {
it("Get Twillio message from the response", () => {
cy.request({
method: METHOD.GET,
url: '<url_from_twilio>',
failOnStatusCode: false,
auth: {
username:'<Account SID>',
password:'<Auth Token>',
},
}).then((response) => {
expect(response.status).to.eq(STATUS_CODE.OK);
const respBody = response.body;
cy.log(respBody.messages[0].body)
});
});
Clearly, we can bypass the provision of having a physical sim in case. This will help to receive SMS anywhere using the API.
I’m planning to do a writing blog on receiving emails through API and it's also one of the main challenges of tech automation.
Feel free to hit clap if you like the content. Happy Automation Testing :) Cheers. 👏