How to generate a string of random letters in UFT/QTP

This week's function is extremely simple, but very effective. It is a function that will prevent many headaches when attempting to create dummy data. You may use it to generate user names, passwords, first names, and any other strings of characters that you desire. Just pass the length of the string that you want, and the function will generate a random string for you to use for your needs. Let us know what you think about this function.

Some real world uses:

  1. Create user names on demand for testing login
  2. Create passwords for testing login
  3. Create strings of different lengths to use for boundary conditions
  4. Generate unique strings
''' <summary>
''' QTPtutorial.net
''' Generates a string of random characters based on the integer passed to the function
''' </summary>
''' <param name="intNumberOfLetters" type=" integer	">	Represents the number of characters that you want to create into a random string 	</param>
''' <returns type="">	A string of random characters A-Z based on the number passed to the function 	</returns>
''' <example>
''' fn_getRandomLetters(8)
''' </example>
Function fn_getRandomLetters(intNumberOfLetters)
   Dim i
   'allows the RND function to always generate a random value
   Randomize
   For i = 1 To intNumberOfLetters	'Loop from 1 until however many letters are specified by the parameter
	   'return an character every single time we loop through
	   '90 - 65 means (Z - A) in ASCII
	fn_getRandomLetters = fn_getRandomLetters & Chr(Int((90-65 + 1) * Rnd + 65))
   Next
End Function
Watch Video

 

 

Pin It on Pinterest

Clef two-factor authentication