Thursday, 20 February 2014

How to create random strings in QTP?

Sometimes we need to create the test data randomly  in the form of strings (numerical, alphanumerical and alphabetical) at run time. I have created one function that will give you random strings.

'to get only alphabetical strings, pass 1 as input
print getRandomString(1)

'to get only alphanumerical strings , pass 2 as input
print getRandomString(2)

'to get only numerical strings , pass 3 as input
print getRandomString(3)

Function getRandomString(byval intType)

stamp = replace(replace(formatdatetime(now),":","-"),"/","")
stamp = replace(stamp,"-","")
stamp = replace(stamp," ","")

If intType = 1 Then
'only alphbetical
For i=1 to 8
Randomize 
x = x & chr( Int((90-65+1)*Rnd+65))
Next

elseIf intType = 2 Then
' alphanumeric
x = "AN" + stamp

elseIf intType = 3 Then
' only digits
x  = stamp

End If
        getRandomString = x

End Function

All the strings will be unique as they are based on time stamp.
Some of the testing scenarios where you will need such function are given below.

  1. Some times users are allowed to save the profile settings which can be loaded later on. In this case, we need to create such profile names that are unique. 
  2. Some times we need to create the entity names which are unique. For example - creating the group Ids, User Ids etc.


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Please Leave your reply. We value your feedback and inputs

Best QTP Books

Everything About QTP

Hello Friends,
You can find QTP study material, Multiple choice questions (mcq), QTP question bank, QTP question papers, QTP notes, QTP questionnaire, scenario based QTP interview questions, QTP tutorial and QTP training on this site.

If you are a fresher or experienced QTP professional with (1/2/3/4) years of experience, this blog is just for you.