How to Create Random Emails Using Selenium

In test automation, creating a random email address is extremely crucial. If we desire to automate a signup web page, every time we require to produce a special email address to finish the signup procedure effectively. In this article, we will certainly demonstrate how to create random e-mail addresses utilizing Selenium IDE.

There are 2 means to produce arbitrary email addresses in selenium IDE.

1. Using a .js file and use it as a command.

2. Using single line command in a selenium script.

Using a.js File and Use as a Command

1. Copy the code listed below into a .txt file and also rename it as 'random_email. js'.

Selenium.prototype.doTypeRandomEmail = function(locator) 
{
    /**
    * Define the value of an input field to a random email id as though you typed it in.
    *
    * @param locator an element locator
    */
    // All locator-strategies are automatically handled by "findElement".
    var element = this.page().findElement(locator);
    /* Create a random email string. */
    var allowedChars = "aabbccddeeffgghhiijjkkllmm";
    var stringLength = 9;
    var randomstring = '';
    for (var i=0; i<stringLength>; i++)
    {
        var rnum = Math.floor(Math.random() * allowedChars.length);
        randomstring += allowedChars.substring(rnum,rnum+1);
    }
    // Append a domain name.
    randomstring += "@domain.com"
    // Replace the element text with the new text.
    this.browserbot.replaceText(element, randomstring);
};

2. Import your personalized command right into selenium.

3. When you have actually included the data to the alternatives web page you require to reactivate the Selenium IDE for the adjustments to take result. Whenever you include some brand-new code or modify the left one in the 'user-extensions'.

4. When you have actually rebooted the IDE, you can see that your command has actually been included in the drop-down.

5. Then, you can utilize this command as you want.

Using Single Line Command in a Selenium Script

Selenium IDE is an Automation screening device that gives the Record/Playback device for screening internet sites. IDE represents Integrated Growth Atmosphere. It is applied as Firefox Attachment as well as enables recording, editing and enhancing, and also debugging examination.

You can make use of the complying with code to transform an Email id arbitrarily throughout each implementation.

storeEval Math.round (Math.random() * 1268) random
type id=email selenium${random}@domain.com
type id=password user0123

Selenium represents the regional part of the e-mail id and arbitrarily signifies arbitrary number as well as increase by 1268 as well as round the worth to local integer number which will certainly be provided to the e-mail id. Throughout each implementation, an arbitrary e-mail id will certainly be created.

Hope this article assists you to address the concern pertaining to creating random e-mails by using Selenium.

More Solutions for Creating Random Emails

1. Try with the Random class that allows you to create a Random number for a random Email ID. In the following code example, the random number will start from 0 to 99.

emailTextBx.click();  
Random randomGenerator = new Random();  
int randomInt = randomGenerator.nextInt(100);  
emailTextBx.sendKeys("username"+ randomInt +"@domain.com");

2. You can create a random and formated three-digit number rather than separate digits. This means that it creates value of 0 and you will get "example000" rather than "example0".

Random random = new Random();
int number = random.nextInt(1000);
String randoms = String.format("%03d", number);

See Also: How to Create Random String

1. The most efficient way of creating random strings in my tests is as below. This example shows how to create a random string with a length of 12 characters.

public static String generateString() {
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.substring(0, Math.min(uuid.length(), 12));
        System.err.println(uuid);
        return uuid;
    }

2. Besides, you can use the following method to create random strings.

protected String getSaltString() {
        String SALTCHARS = "01234567890abcdefghijklmnopqrstuvwxyz";
        StringBuilder salt = new StringBuilder();
        Random rnd = new Random();
        while (salt.length() < 16) { // Define the length of the random string.
            int index = (int) (rnd.nextFloat() * SALTCHARS.length());
            salt.append(SALTCHARS.charAt(index));
        }
        String saltStr = salt.toString();
        return saltStr;

    }


Comments

OliviaJanuary 27, 2022 1:00:30

It worked exactly what I was hoping for. Thank you.

MadeleineAugust 16, 2016 8:03:32

An object of Random class is initialized and the method nextInt(), nextDouble() or nextLong() is used to generate random number. I've ever used Math. Random to generate random value between 0.0 and 1.0.

LaneMay 25, 2020 15:39:19

Nice share. In test automation generate random email address is very important. For example if we want to automate a signup page every time we need to generate a unique email address to complete the signup process successfully.

PeytonMarch 10, 2017 3:33:35

Thanks for this, it really helped me a lot.

AyaanApril 15, 2020 14:01:46

I successfully generate random strings in C# Selenium. And you may refer it to generate random strings in C# for any input length.

public static string RandomGenerate(int length)
        {
            const string chars = "0123456789abcdefghijklmnopqrsqtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }



Leave a reply



Submit