Search Here!

Wednesday, April 2, 2014

Get random string, email string and number in Protractor JS.

Get random string, email string and number in Protractor JS.

/**
* Usage: Return Random Email Id.
*/
exports.getRandomEmail = function () {
    var strValues = "abcdefghijk123456789";
    var strEmail = "";
    for (var i = 0; i < strValues.length; i++) {
        strEmail = strEmail + strValues.charAt(Math.round(strValues.length * Math.random()));
    }
    return strEmail + "@mymail.test";
};

/**
* Usage: Generate random string.
* characterLength :  Length of string.
* Returns : Random string.
*/
exports.getRandomString = function (characterLength) {
    var randomText = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    for (var i = 0; i < characterLength; i++)
        randomText += possible.charAt(Math.floor(Math.random() * possible.length));
    return randomText;
};

/**
* Usage: Generate random number.
* characterLength :  Length of number.
* Returns : Random number.
*/
exports.getRandomNumber = function (numberLength) {
    var randomNumber = "";
    var possible = "0123456789";
    for (var i = 0; i < numberLength; i++)
        randomNumber += possible.charAt(Math.floor(Math.random() * possible.length));
    return randomNumber;
};

2 comments:

  1. Thanks Manish for the posts. I just started using proctractor and the posts are really helpful. I have a question about the "exports". Is it a standard way of making the functions accessible through proctractor or is it just a naming convention? Thank You

    ReplyDelete
  2. exports is a node.js concept that declares the functions that your module makes available to code outside itself. It defines the module's interface.

    ReplyDelete