TIP 1: How to generate random email address in C#

How to generate random email in C#Hi all, in this article I would like to show you some short bunches of code which helps you to test your amazing web applications. I definitely understand, that for Senior C# developers it can be funny and obvious, but let’s do not forget about people who just started learn C# and Selenium testing, because for them it will be not so obvious as expected :)

Let’s say that you have to generate email address to test “Create User” form. We can use standart microsoft class Random and method Next(). Please, take a look on the next code:

 

 

Console Application Example:

    class Program
    {
        static void Main(string[] args)
        {
            string myCustomEmailAddress = GenerateEmailAddress();
            
            Console.WriteLine(myCustomEmailAddress);
            
            Console.ReadKey();

        }

        // Generate Random Email Address 
        public static string GenerateEmailAddress()
        {
            return "vitalina.boiko" + GetRandomNumber() + "@gmail.com";
        }

        // Generate random number for email address
        public static int GetRandomNumber()
        {
            return new Random().Next(100000, 100000000);
        }
    }

OUTPUT:

vitalina.boiko78206568@gmail.com

This code works fine if you would like to generate one email per test. It will be a problem, if you need 10 or 100 emails in the same time, because generation of 100 emails in the loop will return the same result.

The idea of how to resolve this issue i found here. The problem is that we create ‘Random’ instance every time when we call GetRandomNumber() method with the same seed. To fix we have two ways:

1) Pass new seed every time to ‘Random’ class constructor

2) Create ‘Random’ instance only once

So, i have chosen second way.

let’s create helper for that:

    public class TestDataHelper
    {
        private static Random _random = new Random();

        public static string GenerateEmailAddress()
        {
            return string.Format("vitalina.boiko{0}@gmail.com", _random.Next(10000, 99999));
        }
    }

so,  now we have our helper which we can use, btw. this class can contains many methods which help us to test our application.

using:

Console Application Example:

    class Program
    {
        static void Main(string[] args)
        {
            string myCustomEmailAddress1 = TestDataHelper.GenerateEmailAddress();

            string myCustomEmailAddress2 = TestDataHelper.GenerateEmailAddress();

            string myCustomEmailAddress3 = TestDataHelper.GenerateEmailAddress();

            Console.WriteLine(myCustomEmailAddress1);

            Console.WriteLine(myCustomEmailAddress2);

            Console.WriteLine(myCustomEmailAddress3);

            Console.ReadKey();
        }
    }

OUTPUT:

vitalina.boiko52275@gmail.com
vitalina.boiko48573@gmail.com
vitalina.boiko09652@gmail.com

That’s it!

 

Download DEMO (Visual Studio 2013)