VB Script and QTP - Part3 | Quick Test Professional(QTP)
.
QTP Blog RSS

VB Script and QTP - Part3

This post is third in the VB Script and QTP series. For earlier posts on this series refer VB Script and QTP Part1 and VB Script and QTP Part2


Here we shall talk about user defined procedures.

Procedures are set of executable statements.

In VBScript, there are two types of procedures:

  1. Sub Procedures
  2. Function Procedures

Sub Procedures

A sub procedure is a series of VBScript statements, enclosed by Sub and End Sub statements which perform actions but do not return a value. A sub procedure can take arguments. If a sub procedure doesn’t receive any arguments, its Sub statement must include an empty parenthesis().

The following Sub procedure uses two intrinsic, or built-in, VBScript functions, MsgBox and InputBox , to prompt a user for information. It then displays the results of a calculation based on that information. The calculation is performed in a Function procedure created using VBScript. The Function procedure is shown after the following discussion.

Sub ConvertTemp()

temp = InputBox("Please enter the temperature in degrees 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 a sub procedure but it can return value to the calling function. A function procedure can take arguments (constants, variables or expressions that are passed to it by a calling procedure). If a function procedure has no arguments, it Function statement must include an empty set of parenthesis. A function returns a value by assigning a value to its name in one or more statements of the procedure. Since VBScript has only one base data type, a function always returns a variant.

In the following example, the Celsius function calculates degrees Celsius from degrees Fahrenheit. When the function is called from the ConvertTemp Sub procedure, a variable containing the argument value is passed to the function. The result of the calculation is returned to the calling procedure and displayed in a message box.

Sub ConvertTemp()
temp = InputBox("Please enter the temperature in degrees F.", 1)
MsgBox "The temperature is " & Celsius(temp) & " degrees C."
End Sub

Function Celsius(fDegrees)
Celsius = (fDegrees - 32) * 5 / 9
End Function
Tips:
  1. To get data out of a procedure, you must use a Function. Remember, a Function procedure can return a value; a Sub procedure can't.
  2. A Function in your code must always be used on the right side of a variable assignment or in an expression.
  3. To call a Sub procedure from another procedure, type the name of the procedure along with values for any required arguments, each separated by a comma. The Call statement is not required, but if you do use it, you must enclose any arguments in parentheses.
  4. The following example shows two calls to the MyProc procedure. One uses the Call statement in the code; the other doesn't. Both do exactly the same thing.
Call MyProc(firstarg, secondarg)

MyProc firstarg, secondarg

Notice that the parentheses are omitted in the call when the Call statement isn't used.
Related:
VB Script and QTP Part1

VB Script and QTP Part2

Some Useful Tips with QTP

Source: Microsoft VB Script Reference


If you want to keep track of further articles on QTP. I recommend you to subscribe via RSS Feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.

6 comments:

Anonymous said...

Hi Ankur.,What are the Certification Exams is QTP, Tell me the price and and venue where mercury people can conduct.

neeharika said...

Hi Ankur
The infor you provided on Vb script is very useful.Great Work
If you explain in detail about SetToProperty, SetRoProperty, GetToProperty scripting this kind of stuff ,that would be great

sowmya said...

hi Ankur...this sowmya...

I want guideness 4 u....now i doing project based on QTP tools...plz guide me...

when i recording the applications(web applications)it shows the error.........."Cannot find the "igbAdminIcon" object's parent "Home Page - Klefton University" (class Page). Verify that parent properties match an object currently displayed in your application."

i check in "object repository"...that object is recording..but its shows "Run Error"...

Anonymous said...

Hi Ankur

Could please tell whether it is possible to pass arguments to VBScript Sub or Func as optional and Null. What would be the syntax?

I could not find an answer to this from the references I've checked.

Thanks.

Anonymous said...

Hi Ankur,

I have used the webservice add-in for QTP. After providing the WSDL URL and completing the webservicing wizard it has automatically generated the script and when run that script providing the proper input its giving the error indicating that destination server not accepting the request let me know how can i over come this

Anonymous said...

Hi Folks,

I hear various answers for this question:

By which type the arguments are passed to a function and procedure?

By Val or By Ref

Which one is correct...Plzz clarify
Thanks.