QTP Control Flow Examples



11.1 read a number and verify that number Range weather in between 1 to 100 or 101 to 1000?


Option explicit
Dim a,x
a=Inputbox ("Enter a Value")
a=cdbl(a)
If a<= 100 Then For x= 1 to 100 If a=x Then msgbox "a is in between 1 to 100 range" End If Next else For x= 101 to 1000 If a=x Then msgbox "a is in between 101 to 1000 range" End If Next End If
11.1 read Data and find that data size, If size <>4 then display invalid data message, if data size = 4 then verify “a” is there or not in that data?


Dim x
x=Inputbox ("Enter 4 digit value")
x1=Right(x,1)
x2=Left (x,1)
x3=mid (x,2,Len(1))
x4=mid (x,3,Len(1))
y=len(x)
If y=4 Then
If x1="a" or x2="a" or x3="a" or x4="a" Then
msgbox "a is there"
else
msgbox "a is Not there"
End If
else
msgbox "Invalid Data"
End If

VB Script Procedures


In VBScript, there are two kinds of procedures available; the Sub procedure and the Function procedure.

11.1 Sub Procedures

A Sub procedure is a series of VBScript statements (enclosed by Sub and End Sub statements) that perform actions but don't return a value.

A Sub procedure can take arguments (constants, variables, or expressions that are passed by a calling procedure).

If a Sub procedure has no arguments, its Sub statement must include an empty set of parentheses ().

Syntax:
Sub Procedure name ()
Statements
-----------
-----------
End Sub
Or
Sub Procedure name (argument1, argument2)
Statements
-----------
-----------
End Sub

Example: 1

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

Example: 2

11.2 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 can also return a value.

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, its Function statement must include an empty set of parentheses.

A Function returns a value by assigning a value to its name in one or more statements of the procedure. The return type of a Function is always a Variant.

Syntax:
Function Procedure name ()
Statements
-----------
-----------
End Function
Or
Function Procedure name (argument1, argument2)
Statements
-----------
-----------
End Function

Example: 1

Function Celsius(fDegrees)
Celsius = (fDegrees - 32) * 5 / 9
End Function

Example: 2

Function cal(a,b,c)
cal = (a+b+c)
End Function

11.3 Getting Data into and out of Procedures

o Each piece of data is passed into our procedures using an argument.
o Arguments serve as placeholders for the data we want to pass into our procedure. We can name our arguments any valid variable name.
o When we create a procedure using either the Sub statement or the Function statement, parentheses must be included after the name of the procedure.
o Any arguments are placed inside these parentheses, separated by commas.

11.4 Using Sub and Function Procedures in Code

A Function in our code must always be used on the right side of a variable assignment or in an expression.

For example:
Temp = Celsius(fDegrees)
-Or-
MsgBox "The Celsius temperature is " & Celsius(fDegrees) & " degrees."

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.

Post a Comment

Previous Post Next Post