Documente Academic
Documente Profesional
Documente Cultură
Table Of Contents
Introduction:............................................................2
Why Expert View?....................................................2
Basic OOPS Concepts...............................................3
VB Script data Types:...............................................3
Variant Sub types:............................................................................................................4
VB Script variables:.........................................................................................................4
Variable declaration:........................................................................................................5
Types of Variables:..........................................................................................................5
Multi dimensional arrays:................................................................................................5
1
Usage of Preserve keyword:............................................................................................6
Scope and life time of VB script variables:.....................................................................6
VB script operators:.................................................7
VB Script Conditional statements:............................8
Usage of Classes in VB script:...................................9
Programmatic Description:.....................................10
Static form of DP:..........................................................................................................11
Dynamic Form of DP:....................................................................................................13
Working with child objects:.....................................13
Pro’s and Cons of DP:.............................................14
Dictionary Object...................................................15
Advantages of using the “Dictionary Object”:..............................................................15
References:............................................................17
Introduction:
This material is mainly targeted for people who are well aware of QTP basics
like recording and running a test case. Current times require users to have
programming skills apart from basic record and playback to effectively
automate an application.
2
change in object properties during run-time nor does it perform any kind of
computations and provide us with results.
In brief, expert view helps us to build tests, with which we can handle the
following
Any change made in the expert view will be correspondingly translated in the
keyword view. All test actions, except the root Global action can be edited in
the expert view.
To work with the expert view, one needs to have a basic understanding of the
VB Scripting language, which is a programming language developed by
Microsoft.
3
most appropriate for numbers. Similarly, if you're working with data that can
only be string data, VBScript treats it as string data. You can always make
numbers behave as strings by enclosing them in quotation marks (" ").
Subtype Description
Empty Variant is uninitialized. Value is 0 for numeric variables or a zero-length
string ("") for string variables.
Null Variant intentionally contains no valid data.
Boolean Contains either True or False. (The True keyword has a value equal to -1
and a False key word will have a value equal to 0)
Byte Contains integer in the range 0 to 255.
Integer Contains integer in the range -32,768 to 32,767.
Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807.
Long Contains integer in the range -2,147,483,648 to 2,147,483,647.
Single Contains a single-precision, floating-point number in the range
-3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to
3.402823E38 for positive values.
Double Contains a double-precision, floating-point number in the range
-1.79769313486232E308 to -4.94065645841247E-324 for negative
values; 4.94065645841247E-324 to 1.79769313486232E308 for positive
values.
Date Contains a number that represents a date between January 1, 100 to
(Time) December 31, 9999.
String Contains a variable-length string that can be up to approximately 2
billion characters in length.
Object Contains an object.
Error Contains an error number.
VB Script variables:
4
A variable is a placeholder which refers to a memory location where the
intermediate data of a script is stored. In VB script you can get the value of
the variable by referring its name.
All variables in VB script are of the fundamental data type discussed in the
previous section called “Variant”
Variable declaration:
Types of Variables:
There are two types of variables namely Scalar and Array variables. A scalar
variable can hold a single value (can be an integer, string, object or a any of
the variant sub types). We have one more variable type called Array variable
using which one can create a series of variables. Array variables are created
in the same way as scalar variables but in the case of Array variables, you
should give the size of the Array variable.
5
In a two-dimensional array, the first number is always the number of rows;
the second number is the number of columns.
You can also declare an array whose size changes during the time your script
is running. This is called a dynamic array. The array is initially declared within
a procedure using either the Dim statement or using the ReDim statement.
However, for a dynamic array, no size or number of dimensions is placed
inside the parentheses.
When you resize an array there is a chance that the existing data in the array
can be lost. We can prevent this by using Preserve key work when re-sizing a
dynamic array using ReDim statement.
In the above example, all the 11 values stored in Array1 are preserved and
the capacity of the array is increased by 6.
However when you diminish the size of a dynamic array the values from the
last index of the array would be lost.
If you use the Preserve keyword, you can resize only the last array
dimension, and you can't change the number of dimensions at all. For
example, if your array has only one dimension, you can resize that dimension
because it is the last and only dimension. However, if your array has two or
more dimensions, you can change the size of only the last dimension and still
preserve the contents of the array.
6
A variable declared inside a The scope of the variable lies
procedure inside the procedure where it is
defined. The lifetime of the
variable ends after the last line of
this procedure is executed
A Variable declared out of a The scope of the variable extends
procedure to all the procedures in the current
script as it is a script level variable.
The life time of this variable ends
when the script completes
execution
Public variables Public <variable Name>. The
scope of this variable extends
through out the script
VB script operators:
Like any other programming language VB script supports the following
operations on Data. VB script has Arithmetic, logical and comparison
operators.
Arithmetic operators:
Description Symbol
Exponentiation ^
Unary negation -
Multiplication *
Division /
Integer division \
Modulus arithmetic Mod
Addition +
Subtraction -
String concatenation &
Logical operators:
Description Symbol
Logical negation Not
Logical conjunction And
7
Logical disjunction Or
Logical exclusion Xor
Logical equivalence Eqv
Logical implication Imp
1. if –elseif
2. Switch case
Syntax of if – else:
If <condition> then
…….statements……………..
…………………………….
………………………..
Elseif <condition> then ‘Please note that period is not allowed in between
else and if
…………statements……………….
…………………..
Else
……..statements………
……………
End if
Example:
If Browser(oBrowser).Page(oPage).WebElement(oWebelement).Exist(5) then
sStatus = “Pass”
reporter.reportevent micPass, “Test step name”, “test step
description”
elseif then sStatus = “Fail”
reporter.reportevent micFail, “Test step name”, “test step description”
else
ExitTestIteration
end if
8
A better way to choose between several alternatives is the Select Case
statement.
Syntax:
Select case <RO value>
Case “<value1>”
…..statements……….
…………………
Case “<Value2>”
……..statements………..
………………
Case Else ‘ This is smilar to default case in C
…..statements……….
…………..
End Select
Example:
Select case
Browser(oBrowser).Page(oPage).Link(oLink).GetROProperty(“innerhtml”)
Case “Assignments”
Browser(oBrowser).close
Case “Sessions”
Browser(oBrowser).Page(oPage).Link(oLink).click
Case Else
ExitTestIteration
End Select
Class myClass
Function myFunction
Msgbox “in function”
End function
End class
Action script calls a
function which acts
a constructor to the
class defined a
library Function cReateConstructor()
Set x = new myClass
x.mFunction()
End Function
9
Programmatic Description:
When a particular action is recorded, QTP stores the object in the Object
Repository. During script playback, QTP finds the Object in Object Repository
using object Logical Name and Object Hierarchy. QTP retrieves test object
properties from OR. QTP searches actual application for the object with the
same properties as the OR test object and performs user action.
CONS:
Additional layer to maintain
Unnecessary objects can be created
Multiple users cannot concurrently save/write to the shared OR
It won’t eliminate the need for Descriptive Programming in most of
cases
Descriptive programming can be mainly used to bypass the OR. This is one of
the key concepts in programming using the expert view, since the user can
create objects similar to ones existing in the OR.
10
message, “Object’s description matches more than one of the objects
currently displayed in your application" would be displayed.
3) DP is also useful in situations where the script has to handle dynamic
objects. For instance, based on the details provided by the user in one
page, a list box may or may not appear in the next page.
4) Modification to a test case is needed but the Object repository for the
same is Read only or in shared mode i.e. changes may affect other
scripts as well.
5) When using an Object Reference in an External Function, it is difficult
to make sure if the relevant object has been defined in the Calling
Action’s Object Repository or Even if it is defined, does it have the
same Logical Name?
6) In certain cases, an object may change its hierarchy i.e. an object may
appear under a different parent each time.
<Object>(“<Property>: = <Value>”)
1) SystemUtil.Run "Internet","","","open"
2) Browser("Browser").Page("Page").Sync
3) Browser("Browser").Navigate "http://www.google.co.in/"
4) Browser("Browser").Page("Google").WebEdit("q").Set "QTP"
5) Browser("Browser").Page("Google").WebEdit("q").Submit
This is how the script would look, if static method of descriptive programming
is employed.
Ex: 2 (static)
1) SystemUtil.Run "Internet","","","open"
2) Browser("Title:=Browser").Page("Title:=Page").Sync
3) Browser(“Title:=Browser”). Navigate http://www.google.co.in/
11
4)
Browser(“Title:=Browser”).Page(“Name:=Google”).WebEdit(“Name:=q”).Set
“QTP”
5)
Browser(“Title:=Browser”).Page(“Name:=Google”).WebEdit(“Name:=q”).Sub
mit
It is evident that DP has been employed from the 3rd step. When using DP
from a specific point in the test, the user must continue to use DP from that
point onwards. If a test object is specified by its OR name after other objects
in the hierarchy have been specified using DP, QTP will not be able to identify
the object.
The following statement can be used if the “Browser” is present in the OR.
Browser(“Title:=Browser”).Page(“Title:=Google”).WebEdit(“q”).Set “QTP”
In this case, the user has started to use DP. When QTP comes across the part
WebEdit(“q”), it will try to find the object in OR and the statement will fail.
For e.g.
Browser(“Title:=Browser”).Page(“Title:=Google”).WebEdit(“Name:=q”,
height:=400).Submit
This would attempt to identify the webEdit object with logical name “q” and
height 400.
By now all of you might be wondering how it’s possible for a person who is
going to code with DP to know the peoperties and values of the web/window
objects. This is mainly provided in a data sheet by the developers, else a
simple way would be to use the “Object Spy” tool to highlight the object in
the application and learn it’s properties.
e.g. Browser(“Title:=Browser”).Page(“Title:=*Google*”).WebEdit(“Name:=q”,
“innerhtml:=q”).Submit
This would attempt to find the page with title “Google” preceded and
followed by any number of characters.
12
Incase the statement Browser(“Title:=Browser”).Page(“Title:=Google”), is
used several times in the script, the user can assign the value to a variable
and use this variable henceforth.
1) SystemUtil.Run "Internet","","","open"
2) Browser("Title:=Browser").Page("Name:=Page").Sync
3) Browser(“Title:=Browser”). Navigate http://www.google.co.in/
4) Set myBrowser = Browser(“Title:=Browser”).Page(“Name:=Google”)
5) myBrowser. WebEdit(“Name:=q”).Set “QTP”
6) myBrowser. WebEdit(“Name:=q”).Submit
1) SystemUtil.Run "Internet","","","open"
2) Browser("Browser").Page("Page").Sync
3) Browser("Browser").Navigate "http://www.google.co.in/"
3) Set myObj=Description.create()
4) myObj(“micclass”).Value= “Browser”
5) myObj(“Title”).Value= “Browser”
6) Set myObj1=Description.create()
7) myObj1(“micclass”).Value= “Page”
8) myObj(“Title”).Value= “Google”
9) Set myObj2=Description.create()
10) myObj2(”micclass”).Value= “WebEdit”
11) myObj2(“Text”).Value=”q”
12) Set myObj3=Description.create()
13) myObj3(“Text”).Value=”Submit”
14) myObj3(“micclass”).Value= “WebButton”
12) Browser(“myObj”).Page(“myObj1”).WebEdit(“myObj2”).Set “QTP”
13) Browser(“myObj”).Page(“myObj1”).WebButton(“myObj3”).Click
13
The example sited below, would help get the count of the number of edit
boxes present in the site “http://labs.google.com/sets”.
1) SystemUtil.Run "Internet","","","open"
2) Browser("Browser").Page("Page").Sync
3) Browser("Browser").Navigate http://labs.google.com/sets
4) Set editBox = Description.create()
5) editBox(“micclass”).Value =”WebEdit”
6) Set Edits = Browser("Google Sets").Page("Google
Sets").ChildObjects(editBox)
7) MsgBox "Number of Edits: " & Edits.Count
When this script is executed, the string “Number of Edits: <no>” would be
displayed in a message box.
In this example the index “0” is used to refer to the first edit box. QTP enters
the string “edit box 1” in the first edit box.
The following is an example to enter text in all the edit boxes in one stretch
1) SystemUtil.Run "Internet","","","open"
2) Browser("Browser").Page("Page").Sync
3) Browser("Browser").Navigate http://labs.google.com/sets
4) Set editBox = Description.create()
5) editBox(“micclass”).Value = “WebEdit”
6) Set Edits = Browser("Google Sets").Page("Google
Sets").ChildObjects(editBox)
7) count = Edits.Count
8) For i = 0 to count
9) Edits(i).Set “edit box” &(i+1)
10) Next
These scripts would work irrespective of the number of edit boxes present in
the page, as the dynamic count is retrieved from the script.
14
It’s a white box
Compatible with different QTP versions
Code portability is high
Easy to mass update
CONS:
Lower Code Readability and requires more comments, like “what
object is accessed”
Potentially slower to create
To highlight an object in the application requires utilizing the
“Highlight” method
Dictionary Object
This would display the value “1” in the message box. The first 2 statements
can be present in one action say Action 1. If the 3rd statement is executed
from another action say Action 2, it would still work as dictionary objects are
accessible from all actions.
15
Returns true if a specified key exists in the Dictionary object, false if it
does not.
3) Keys Method (object.Keys( ))
Returns an array containing all existing keys in a Dictionary object.
4) Remove Method (object.Remove(key))
Removes a key, item pair from a Dictionary object.
5) RemoveAll Mthod (object.RemoveAll))
The RemoveAll method removes all key, item pairs from a Dictionary
object.
Using this approach can reduce the number of functions to declare and
initiate a web object.
Dim GUIElement
Public a
Set a = CreateObject("Scripting.Dictionary")
Set GUIElement = CreateObject("Scripting.Dictionary")
GUIElement.Add "oBrowser", "Base camp (software) -"
GUIElement.Add "oPage”, "Basecamp (software) -"
GUIElement.Add "micclass", "WebElement"
GUIElement.Add "innertext", "Features"
GUIElement.Add "html tag", "SPAN"
GUIElement.Add "Class", "toctext"
act1 = "highlight"
‘ Getting the keys of the scripting.dictionary
GUIKeys= GUIElement.Keys
‘The below loop will set the properties of the objects in one shot.
For i = 2 to GUIElement.count-1
GUIObjects (GUIKeys(i)).value = GUIElement(GUIKeys(i))
Next
Browser(GUIElement(GUIKeys(0))).Page(GUIElement(GUIKeys(1))).Link(GUIOb
jects).highlight
16
References:
1. MSDN website
17