There are two ways to generate random email in selenium IDE using javascript:

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

2. Use single line command in a selenium script.

1. Using a .js file and use as a command:
  • Paste the following code into a text file and rename it as ‘random_email.js’
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 += "@somedomain.com"

    // Replace the element text with the new text
    this.browserbot.replaceText(element, randomstring);
};
  • Now Import your custom command into selenium.

import_code

  • Once you have added the file to the options page you need to restart the Selenium IDE for the changes to take effect.

Whenever you add some new code or make changes to the exiting one in the ‘user-extensions.js’, you need to restart the Selenium IDE for the changes to take effect.

  • Now once you have restarted the IDE, you can see that your command has been added to the drop-down.

random_email_drop_down

  • Now you can use this command as you like. Here’s and example:

sel_user_1

2. Use single line command in a selenium script:

 Here is the single line code for random email generation.

storeEval “six.testemail+” + Math.floor(Math.random()*11111) + “@gmail.com”; email
type id=signup_email ${email}
type id=field_737 ${email}

In first line  the “storeEval” command is used for the javascript code in the middle column. and in the right most column “email” is used to store the generated random email into a variable called “email”.

Here one thing can be noted that storing is not necessary, I used it because I need to input the same random email into two different field that’s why I need to generate the random email and store it into a variable then type the stored value from variable to text fields, otherwise every time I use the js it generate different value so the two fields have different values and show error for mismatching.

P.S.

Random Text generation:

Here is another tips: We often need to use random text like random “username” or for something like that. We can easily make a custom command for this using our previous command. We just need to change like this:

Custom js code for random text generation:

// JavaScript Document
Selenium.prototype.doRandomText = 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 = 6;
 var randomstring = '';

 for (var i=0; i<stringLength; i++) {
 var rnum = Math.floor(Math.random() * allowedChars.length);
 randomstring += allowedChars.substring(rnum,rnum+1);
 }

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

Now we just need to save and import the js file to selenium and restart it. We should find the custom command “RandomText” in drop-down now.

source: 
http://www.codediesel.com/testing/adding-custom-commands-to-the-selenium-ide/
http://www.deanclatworthy.com/2011/01/dynamicrandom-javascript-values-in-selenium-ide/
http://stackoverflow.com/questions/161984/using-selenium-ide-with-random-values
Advertisements