First example of Random Numbers:
When you define parameters for an action you can set the parameter's value as Random numbers. There are many different ways in which you can use Random numbers in QTP. Lets jump at the examples straightway.
Example 1:
1. Open a new test.
2. In the keyword View right-click on Action1, select Action Properties, 'Action Properties' window opens, Go to 'Parameters' Tab.
3. In the 'Input Parameters' area click on the '+' sign and enter the Name of the input parameter as 'a', Type as Number, and Default value as 1. Click Ok.
4. Again right-click on Action1 in the keyword View , select 'Action Call Properties', 'Action Call Properties' window opens. Go to 'Parameter Values' Tab.
5. Make a single click under 'Value' column in the 'Input Parameter's' area, it will become a button '<#>'. Click on this button.
6. It will open 'Value Configuration Options' window.
7. Click on 'Parameter' radio button, select 'Random Number' from the dropdown. In the Numeric Range enter 0 against From and 100 against To.
8. Click on Name Checkbox and choose arg_a from the dropdown.
9. In the 'Generate New Random Number' area, select first option-For each action iteration. Click ok.
10. Again Click ok to come out of 'Action Call Properties' window.
11. Now Go to Insert (Menu)-> Call to New Action, 'Insert Call to New Action' window opens, click ok to insert a new action- action2.
12. Go to Expert view of action1 and type:
msgbox "action1"
msgbox(parameter("a"))
13. Go to Expert View of Action2 and type:
For i=1 to 3
RunAction "Action1", oneIteration, RandomNumber("arg_a")
Next
14. When you copy the above text to Expert View of Action2, it will show you a message that it has made Action1 Reusable, just click ok.
15. Now Run the test.
(It would be better if you run it by activating the expert view, then it will show you which step it is currently running by pointing to that particular step with yellow color arrow and then you will be able to understand it in a better way. You will see that it shows a different value in each msgbox() because we selected 'For each action iteration' from the 'Generate new random number' area. If we select the second option 'For each test iteration' then a message
box will show same values, but different values if you run it next time i.e. a different value at each test run.)
RandomNumber is an Object.
RandomNumber(ParameterNameOrStartNumber [,EndNumber])
EndNumber is optional above.
Second example of Random Numbers:
Here is another way of generating random numbers:
Open a new test and in the Expert view write these lines and run the test:
For i=1 to 5
var1=RandomNumber (0, 100)
msgbox(var1)
Next
Third example of Random Numbers: (This is more or less same as the first one)
One more way is to define a Random Number parameter in the 'Parameter Options' or 'Value Configuration Options' dialog box.
1. Open a new test.
2. In the keyword View right-click on Action1, select 'Action Properties', 'Action Properties' window opens, Go to 'Parameters' Tab.
3. In the 'Input Parameters' area click on the '+' sign and enter the Name of the input parameter as 'a', Type as Number, and Default value as 1. Click Ok.
4. Again right-click on Action1 in the keyword View , select 'Action Call Properties', 'Action Call Properties' window opens. Go to 'Parameter Values' Tab.
5. Make a single click under 'Value' column in the 'Input Parameter's' area, it will become a button '<#>'. Click on this button.
6. It will open 'Value Configuration Options' window.
7. Click on 'Parameter' radio button, select 'Random Number' from the dropdown. In the Numeric Range enter 0 against From and 100 against To.
8. Click on 'Name' Checkbox and choose arg_a from the dropdown.
9. In the 'Generate New Random Number' area, select first option-For each action iteration. Click ok.
10. Again Click ok to come out of 'Action Call Properties' window.
11. Now in the Expert View of action1 type:
x=RandomNumber("arg_a")
msgbox(x)
12. And Run the Test.
Fourth example of Random Numbers:
Another VBScript method of generating a random number:
For i= 1 to 3
var1 = int((101*rnd)+0) ' Generate random value between 0 and 100.
MsgBox var1
next
Let's talk about Randomize and Rnd for some time:
Randomize [number]
We use a number with Randomize to initialize the Rnd function's random-number generator, giving it a new seed value. If the number is omitted, the value returned by the system timer is used as the new seed value. In simple terms Rnd is a function and Randomize is used to initialize this function.
If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called.
No matter how many times you Run the below code it generates the same values:
For i= 1 to 3
randomize(2)
var1 = Int((6 * Rnd) + 1) ' Generate random value between 1 and 6.
MsgBox var1
next
But if you omitt randomize(2) from the above code and instead put only randomize then at each run it generates different values.
For i= 1 to 3
randomize
var1 = Int((6 * Rnd) + 1) ' Generate random value between 1 and 6.
MsgBox var1
next
Some light on Rnd:
The following formula is used to produce a random number in a given range:
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
likewise
Int((6 * Rnd) + 1) ' Generate random value between 1 and 6.
The Rnd function returns a value less than 1 but greater than or equal to 0.
Rnd(number)If the number is Less than zero (< 0) then Rnd generates 'The same number' every time, using number as the seed.
For i= 1 to 3
x=rnd(-1)
msgbox(x)
Next
If the number is Greater than zero(> 0) then Rnd generates 'The next random' number in the sequence.
For i= 1 to 3
x=rnd(1)
msgbox(x)
Next
If the number is Equal to zero (=0)then Rnd generates 'The most recently generated' number.
For i= 1 to 3
x=rnd(0)
msgbox(x)
Next
If the number is Not supplied then Rnd generates 'The next random number in the sequence.'
For i= 1 to 3
x=rnd()
msgbox(x)
Next
Remember:
For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.
Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.
Also See:
QTP SystemUtil Vs InvokeApplication
QTP Optional Step
QTP Relative Path
QTP Crypt Object