Selenium WebDriver Web App Sign Up Test

Lately I’ve been having fun with the Selenium WebDriver to test a Web App! It’s super fun, and if you haven’t used Selenium before, I highly recommend it!

I’m writing all of my tests in C#, and it was really easy to get started thanks to Selenium’s documentation and some nice write-ups from Microsoft.

The most fun test so far has been the Sign Up test. Obviously I want to make sure that the process to Sign Up for a new account doesn’t break, so I created a test for it.

The sign up process I’m testing, however, requires that you provide a verification code sent  to your email. I started looking into how this could be done. Maybe creating a Selenium method to sign into a Gmail account, or use the Zapier email parser?

I landed on using Gmail’s API, and it works quite well! Thankfully Google’s documentation is fantastic and I was able to figure it out relatively quickly. The quickstart was extremely helpful, I would definitely recommend starting there.

My Sign Up test uses a .runsettings file that I set up in an Azure DevOps Release Pipeline. That way I can create a unique email per release. It looks something like [email protected] I didn’t include the setup code for the Gmail API below, but you can find it in their quickstart.

The code for retrieving the verification was fairly simple.

Let me know if I can do anything better!

List result = new List(); // Create a list
UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List("me"); // Define the request
request.Q = "to:" + email; // Define our filter to search for the email address we specify</code>

do
{
     try
     {
          ListMessagesResponse response = request.Execute(); // Execute the request
          result.AddRange(response.Messages); // Add the emails we find to the list
          request.PageToken = response.NextPageToken; // Look for more results
     }
     catch (Exception e)
     {
          Trace.Write("An error occurred: " + e.Message);
     }
} while (!String.IsNullOrEmpty(request.PageToken)); // Stop looking when there are no more results

var id = result.Select(x => x.Id).ToArray(); // Get email id from our results, should only be one email to get id out of

try
{
     var message = service.Users.Messages.Get("me", id[0]).Execute(); // Get the email using the email id
     string snippet = message.Snippet; // Retrieve the snippet from the email
     string numbers = Regex.Replace(snippet, "[^0-9]+", string.Empty); // Get numbers out of snippet
     string code = numbers.Substring(numbers.Length - 6, 6); // Our code is six digits and always at the end, get the last six numbers
     return code; // Return our verification code
}
catch (Exception e)
{
     Trace.Write("An error occurred: " + e.Message);
}

return null; // If no verification code was found, return null

Leave a Reply

Your email address will not be published. Required fields are marked *