Upload
ramu-palanki
View
135
Download
0
Tags:
Embed Size (px)
Citation preview
VB ScriptVB Script
04/15/23For more QTP Scripts, www.ramupalanki.com
• VBScript is a scripting language
•Scripting language is a lightweight programming language
•VBScript is a light version of Microsoft's programming language Visual Basic
•VBScript can be used for both client-side and server-side programming.
04/15/23For more QTP Scripts, www.ramupalanki.com
VBScript VariablesVBScript VariablesA variable is a "container" for information you want to store.
Naming Rules: Must begin with an alphabetic character Cannot contain a period Cannot exceed 255 characters Must be unique within its scope
04/15/23For more QTP Scripts, www.ramupalanki.com
Lifetime OF VariablesLifetime OF VariablesLocal Variables:
When you declare a variable within a procedure, the variable can only be accessed within that procedure. When the procedure exits, the variable is destroyed. You can have local variables with the same name in different procedures, because each is recognized only by the procedure in which it is declared.
Global Variables:If you declare a variable outside a procedure, all the procedures
on your page can access it. The lifetime of these variables starts when they are declared, and ends when the script is closed.
04/15/23For more QTP Scripts, www.ramupalanki.com
Variable (Cont)Variable (Cont)Declaration:
Dim Keyword is used to Declare a variableYou can Assign a Value to variable directly (without
declaring a variable). But not a good practice.Option Explicit Keyword is used to restrict
variables to be declared before their usage.Example:
Option explicitDim var_ xvar_x=1
04/15/23For more QTP Scripts, www.ramupalanki.com
ConstantsConstantsConst keyword is used to declare constants.It is necessary to initialize the Constant
during its declaration. You cannot change the value of constants in
later script.
Syntax:const x=1
04/15/23For more QTP Scripts, www.ramupalanki.com
Practical Work !Practical Work !
04/15/23For more QTP Scripts, www.ramupalanki.com
ArraysArrays
An array is a set of variables conveniently packages for easy handling
Sometimes you want to assign more than one value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable
04/15/23For more QTP Scripts, www.ramupalanki.com
Arrays (cont.)Arrays (cont.)The declaration of an array variable uses
parentheses ( ) following the variable name.
Example:
dim names(2)
names(0)=“Ali“names(1)=“Imran“names(2)=“Khan"
04/15/23For more QTP Scripts, www.ramupalanki.com
Arrays (cont.)Arrays (cont.)
An array can be multi dimensional.
There can be 60 (maximum) dimensions in an array.
Multiple dimensions are declared by separating the numbers in the parentheses with commas.
04/15/23For more QTP Scripts, www.ramupalanki.com
Practical Work !Practical Work !
04/15/23For more QTP Scripts, www.ramupalanki.com
ProceduresProceduresA Sub procedure:is a series of statements, enclosed by the Sub
and End Sub statements can perform actions, but does not return a
value can take arguments that are passed to it by a
calling procedure without arguments, must include an empty
set of parentheses ()
04/15/23For more QTP Scripts, www.ramupalanki.com
Procedures (Cont)Procedures (Cont)Sub Keyword is Used to declare a procedure.End Sub Keyword is Used to defining the
ending boundary of a procedure.Call Keyword is Used in order to invoke a
procedure.
Syntax:Sub mysub()
some statements End Sub
Call mysub()04/15/23
For more QTP Scripts, www.ramupalanki.com
Procedures (Cont)Procedures (Cont)Procedure can take arguments that are
passed to it by calling that procedure .
Syntax:Sub procedure name(arg1,arg2)
some statements End Sub
Call mysub(value1,value2)
04/15/23For more QTP Scripts, www.ramupalanki.com
Practical Work !Practical Work !
04/15/23For more QTP Scripts, www.ramupalanki.com
FunctionsFunctionsA Function procedure:is a series of statements, enclosed by the
Function and End Function statements can perform actions and can return a value can take arguments that are passed to it by a
calling procedure without arguments, must include an empty
set of parentheses () returns a value by assigning a value to its
name 04/15/23
For more QTP Scripts, www.ramupalanki.com
Functions (Cont)Functions (Cont)Function Keyword is Used to declare a Function.End Function Keyword is Used to defining the
ending boundary of a Function.<Function Name> is Used in order to invoke a
Function.
Syntax:Function myfunc()
some statements End Function
myfunc04/15/23
For more QTP Scripts, www.ramupalanki.com
Practical Work !Practical Work !
04/15/23For more QTP Scripts, www.ramupalanki.com
If ConditionIf ConditionIf keyword is used for executing a set of code
when certain condition is true.Endif keyword is used to end the if condition
code block.
Syntax:
If <condition> then Some statements
End if04/15/23
For more QTP Scripts, www.ramupalanki.com
If-Else ConditionIf-Else Conditionif...then...else - use this keyword if you want
to select one of two sets of lines to execute
if...then...elseif - use this keyword if you want to select one of many sets of lines to execute
04/15/23For more QTP Scripts, www.ramupalanki.com
If-Else Condition (cont.)If-Else Condition (cont.)Syntax :
if <condition> then Some statements
ElseSome statements
end If
04/15/23For more QTP Scripts, www.ramupalanki.com
If-Elseif Condition (cont.)If-Elseif Condition (cont.)Syntax :
if <condition> then Some statements
Elseif <condition> thenSome statements
ElseSome statements
end If
04/15/23For more QTP Scripts, www.ramupalanki.com
Practical Work !Practical Work !
04/15/23For more QTP Scripts, www.ramupalanki.com
Select Case ConditionSelect Case ConditionSelect case keyword is used to execute one of many blocks
of code.
Syntaxselect case <variable>
case <first expected value>Some statements
case <second expected value>Some statements………………case Else
Some Statementsend select
04/15/23For more QTP Scripts, www.ramupalanki.com
Practical Work !Practical Work !
04/15/23For more QTP Scripts, www.ramupalanki.com
For LoopFor LoopFor loop is used to execute set of statements ,
pre defined number of iterations.For … Next keywords are used to implement
the For Loop.
Syntax:For <Loop initialization state> to <ending condition>
Some Statements
Next04/15/23
For more QTP Scripts, www.ramupalanki.com
For Loop (cont.)For Loop (cont.)Step keyword is used to increase or decrease
the counter variable by the specified value.
Syntax
For <Loop initialization state> To <ending condition> Step <Increment/Decrement value>
some statementsNext
04/15/23For more QTP Scripts, www.ramupalanki.com
For Loop (cont.)For Loop (cont.)You can use for loop in order to repeat a block
of code for each item in a collection, or for each element of an array.
Syntax:For Each <variable name > in <array Variable
Name> <Some Statements>Next
04/15/23For more QTP Scripts, www.ramupalanki.com
Practical Work !Practical Work !
04/15/23For more QTP Scripts, www.ramupalanki.com
Do-while loopDo-while loopDo-while keywords are used to execute
specified code for a set of times (until a condition remains true or a condition becomes false).
Syntax
Do While <Condition for loop>Some Statements
Loop
04/15/23For more QTP Scripts, www.ramupalanki.com
Do-while loop (cont.)Do-while loop (cont.)
Do-While can also used in following syntax:
Do some Statements
Loop While i>10
04/15/23For more QTP Scripts, www.ramupalanki.com
Do-Until Loop Do-Until Loop Do – Until keyword is used for repeating
some set of statements until a certain condition is true.
Syntax:Do Until <Condition>
some statmemts
Loop
04/15/23For more QTP Scripts, www.ramupalanki.com
Do-Until Loop (cont.)Do-Until Loop (cont.)Do-Until can also used in following syntax:
Do some statements
Loop Until <Condition>
04/15/23For more QTP Scripts, www.ramupalanki.com
Practical Work !Practical Work !
04/15/23For more QTP Scripts, www.ramupalanki.com
Built in FunctionsBuilt in FunctionsVB Script provides several built in functions
that can be used just by calling them.
Few Examples:
DateTimeInt
04/15/23For more QTP Scripts, www.ramupalanki.com
Practical Work !Practical Work !
04/15/23For more QTP Scripts, www.ramupalanki.com
Thank You !Thank You !
04/15/23For more QTP Scripts, www.ramupalanki.com