Using Selenium IDE with random values

前端 未结 12 1862
囚心锁ツ
囚心锁ツ 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:18

    Here another variation on the gmail example:

    <tr>
      <td>runScript</td>
      <td>emailRandom=document.getElementById('email');console.log(emailRandom.value);emailRandom.value=&quot;myEmail+&quot; + Math.floor(Math.random()*11111)+ &quot;@gmail.com&quot;;</td>
     <td></td>
    </tr>
    
    0 讨论(0)
  • 2020-12-04 08:19

    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.

    0 讨论(0)
  • 2020-12-04 08:21
    <tr>
    <td>store</td>
     <td>javascript{Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 8)}</td>
    <td>myRandomString</td>
    </tr>
    
    0 讨论(0)
  • 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:

    <tr>
        <td>randomString</td>
        <td>6</td>
        <td>x</td>
    </tr>
    <tr>
        <td>type</td>
        <td>username</td>
        <td>${x}</td>
    </tr>
    
    0 讨论(0)
  • 2020-12-04 08:28

    Here's a one-line solution to generating a random string of letters in JS:

    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("").filter(function(e, i, a) { return Math.random() > 0.8 }).join("")
    

    Useful for pasting into Selenium IDE.

    0 讨论(0)
  • 2020-12-04 08:28

    Selenium RC gives you much more freedom than Selenium IDE, in that you can:

    • (1) Enter any value to a certain field
    • (2) Choose any field to test in a certain HTML form
    • (3) Choose any execution order/step to test a certain set of fields.

    You asked how to enter some random value in a field using Selenium IDE, other people have answered you how to generate and enter random values in a field using Selenium RC. That falls into the testing phase (1): "Enter any value to a certain field".

    Using Selenium RC you could easily do the phase (2) and (3): testing any field under any execution step by doing some programming in a supported language like Java, PHP, CSharp, Ruby, Perl, Python.

    Following is the steps to do phase (2) and (3):

    • Create list of your HTML fields so that you could easily iterate through them
    • Create a random variable to control the step, say RAND_STEP
    • Create a random variable to control the field, say RAND_FIELD
    • [Eventually, create a random variable to control the value entered into a certain field, say RAND_VALUE, if you want to do phase (1)]
    • Now, inside your fuzzing algorithm, iterate first through the values of RAND_STEP, then with each such iteration, iterate through RAND_FIELD, then finally iterate through RAND_VALUE.

    See my other answer about fuzzing test, Selenium and white/black box testing

    0 讨论(0)
提交回复
热议问题