Automated script to validate a verification code

I am using javascript to create automated testing for a B2C sign-up policy. I am using mailosaur as an inbox to receive my verification code to complete the email registration. My script is similar to the below-pasted script. This can help me validate the message that's been successfully sent to the inbox however, I would want to add an additional fixture where the script can copy just the verification code (The body of the message would look like "your code is: XXXXXX") sent to the generic inbox and paste it back to a registration page to complete the user registration. Please advise how can this be achieved? Many Thanks

import uuidv4 from "uuid/v4";
import MailosaurClient from "mailosaur";

import signUpModel from "./page_models/sign_up_model";

const client = new MailosaurClient("<MAILOSAUR API KEY>");
const mailosaurServerId = "<MAILOSAUR SERVER ID>";
const emailAddress = `${uuidv4()}@dev-tester.com`;

fixture("Airport Gap Signup Flow")
  .page("https://airportgap-staging.dev-tester.com/tokens/new")
  .beforeEach(async () => await client.messages.deleteAll(mailosaurServerId));

test("User receives an email after signup", async t => {
  await t
    .typeText(signUpModel.emailInput, emailAddress)
    .typeText(signUpModel.passwordInput, "airportgap123")
    .click(signUpModel.submitButton);

  await t.wait(10000);

  let message = await client.messages.get(mailosaurServerId, {
    sentTo: emailAddress
  });

  await t.expect(message.to[0].email).eql(emailAddress);

  await t.expect(message.subject).eql("Here's your generated token");

  await t
    .expect(message.html.body)
    .contains("Here's your newly generated Airport Gap token");
});

Upvotes: 0

Views: 1165

Answers (1)

pavelsaman
pavelsaman

Reputation: 8362

It could be as simple a task as parsing some text:

const text = "Verify your email address Thanks for verifying your XXXXXXXXXX account! Your code is: 09876 Sincerely, B2C Dev";
const code = text.match(/Your code is: [0-9]+/)[0].split(':')[1].trim();
console.log(code);

This will output 09876 into a console.

1/ To match your code in the message:

text.match(/Your code is: [0-9]+/)

2/ To split it by colon:

[0].split(':')

3/ To get only the numbers (the code itself) and remove whitespace:

[1].trim()

I'd most likely create a helper function and wrap this code in it, so the test case is less polluted.


You can look up these methods on MDN:

Upvotes: 3

Related Questions