Creating a Script to Generate Random Numbers

The first step is to create a script that generates a random number that you can use for the age of the user.

  1. Choose File > New.
    The New Asset dialog box opens.
  2. Select .NET Script from the asset types list, type function_randomAge in the Asset name text box and click OK.
    Tip: By naming your script "function" you can create an organized library of commonly used testing tasks from which you can quickly select and insert into a visual test.
    The script opens in the Code window.
  3. Place your cursor on the line following Sub Main(), and type:
    Dim rand As New Random()
    Dim TSrandomAge As Integer = rand.Next(10000, 99999)
    MsgBox(TSrandomAge)
    
  4. Click Playback on the toolbar. The Playback dialog box opens.
  5. Click OK. A message box opens and displays a randomly generated number between 10,000 and 99,999.
  6. Click OK. The Playback Complete dialog box opens.
  7. Click Go to .NET Script.
  8. Replace the numeric range parameters (10000, 99999) with the parameters MinVal and MaxVal. You can use these two parameters to set the range of the random number from the visual test that plays back this script instead of having to open the script and change the values. Type the following:
    Dim TSrandomAge As Integer = rand.Next(MinVal, MaxVal)
  9. To verify the script works properly, assign the parameters the following values after Sub Main():
    Dim MinVal=16
    Dim MaxVal=105
    
    Your script should look like the following code:
    Public Sub Main()
      Dim rand As New Random()
      Dim MinVal=16
      Dim MaxVal=105
      Dim TSrandomAge As Integer = rand.Next(MinVal, MaxVal)
    
      MsgBox(TSrandomAge)
    End Sub
  10. Click Playback on the toolbar. The Playback dialog box opens.
  11. Click OK. A message box appears and displays a randomly generated number between 16 and 105.
  12. Click OK. The Playback Complete dialog box opens.
  13. Click Go to .NET Script.
  14. To set the MinVal and MaxVal parameters using input parameters, perform the following steps:
    1. Modify the Main() sub to include the input parameters that you want to create.
      Public Sub Main(args As IDictionary(Of String, Object))
        Dim MinVal=args ("MinVal")
        Dim MaxVal=args ("MaxVal")
      where "MinVal" and "MaxVal" are the names of the input parameters that we will create in the next procedure.
    2. Modify the Main() sub to include the output parameters that you want to create.
      Public Sub Main(args As IDictionary(Of String, Object))
      		    	
        args ("TSrandomAge")=TSrandomAge
      where "TSrandomAge" is the names of the output parameters that we will create in the next procedure.
    3. Since the script will pass the age to the visual test, comment out the message box code by placing an apostrophe in front of the statement.
      'MsgBox(TSrandomAge)
    The entire script should look like the following:
    Public Module Main
    	Dim _desktop As Desktop = Agent.Desktop
    
    	Public Sub Main(args As IDictionary(Of String, Object))
    	
    		Dim rand As New Random()
    		
    		Dim MinVal= args("MinVal")
    		Dim MaxVal= args("MaxVal")
    		
    		Dim TSrandomAge As Integer = rand.Next(MinVal, MaxVal)
    		args ("TSrandomAge")=TSrandomAge
    		'MsgBox(TSrandomAge)
    				
    	End Sub
    End Module
    

Next, define the script input and output parameters used to pass the random age to the visual test.