1)
VBScript in QTP
Scripting language for
QuickTest Professional (QTP) is VBScript.
VBScript (short form of
Visual Basic Scripting Edition) is a lively scripting language interpreted via
Microsoft's Windows Script
Host.
VBScript has many powerful
functions and provides excellent support for variables, data types, and error
handling.
Two script engines can
interpret VBScript- VBScript.dll, which is invoked by asp.dll is
used in web
environment and Wscript.exe
& Cscript.exe in Windows GUI environment using Windows Script Host
(WSH).
We typically, use VBScript
within WSH to automate systems administration tasks. WSH is the system
module that transforms a
VBScript file into a Windows executable file.
Wscript.exe
is used to display output and receive input in Windows GUI
format such as dialog and input
boxes.
Cscript.exe
is used in a command-line environment. When VBScript source code
is contained in
standalone files, they
have the file extension .Vbs.
2)
VBScript Data Type
VBScript has only one data
type called a Variant that can store any type of value. A Variant is a special
kind
of data type that can
contain different kinds of information, depending on how it is used.
Suppose if you’re using a
variable for assigning a numeric value, that variable behaves like a numeric
data
type. If you assign string
value, that variable behaves like a string.
VBScript contains
following subdatatypes in the Variant.
1) Empty
2) Null
3) Boolean
4) Byte
5) Integer
6) Currency
7) Long
8) Single
9) Double
10) Date (Time)
11) String
12) Object
13) Error
We can use conversion
functions to convert data from one sub-data type to another type. To find
subdatatype of a variant
we need to use vartype function.
Explanation
of VBScript for use in HP QuickTest Professional (QTP)
http://www.softwaretestinggenius.com
Page 3 of 23
3)
VBScript Variable
A variable is a convenient
placeholder to store program information. We can change variable value in
script
running time. In VBScript
variables are always of one fundamental data type Variant.
Advantage
of using Variable in scripts: Variable is quite useful in
carrying a value. For example if the
script is using a value 10
in five places (3rd, 7th, 12th, 17th, 20th lines). In case the value is changed
from 10
to 20 then we need to
change that value in all the places wherever it is used. But if we have used
variable in
place of value (x=10) we
need to change in only one place if that value is changed from 10 to 20(x=20).
Variables are having
flexibility to change value in run time.
Variable names go by the
following standard naming conventions in VBScript. A variable name:
# Must begin with an
alphabetic character
# Cannot contain an
embedded period (.)
# Must not exceed 255
characters
# Must be unique in the
scope in which it is declared.
It must be distinctive
(unique) within the scope in which it is declared. If we declare a variable
inside a
procedure then its scope
is local to that Procedure and only code within that procedure can access it,
but
if we declare a variable
outside a procedure in a script, it is available to all the procedures in a
script.
Procedure level variable
exits as long as we are in the procedure and a life of a script level variable
is the
time it is declare up till
the time script finishes.
Variables can be declared explicitly
and implicitly.
Explicitly variables are
declared with Dim statement, Public Statement, Private Statement.
Dim Name
Dim Name, employee address,
city
Implicitly we can declare
them within the script by just using the variable name. But this practice is
prone
to errors.
We can force VBScript to
require all variables to be explicitly declared by including the statement Option
Explicit at the
start of every script.
VBScript does not provide
support for constants.
From VBScript 5.0 onwards
programmers are able to create class constructs in VBScript-A step towards
OOP.
Variables declared by Dim
and Public are public in nature (accessible outside of the class). By default
also variables are Public:
in nature. With Private we can declare variables not visible outside
of the class.
Example of
a variable:
Type this in notepad, save
the notepad with some name and .html extension (like in c:\program\p.html
where “program” is the
name of a folder)
Explanation
of VBScript for use in HP QuickTest Professional (QTP)
http://www.softwaretestinggenius.com
Page 4 of 23
<html>
<body>
<script type=”text/vbscript”>
dim variable_name
variable name =”Testing
Genius”
document.write(variable_name)
</script>
</body>
</html>
Now open internet explorer
and in the address bar type c:\program\p.html and press enter.
Another
example that can be tried is:
<html>
<body>
<script
type="text/vbscript">
dim variable_ name
variable_name = 2
variable_name =
variable_name+1
document.write(variable_name)
</script>
</body>
</html>
Another
example of getting input from the user:
Enter- the below
code in notepad and save it with .vbs extension (like we saved it as
c:\program\q.vbs where
“program” is the name of a
folder)
dim variable_ name
variable name
=InputBox("Enter your name:")
MsgBox("Your name is
" & variable_name)
Now go to command prompt
(C:\>) and type program\q and hit enter ( there is no need to type the
extension)
4)
VBScript Arrays
Array Variable: A variable
containing a single value is a scalar variable. we can create a variable that
can
contain a series of values
using an index number. This is called an array variable. Arrays are useful when
we
are storing sets of
similar data. We can store any kind of data in an array. The array can hold a
combination
of data types.
Arrays
are of two types: 1) Fixed Length Arrays 2) Dynamic Arrays
Fixed arrays have a
specific number of elements in them, whereas dynamic arrays can vary in the
number of
elements depending on how
many are stored in the array.
Here we have explained all
the ways to initialize and use arrays in VBScript. Every element of an array is
associated with a unique
index number. By default, index number starts from 0. The number of elements
in an array is a fixed
number. It can also be re-adjusted dynamically.
Explanation
of VBScript for use in HP QuickTest Professional (QTP)
http://www.softwaretestinggenius.com
Page 5 of 23
Method of
creating Fixed Length Arrays:
For Example Dim a(10) :
Here 'a' is an array and is having 11 elements (Since array count starts from
0).
Here it's a fixed size
array with size 10.
Method of
creating Dynamic Arrays:
A dynamic array is created
in the same way as a fixed array, but we don't put any bounds in the
declaration.
For Example Dim x() : Here
'x' is the dynamic array and we can store n number of elements in it. The
benefit
of a dynamic array is that
if we don't know how large the array will be when we write the code, we can
create
code that sets or changes
the size while the VBScript code is running.
Arrays can have multiple
dimensions. VBScript supports up to 60.
Example –
1:
Dim
variable_name(upper_limit) [As data_type]
If "As
data_type" is not specified, it will be a variant. Above we have declared
a fixed size array. The array
size limit is upper limit
+1 because index starts from 0.
<html>
<body>
<script
type-"text/vbscript">
dim e_name (5)
e_name(0)="Himanshu"
e_name(1)="Piyush"
e_name(2)="Neha"
e_name(3)="Manjri"
e_name(4)="Rohit"
e_name(5)="Amar"
for i-0 to 5
document.write(e_name(i)
& "<br />")
next
</script>
</body>
</html>
Example –
2:
Dim variable name() [As
data-type]
ReDim [Preserve]
variable_name(upper_limit)
Firstly we declare an
array with no upper limit and then with ReDim we reset the upper bound
to a new
value. The optional key
word "Preserve" states that all of the old elements must be
preserved when
changing the array size.
The size of the dynamic
array changes during the time our script is running. The array is initially
declared using either the
Dim statement or using the ReDim statement, For a dynamic array, no size
or
number of dimensions is
placed inside parentheses.
Dim first Array()
ReDim second-Array()
In the following example,
ReDim sets the initial size of the dynamic array to 25
ReDim first_Array(25)
We can resize a dynamic
array unlimited number of times.
Explanation
of VBScript for use in HP QuickTest Professional (QTP)
http://www.softwaretestinggenius.com
Page 6 of 23
Dim array-dynamic()
' Size the dimension to
contain one dimension with 3 elements
ReDim array_dynamic(2)
Put data in the array
array_dynamic(0)= “1”
array_dynamic(1)= “2”
array_dynamic(2)= “3”
Resize the array, but keep
the existing data
ReDim Preserve
array_dynamic(5)
' Display the 3rd element
MsgBox array_dynamic(2)
MsgBox displays 3.
Example –
3:
variable_name =
Array(element1, element2, ...)
Array function takes
values of variant type and returns a dynamic sized array. The arguments are a
listing
of values that will become
the elements of the array,
Dim a
a=Array(5,10,15,20)
document.write(a(3))
Output: 20
Some of
the Array keywords and their uses;
Keyword
Function
Dim It will
Declare an array
Erase Reinitializes
the elements if it is a fixed-size array and
deallocates the memory
used if it is a dynamic array.
IsArray This will
Return True if A is an array, False if it is not
LBound This will
Return lower hound of an array, in VBScript it will
always return 0
Preserve Preserve
(Optional) is used to preserve the data in an
existing array, when you
resize it.
ReDim This is
used to size or resize a dynamic array.
UBound This will
Return all upper- bound of array
Explanation
of VBScript for use in HP QuickTest Professional (QTP)
http://www.softwaretestinggenius.com
Page 7 of 23
5)
VBScript Constants
A constant is a meaningful
name that takes the place of a number or string and never changes. The
difference between
variable and constant is we can change the variable value in run time but for
constants its
not possible.
How do we
create constants?
For Example: const
str="QTPGENIUS" : Here ”str” is a constant and the value will never
change.
We have two types of
constants 1) Public constants 2) Private constants.
By default all constants
are Public. However we can specify the type if required
For Example: Public const
str="QTPGENIUS"
or
For Example: Private const
str="QTPGENIUS"
6)
VBScript Functions and Subroutines
There are two types of
procedures in VBScript
1)
Function Procedure: It is a series of VBScript statements enclosed by the Function and
End Function
statements. In Function
procedures we can use function name to assign a value. Function Procedure is
able
to return the value.
For example:
Function demo_add(a,b)
demo_add=a+b
End Function
oVal=demo_add(2,3)
msgbox oVal 'Returns 5
In this example demo_add
function returns a value to oVal.
2) Sub
Procedure: It is a series of VBScript statements enclosed by the Sub and End
Sub statements.
Sub Procedure cannot
return any value.
For example:
Sub demo_sub(a,b,c)
c=a+b
End sub
demo_sub 2,3,x
msgbox x 'Returns 5
This example will do the
same as what function procedure is doing above. But in sub Procedure we need to
use one more parameter to
get values from the sub procedure.
The main difference
between a function and a subroutine is that a subroutine will do some
processing of
the code and then quit; while a function
processes some code and then returns the result back.
VBScript functions are described using the Function and End Function keywords.
<html>
<body>
<script type=”text/vbscript”>
FUNCTION add2numbers
Result . 2 + 2
add2numbers = Result
END FUNCTION
document.write(add2numbers)
</script>
</body> </html>
<html>
<body>
<script type=”text/vbscript”>
FUNCTION add2numbers
Result . 2 + 2
add2numbers = Result
END FUNCTION
document.write(add2numbers)
</script>
</body> </html>
No comments:
Post a Comment