Generate random text in a variable

Create a variable that has randomly generated characters.

randomString options varName

Use varName as you would any variable (e.g., $Unknown macro: {varName})

'options' can be nothing, a number, one of the words 'alpha', 'numeric' or 'alphanumeric' or a pipe-delimited string containing either of those.

'8|alpha' and 'alpha|8' both do the same thing.

// Generate random text for a variable
// Possible options:
//   length      number indicating how long to make the string (defaults to 8)
//
//   type        string indicating what type of string to create alpha, numeric
//               or alphanumeric (defaults to alphanumeric)
//
//   length|type pipe delimited option list

Selenium.prototype.doRandomString = function( options, varName ) {

    var length = 8;
    var type   = 'alphanumeric';

    var o = options.split( '|' );

    for ( var i = 0 ; i < 2 ; i ++ ) {
        if ( o[i] && o[i].match( /^\d+$/ ) )
            length = o[i];

        if ( o[i] && o[i].match( /^(?:alpha)?(?:numeric)?$/ ) )
            type = o[i];
    }

    switch( type ) {
        case 'alpha'        : storedVars[ varName ] = randomAlpha( length ); break;
        case 'numeric'      : storedVars[ varName ] = randomNumeric( length ); break;
        case 'alphanumeric' : storedVars[ varName ] = randomAlphaNumeric( length ); break;
        default             : storedVars[ varName ] = randomAlphaNumeric( length );
    };
};

function randomNumeric ( length ) {
    return generateRandomString( length, '0123456789'.split( '' ) );
}

function randomAlpha ( length ) {
    var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split( '' );
    return generateRandomString( length, alpha );
}

function randomAlphaNumeric ( length ) {
    var alphanumeric = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split( '' );
    return generateRandomString( length, alphanumeric );
}

function generateRandomString( length, chars ) {
    var string = '';
    for ( var i = 0 ; i < length ; i++ )
        string += chars[ Math.floor( Math.random() * chars.length ) ];
    return string;
}

File: 

AttachmentSize
Plain text icon generateRandomTextInVariable.txt1.76 KB

Type: 

User-Extension