Using Selenium IDE with random values

前端 未结 12 1879
囚心锁ツ
囚心锁ツ 2020-12-04 07:49

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 w

12条回答
  •  感情败类
    2020-12-04 08:24

    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 ] = 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:

    
        randomString
        6
        x
    
    
        type
        username
        ${x}
    
    

提交回复
热议问题