Forum Discussion

rcash's avatar
rcash
New Contributor
15 years ago

Random value using Keyword test

I've got an Excel worksheet with 500 customer IDs under one column. I'd like for TestComplete to randomly choose one of the IDs to use when the customer logs in. How is that accomplished using keyword tests?

4 Replies

  • Hi Robert,



    You can use one of the three approaches described in the Working With Microsoft Excel Files help topic to read data from Excel files. As for generating random values, you'll need to use a short script for that and call the script from your keyword test. For example, if your project's scripting language is VBScript (default), the script code will look like this:




    Function getRandomNumber


      ' Generate random number from 0 to 500


      getRandomNumber = Round(rnd() * 500)


    End Function




    The function can be called from your keyword test via the Run Script Routine operation.
  • rcash's avatar
    rcash
    New Contributor
    Hello Allen,

    When I try to call the script I'm getting syntax errors ...



    Syntax error(s)

    At "*500)

    End Function

    "

    error 110: SYNTAX ERROR while lexing character "y",



    Error location:

    Unit: "OLO\OLO-SU.COM\Unit2"

    Line: 3 Column 3.



    The "y" in the error 110 line actually has a horizontal : over it; I couldn't figure out a way to type it.



    Any ideas?

    Thanks,

    Bob
  • rcash's avatar
    rcash
    New Contributor
    Allen,

    Please ignore the last post. I was able to get the random number generated using jscript. My only problem now is how to get the randomly generated number to be used in selecting that particular row number in the Excel file.

    Regards,

    Bob

  • Hi Bob,





    You can arrange a loop for that purpose - count the number of Excel values enumerated until the number equals the needed value. If you face some problems when implementing this approach, please post some images demonstrating the problem here.





    If you are working with Excel via COM, you can also specify the needed coordinates explicitly via Cells(N, 1), where N is the number of the needed row.