11. Practical Work ! 12/29/10 For more QTP Scripts, www.ramupalanki.com
12.
13.
14.
15. Practical Work ! 12/29/10 For more QTP Scripts, www.ramupalanki.com
16.
17.
18. Practical Work ! 12/29/10 For more QTP Scripts, www.ramupalanki.com
19.
20.
21.
22.
23. Practical Work ! 12/29/10 For more QTP Scripts, www.ramupalanki.com
24.
25. Practical Work ! 12/29/10 For more QTP Scripts, www.ramupalanki.com
26.
27.
28.
29. Practical Work ! 12/29/10 For more QTP Scripts, www.ramupalanki.com
30.
31.
32.
33.
34. Practical Work ! 12/29/10 For more QTP Scripts, www.ramupalanki.com
35.
36. Practical Work ! 12/29/10 For more QTP Scripts, www.ramupalanki.com
37. Thank You ! 12/29/10 For more QTP Scripts, www.ramupalanki.com
Editor's Notes
A variable is a "container" for information you want to store. A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value. In VBScript, all variables are of type variant , that can store different types of data.
************** Code for Variable ************************** Option Explicit Dim var_x var_x=1 msgbox("Value in X is:" &var_x) ************** Code for Constant ************************ Const x=20 msgbox("The Constant value is: " & x)
******* Code of Single Dimensional Array ********************* Dim names(2) names(0)="Mahboob" names(1)="Adeel" names(2)="Usman" msgbox("Array 0 Index Value is:" &names(0)) msgbox("Array 1st Index Value is:" &names(1)) msgbox("Array 2nd Index Value is:" &names(2)) ******* Code of Multi Dimensional Array ********************* Dim Two_Dim(1,1) Two_Dim(0,0) = "first row first column" Two_Dim(0,1) = "first row second column" Two_Dim(1,0) = "second row first column" Two_Dim(1,1) = "second row second column" msgbox(" Values in the row: " & Two_Dim(0,0) & " - " & Two_Dim(0,1) & " - " & Two_Dim(1,0) & " - " & Two_Dim(1,1) )
Dim num1,num2,result Sub add() num1= 10 num2 = 20 result=num1 + num2 msgbox ("The Result is:" & result) End Sub Call add()
********** Code for Procedure without Arguments ******************** To be performed on QTP. Example is already created in QTP Dim num1,num2,result Sub add() num1= 10 num2 = 20 result=num1 + num2 msgbox ("The Result is:" & result) End Sub Call add() *********** Code for Procedure With Arguments ********************** Dim arg1,arg2,result Sub add(arg1,arg2) result=arg1 + arg2 msgbox("The Result for Passed Values in procedure is:" & result) End Sub call add (30,20) *********** Code for Procedure With Arguments ( after user Input)********************** Dim input1,input2,result input1=inputbox("Please Enter First Value") input2=inputbox("Please Enter Second Value") Sub addition(input1,input2) result=int(input1)+int(input2) msgbox("The Result is:" & result) End Sub Call addition(input1,input2)
' *********************Creating a Function without Arguments********************************** Function addition() Dim val1,val2,result val1=50 val2=50 result=val1+val2 addition=result End Function msgbox("The Result is:" &addition)
********** Code for Function without Arguments ******************** Function addition() Dim val1,val2,result val1=50 val2=50 result=val1+val2 addition=result End Function msgbox("The Result is:" &addition) ********** Code for Function with Arguments ************************************** Function addition(val1, val2) Dim result result =val1 + val2 addition =result End Function Dim temp temp = addition(5,7) msgbox("Result of addition: " & temp) ********** Code for Function with Arguments by getting User Input ******************** Dim arg1,arg2 arg1=inputbox("Please enter first value: ") arg2=inputbox("Please enter second value: ") Function addition(val1, val2) Dim result result =int(val1) + int(val2) addition =result msgbox("Result of addition: " & addition) End Function addition arg1,arg2
**********************Code for simple If condition******************* Dim user_input user_input = inputbox(&quot;Enter any value less than 10 to execute If statement code &quot;) If user_input < 10 Then msgbox(&quot;Code in if statement is executed......!&quot;) End If
************************* Code for If - Else condition ******************************** Dim user_input user_input = inputbox(&quot;Please enter value (if condition will execute incase entered value is less than 10) : &quot; ) If user_input<10 Then msgbox (&quot;IF statement executed&quot;) else msgbox (&quot;Else statement executed&quot;) End If ************************* Code for If - Else condition********************************** Dim user_input user_input = inputbox(&quot;Please enter value (if condition will execute incase entered value is less than 10) : &quot; ) If user_input<10 Then msgbox (&quot;IF statement executed&quot;) else msgbox (&quot;Else statement executed&quot;) End If ************************* Code for If – Elseif-Else condition***************************** Dim user_input user_input = inputbox(&quot;Please Enter Value to be comapred with 10 : &quot; ) If user_input<10 Then msgbox (&quot;IF statement executed&quot;) elseif user_input=10 then msgbox (&quot;ElseIF statement executed&quot;) else msgbox(&quot;Else Statement is Executed&quot;) end if
************************* Code for Select Case ******************************** Dim user_input user_input=inputbox(&quot;Please Enter Payment Method i.e. Check/Cash/CC&quot;) Select Case user_input case &quot;Check&quot; msgbox(&quot;You Entered Check&quot;) Case &quot;Cash&quot; msgbox(&quot;You Entered cash&quot;) Case &quot;CC&quot; msgbox(&quot;you entered Credit Card&quot;) Case Else msgbox(&quot;Payment method not recognized&quot;) End Select
************************* Code For Loop******************************** Dim i For i=0 to 10 msgbox (&quot;Loop Iteration is:&quot; &i) Next ****************** Code for For Loop with step keyword******************************** Dim i For i=20 to 10 step -2 msgbox (&quot;Loop Iteration is:&quot; &i) Next ****************** Code for For Loop with Each Keyword******************************** Dim var Dim arr(2) arr(0)=&quot;Ali&quot; arr(1)=&quot;Naseer&quot; arr(2)=&quot;Adeel“ For each var in arr msgbox(&quot;The Stored value in Array was:&quot; & var) Next
************************* Code Do-While Loop******************************** Dim x x=20 Do while x >10 msgbox(&quot;Value in X is Less Greater than: 10&quot;) x=x-1 Loop /***************************Do –While (condition in end)*************************** Dim x x=20 Do msgbox(&quot;Value in X is Less Greater than: &quot;&x) x=x-1 Loop while x >10 /******************************** Do Until Loop**************************************** ' Code for do - until loop Dim x x= 30 Do until x < 20 msgbox(&quot;Value in X is:&quot; & x) x=x-1 Loop /************************* Code for do-until loop (condition in the end) ***************************** Dim x x= 30 Do msgbox(&quot;Value in X is:&quot; & x) x=x-1 Loop until x < 20
************************* Built In functions ******************************** **************** System date ********************************************* 'msgbox(&quot;Today's date(mm/dd/yyyy): &quot;& date) **************** System time ********************************************* 'msgbox(&quot;Today's date(mm/dd/yyyy): &quot;& time) *************** random number generated between 0 and 1 ******************** 'For i=0 to 10 'msgbox(&quot;Random Number: &quot; & rnd ) 'Next **************** Search occurrence of In String from the full string **************************** 'Dim full_string,search_string,result 'full_string = inputbox(&quot;Enter the complete string: &quot;) 'search_string = inputbox(&quot;Search string value: &quot;) 'result = InStr(full_string,search_string) 'msgbox(&quot;Full String: &quot;&full_string&chr(13)&&quot;Search string: &quot;&search_string&chr(13)& &quot;Search string value: &quot;&result)