How can we help?

SMS testing

You are here:

If your application sends SMS, you are in good hands with Trudon, as it can test if your messages are successfully sent via custom JavaScript functions.

The following example can also be used to handle one-time password or two-factor authentication.

For the purpose of this example, we are going to assume you are using Twilio and will extract the last message from a given phone number. Of course, you can change its logic to do basically anything you want, potentially with other SMS platforms.

function trudonJSStep(element, callbacks, builtinVars, customVars)
{
  // variables definition
  var = phoneNumber = '15555555555';
  var acctId = 'xxxxxxxxxxxxxxxxxxxxxxxx';
  var sid = 'xxxxxxxxxxxxxxxxxxxxxxxx';
  var secret = 'xxxxxxxxxxxxxxxxxxxxxxxx';
  var url = `https://api.twilio.com/2010-04-01/Accounts/${acctId}/Messages.json?To=%2B${phoneNumber}&PageSize=1`;

  window.fetch(url, {
    method: 'GET',
    headers: {
      'Authorization': 'Basic ' + window.btoa(`${sid}:${secret}`)
    }
  }).then(function (response) {
    return response.json();
  }).then(function (data) {
    try {
      var lastSMS = data.messages[0].body;
      callbacks.pass('Last message found: ' + lastSMS);
      customVars.lastSMS = lastSMS; //Save the variable
      callbacks.done();
    } catch (err) {
      callback.fail(err);
      callbacks.done();
    }
  });
}

Keep in mind that you have to perform this step after the SMS message is sent during the test run. It’s recommended that you add a 10-20 second pause step before checking for the message to ensure enough time for delivery.

It is also highly recommended that you create a separate Twilio API for such operations with limited access for Trudon.

Was this article helpful?
0 out Of 5 Stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
How can we improve this article?
Need help?