Forum Discussion

mjordan_lynda's avatar
mjordan_lynda
New Contributor
15 years ago

Useful samples for creating random/unique usernames, etc. Keyword test.

I'm looking for a couple useful samples to create random email addresses and usernames when I submit a form.



I'm looking for samples done in either javascript or C#.  



I have js functions which will do this in script mode.



My problem is that I want to take advantage of using keyword tests. 



When I select a form element to give it a parameter and select "code expression" it says the value of the expression must be one line.   So it appears I cannot simply paste the function into value field. 



This has to be one of the most common things a person tries to do when testing a website, so, like I said, I'm looking for some useful examples to do this when using the keywords test, without having to create a data-driven test.

9 Replies

  • Hi Mark,



    Here is a sample of how you can accomplish your task:



    1. Add the following routines to your project:




    function SetRandomEmailAddress() {


        Project.Variables.VariableByName("RandomEmailAddress") =


            RandomString(16) + "@" + RandomString(16) + "." + RandomString();


    }



    function RandomString(Length)


    {


        if(Length == null) {


            Length = 3;


        }


        var SymbolsAndDigits = "abcdefghijklmnopqrstuvwxyz1234567890";


        var Result = "";


        for(var i = 0; i < Length; i++)


        {


            Result += aqString.SubString(SymbolsAndDigits, Math.floor(Math.random() * SymbolsAndDigits.length), 1);


        }


        return Result;


    }


    2. Create a string project variable with "RandomEmailAddress" name. 


    3. Insert the Run Script Routine operation to your keyword test with the RandomEmailAddress routine as a parameter.


    4. After the operation is executed, the "RandomEmailAddress" variable's value will contain a random e-mail address. This variable can be accessed further in the keyword test.
  • Hi David,



    Thanks for the reply.  It totally works and I really appreciate the reply because I am still getting familiar with the finer points of project variables/local variables, scripts, etc,  but I don't doubt this will come in handy for other users because its a pretty common need for testing.  So...uh,  when's a version of the product coming out that will allow me to right click and insert a random value.  =) 



    - Mark
  • jsmith1979's avatar
    jsmith1979
    Occasional Contributor
    I second the request for inserting random strings. I find a constant need for it in my testing too.
  • I hate to bump an older thread, but it was a great help to me, and I wanted to say thanks.


    I also vote for a random string generator method for TC.

  • fishegl's avatar
    fishegl
    Occasional Contributor
    Add my vote for a random string generator.



    thanks.