views:

5388

answers:

4

Is it possible to create Selenium tests using the Firefox plugin that use randomly generated values to help do regression tests?

The full story: I would like to help my clients do acceptance testing by providing them with a suite of tests that use some smarts to create random (or at least pseudo-random) values for the database. One of the issues with my Selenium IDE tests at the moment is that they have predefined values - which makes some types of testing problematic.

+13  A: 

First off, the Selenium IDE is rather limited, you should consider switching to Selenium RC, which can be driven by Java or Perl or Ruby or some other languages.

Using just Selenium IDE, you can embed JavaScript expressions to derive command parameters. You should be able to type a random number into a text field, for example:

type fieldName javascript{Math.floor(Math.random()*11)}

Update: You can define helper functions in a file called "user-extensions.js". See the Selenium Reference (all the way down, in Extending Selenium)

Thilo
I use RC for my own testing, but in this case I would like to give my client some assisted tests they can run through in their browser (without having to have a copy of the dev environment).
Toby Hede
Just thinking ... do you know if Selenium can call Javascript functions embedded in the page? I could build some functions to generate some values.
Toby Hede
I had to make sure to use the javascript as my value as a separate command. I found out that I could not mix literal text with the javascript in the same command. Hence, mine looked like this:__type fieldName Auto-generated user typeKeys fieldName javascript{Math.floor(Math.random()*11)}
Jared
+1  A: 

You can add user exentions.js to get the random values .

Copy the below code and save it as .js extension (randomgenerator.js) and add it to the Selenium core extensions (SeleniumIDE-->Options--->general tab)

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 ] randomString= 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;
}

Way to use

Command                Target     Value
-----------           ---------   ----------
randomString           6           x
type                username       ${x}

Above code generates 6 charactes string and it assign to the variable x

Code in HTML format looks like below:

<tr>
    <td>randomString</td>
    <td>6</td>
    <td>x</td>
</tr>
<tr>
    <td>type</td>
    <td>username</td>
    <td>${x}</td>
</tr>
RajendraChary
the line with the `default` looks like a syntax error.
Geo
A: 

hi thx for your code :)

Note:

In your switch is the default case not right. It should not be:

default: storedVars[ varName ] randomString= randomAlphaNumeric( length );

It must be: default: storedVars[ varName ] = randomAlphaNumeric( length );

I think

K.B.
This is an English-only website.
ryeguy
A: 

(Based on Thilo answer) You can mix literals and random numbers like this:

javascript{"joe+" + Math.floor(Math.random()*11111) + "@gmail.com";}

Gmail makes possible that automatically everything that use aliases, for example, [email protected] will go to your address [email protected]

Multiplying *11111 to give you more random values than 1 to 9 (in Thilo example)

corbacho