Generate Random Email Address

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. In this section we will show how to generate random email address using selenium IDE.

There are two ways to generate random email address in selenium IDE.
    1. Using selenium script command.
    2. Using external user extension.

Using selenium script command:
 
           ➤ Here we use “Math.round(Math.random()*1357)” function to 
                generate random email address.

  Test Case-1:
Command
Target
Value
open
https://www.xoom.com/

clickAndWait
//div[@id='homeactionbtn']/div/a/span/span

storeEval
Math.round(Math.random()*1357)
random
type
id=j_username
selenium${random}@domain.com

If we run the following test case in several times we can observe that a randomly generated unique email is inputted automatically every time in the Email address field.

Using external user extension:

           ➤ Save the following code as ‘random_email.js’ file.

Selenium.prototype.doTypeRandomEmail = function(locator) {
   /**
   * Sets the value of an input field to a random email id,
   * as though you typed it in.
   *
   * @param locator an <a href="#locators">
element locator</a>
   */

   // All locator-strategies are automatically handled by "findElement"
   var element = this.page().findElement(locator);

   /* The following block generates a random email string */
   var allowedChars = "abcdefghiklmnopqrstuvwxyz";
   var stringLength = 8;
   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 += "@domainname.com"

   // Replace the element text with the new text
   this.browserbot.replaceText(element, randomstring);
};

        
          ➤ Now import the  ‘random_email.js’ file into selenium IDE.
                 Step-1: Open Selenium IDE.
                 Step-2: Click on Option.
                 Step-3: Select Option.
                 Step-4: Browse and open  ‘random_email.js’ file.
             
            

                    Step-5: Restart the selenium window.

        ➤ Here we showing a practical example of this user extensions.

Test Case-02:
Command
Target
Value
open
https://www.xoom.com/

clickAndWait
//div[@id='homeactionbtn']/div/a/span/span

typeRandomEmail
//*[@id='j_username']


If we run the following test case in several times we can observe that a randomly generated unique email is inputted automatically every time in the Email address field.

No comments:

Post a Comment