PROCEDURES
Procedures are a set of statements that are enclosed within a group. These groups of statements perform a specific task. Once a procedure is defined it can be called from different locations in the programs as many times as required by the program. The main purpose of using procedure is to maintain modularity and re usability. In VBScript there are two types of procedures, the Sub procedure and the Function procedure.
Advantages of Procedures:
1. Modularity: By dividing our scripts into procedures we can maintain and write programs by segregating the code into smaller and manageable blocks.
2. Reusable: Procedures help us to reduce the number of lines of the code by reusing the code.
3. Generalization: Once a procedure is defined it can be used by different tests.
4· Recursive: Procedures can call themselves any number of times.
Sub Procedures:
Sub procedure is a series of VBScript statements, enclosed by Sub and End Sub statements. The Sub procedure do not return any value. Procedures use two built-in function. MsgBox and InputBox, to prompt a user for some information. It then displays the results of a calculation based on that information.
Ex:
Sub convertTEMP()
temp= InputBox("Please enter the temperature in degree F",1)
MsgBox " The temperature is " &Celsius(temp) & "degrees C"
End Sub
Function Procedures:
temp= InputBox("Please enter the temperature in degree F",1)
MsgBox " The temperature is " &Celsius(temp) & "degrees C"
End Sub
Function Procedures:
A Function procedure is a series of VBScript statements enclosed by the Function and End Function statements. A Function Procedure is similar to to a Sub procedure but the only difference is that it can also return a value. A Function returns a value by assigning a value to its name. The return type of a Function is always a Variant.
Ex:
Function Celsius(fDegrees)
Celsius= (fDegrees - 32) * 5/9
End Function
Scope of Procedures:
A Procedure can be scoped as private,public or default. Procedures are public by default.
Function Celsius(fDegrees)
Celsius= (fDegrees - 32) * 5/9
End Function
Scope of Procedures:
A Procedure can be scoped as private,public or default. Procedures are public by default.
·Public: Indicates that the procedure is accessible to all other procedures in all scripts.
·Private: indicates that the procedure is accessible only to other procedures within the script where it is declared.
·Default: Used only with public keyword in a class block to indicate that the Function procedure is the default method for the class. An error occurs if more than Default procedure is specified in a class.
Call Statement
Call Statement is used to pas program execution control to procedure. Procedure can be called without using a call statement. In such scenarios whenever the program encounters a procedure name it will automatically pass control to the procedure. However , if you use the Call keyword to call a procedure that requires arguments, argument list must be enclosed in parentheses. if you omit the Call keyword from the procedure call, you must also omit the parentheses around argument list.
Call Statement is used to pas program execution control to procedure. Procedure can be called without using a call statement. In such scenarios whenever the program encounters a procedure name it will automatically pass control to the procedure. However , if you use the Call keyword to call a procedure that requires arguments, argument list must be enclosed in parentheses. if you omit the Call keyword from the procedure call, you must also omit the parentheses around argument list.
example: Call MyProc() or MyProc
Exit Statement
The code within a procedure is executed until the last line of the procedure i.e. End statement is encountered. In instances where the user wants to exit the procedure when certain conditions are met, exit statements come into use. Exit statements when encountered within a procedure body exits the procedure unconditionally.
Syntax: Exit [Procedure]
Exit Statement
The code within a procedure is executed until the last line of the procedure i.e. End statement is encountered. In instances where the user wants to exit the procedure when certain conditions are met, exit statements come into use. Exit statements when encountered within a procedure body exits the procedure unconditionally.
Syntax: Exit [Procedure]
Ex1:
'Private Function Login()
'With Dialog("Login")
' .Activate
' .WinEdit("Agent Name:").Set "asdf"
' .WinEdit("Password:").SetSecure "50a4555990d2a64d922fab6f0c8767453ab1b2ed"
' .WinButton("OK").Click
'End With
'End Function
'
'Call Login()
Public Function add2()
x = InputBox("enter the data")
msgbox x
End Function
Call add2()
Function Procedures:
==============
Function login()
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "fasdf"
Dialog("Login").WinEdit("Password:").SetSecure "5041b1a7cb12d51ed0f8ab93b260f7818b205d48"
Dialog("Login").WinButton("OK").Click
End Function
Function Open_Order()
Window("Flight Reservation").WinMenu("Menu").Select "File;Open Order..."
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set "4"
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
End Function
Call login()
Call Open_Order()
Sub Procedures:
===========
----------------------------------------------------------------------------------------------------------
'Sub procedure with no Arguments
Sub Login()
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "asdf"
Dialog("Login").WinEdit("Password:").SetSecure "4ecc5c599022dca4bd0ec109cf24af7aef23e511"
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
End Sub
Call Login()
----------------------------------------------------------------------------------------------------------------
'Sub procedure with Arguments
Sub Login(Agent, Password)
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set Agent
x=Crypt.Encrypt ("mercury")
Dialog("Login").WinEdit("Password:").SetSecure x
Wait 1
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
End Sub
Call Login("Hyderabad", x)
---------------------------------------------------------------------------------------------------------------
'Sub procedure with Arguments, Verification points and error handling
Sub Login(Agent, Password)
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set Agent
Dialog("Login").WinEdit("Password:").SetSecure Password
Dialog("Login").WinButton("OK").Click
If Window("Flight Reservation").Exist(10) Then
Window("Flight Reservation").Close
Msgbox "Login Successful"
Else
SystemUtil.CloseDescendentProcesses
Msgbox "Login Failed"
End If
End Sub
Call Login("Hyd","mercury")
-----------------------------------------------------------------------------------------------------------
'External Sub procedure (Loading Object Repositories at Run-time)
Sub Login(Agent, Password)
RepositoriesCollection.Add "C:\Documents and Settings\Administrator\Desktop\Login.tsr"
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set Agent
Dialog("Login").WinEdit("Password:").SetSecure Password
Dialog("Login").WinButton("OK").Click
If Window("Flight Reservation").Exist(10) Then
Window("Flight Reservation").Close
Msgbox "Login Successful"
Else
SystemUtil.CloseDescendentProcesses
Msgbox "Login Failed"
End If
RepositoriesCollection.RemoveAll
End Sub
------------------------------------------------------------------------------------------------
'Sub Procedure for Login Operation Data Driven Testing
Sub Login(Agent, Password)
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set Agent
Dialog("Login").WinEdit("Password:").Set Password
Wait 1
Dialog("Login").WinButton("OK").Click
If Window("Flight Reservation").Exist(10) Then
Window("Flight Reservation").Close
Datatable("Result",1)="Login Successful"
Else
SystemUtil.CloseDescendentProcesses
Datatable("Result",1)="Login Failed"
End If
End Sub
----------------------------------------------------------------------------
Call Login (DataTable("Agent",1), Datatable("Pwd",1))
-------------------------------------------------------------------------------------------
'Sub Procedure for Login Operation Data Driven Testing
Function Login(Agent, Password)
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set Agent
Dialog("Login").WinEdit("Password:").Set Password
Wait 1
Dialog("Login").WinButton("OK").Click
If Window("Flight Reservation").Exist(10) Then
Window("Flight Reservation").Close
Login="Login Successful"
Else
SystemUtil.CloseDescendentProcesses
Login="Login Failed"
End If
End Function
res= Login ("abcd","mercury")
Msgbox res
No comments
Post a Comment