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:
- Sub Procedures
- 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:
- 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.
- A Function in your code must always be used on the right side of a variable assignment or in an expression.
- 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.
- 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.
Cannot use parentheses when calling a Sub is a common error you may encounter while calling subroutines and functions in QTP. Check the article Cannot use parentheses when calling a Sub to understand the concepts.
You may refer the links below to go to earlier articles in this series
Hi Ankur,
the info provided is very much useful for begginers.
i liked the way you have presented.
Thanks,
Rajashekar Siddappa
Hi Ankur,
i have to compare two big numbes and get True or False, and the code should be optmal.
Please suggest: Str1=”098765432123456789098″
Str2=”09876543212345678909123_abcd_xyz12″
i have used Split for Str2 and took integer part and i have to compare this with Str1.
and i have tried and not succeded.Please help me on this.
Thanks,
Rajashekar
as per my understanding you are trying to match the number in str1 with the number in str2
Str1=”098765432123456789098″
Str2=”09876543212345678909123_abcd_xyz12″
s1=len(Str1)
i=instr(str2,0)
msgbox i
x1=mid(Str2,i,21)
Msgbox x1
if Str1=X1 then
msgbox “TRUE”
else
msgbox “FALSE”
end if
IF NOT PLEASE LET ME KNOW UR ACTUAL REQUIREMENT???
Thanks
Ranjan
Hi Ankur, Under Function Procedure, it is given that If a function procedure has no arguments, it Function statement must include an empty set of parenthesis. what does it mean can explain in brief with example.
I executed below code without braces then why it is still working fine? please reply to my mail id prasannakumar411@gmail.com
function testing
testing=45
End Function
temp=testing
msgbox temp
@Prasanna: Please have a look at Cannot use parenthesis while calling a sub.
How to find multiple occurrences of a specific character in a string(EX: AHSDDGDDBWIP) SHOULD RETURN DDDD ?
Hi..aravind..for that quea answ is
my “ansv.aravind@gmail.com”
Dim str,tempa
str=”AHSDDGDDBWIP”
tempa=split(str,”D”)
msgbox “count the no of D in string”&ubound(tempa)
pls correct me if am wrong
Hi,
How to find multiple occurrences of a specific character in a string(EX: AHSDDGDDBWIP) SHOULD RETURN DDDD ?
Please reply.
hi anonymous,
this is mahesh naidu, your doubt is how to call a libraries into qtp ……
here we follow two methods
1.global
2. local
here local libraries means we call a function with in the script i.e we call with in the action only
syntax: executefile “file Path”
2.Global libraries means we call a function in any action…
procedure:
open a qtp->goto file->settings->Resources->click on browser (+)button->select a librarry file->click on a check syntax button->click on ok
this is the procedure of calling a libraries files into QTP
i tried to call function libraries with using script the following syntax,
executefile “file path”
but it is not getting to work can you pls help anybody how to call the functional libraries dynamically with out using qtp
Thank you Ankur! I just can’t tell you how glad I am to have found your blog page. It has helped me alot and also cleared many of my doubts.
Thanks again!
Hi Ankur,
I am a manual tester and want to learn QTP scripting. I learned the Record and play strategy for QTP but here all the interviews are based on the scripting skills. As I am a beginner I am in a state of confusion from where to start.
Can you please help me how to start learning the scripting methods?
I would really appreciate for your help.
Thanks.
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.
By Ref – it is the default
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
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.
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”…
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
Hi Ankur.,What are the Certification Exams is QTP, Tell me the price and and venue where mercury people can conduct.