Вы находитесь на странице: 1из 6

Descriptive Programming in QTP

Descriptive programming in QTP


Introduction:
This document demonstrates the usage of Descriptive programming in QTP 8.20. It also
discusses situations where Descriptive programming can be used. sing Descriptive
Programming automation scripts can be created even if the application has not been developed.
Descriptive Programming:
!henever QTP records an" action on an" ob#ect of an application$ it adds some description on
how to recogni%e that ob#ect to a repositor" of ob#ects called ob#ect repositor". QTP cannot ta&e
action on an ob#ect until unless its ob#ect description is in the 'b#ect (epositor". )ut descriptive
programming provides a wa" to perform action on ob#ects which are not in 'b#ect repositor"
'b#ect Identification:
To identif" an ob#ect during the pla" bac& of the scripts QTP stores some properties which helps
QTP to uni*uel" identif" the ob#ect on a page. )elow screen shots shows an e+ample 'b#ect
repositor":
,ow to recogni%e a radio button on a page QTP had added 2 properties the name of the radio
button and the html tag for it. The name the left tree view is the logical name given b" QTP for
the ob#ect. This can be changed as per the convenience of the person writing the test case. QTP
onl" allows ,IQ- logical name under same level of hierarch". .s we see in the snapshot the
two ob#ects in )rowser/0Page node are 1!ebTable2 and 1testPath2$ the" cannot have the same
logical name. )ut an ob#ect under some other node can have the same name. ,ow with the
current repositor" that we have$ we can onl" write operation on ob#ects which are in the
repositor". 3ome of the e+ample operations are given below
)rowser45)rowser56.Page45Page56.!eb(adio7roup 45testPath56.3elect 525
cellData 8 )rowser45)rowser56.Page45Page56.!ebTable 45!ebTable56.7et9ellData 4:$:6
)rowser45-+ample256.Page45Page56.!eb-dit45testPath56.3et 5Test te+t5
!hen and !h" to use Descriptive programming;
)elow are some of the situations when Descriptive Programming can be considered useful:
:. The ob#ects in the application are d"namic in nature and need special handling to identif" the
ob#ect. The best e+ample would be of clic&ing a lin& which changes according to the user of the
application$ -+. 1<ogout =02.
2. !hen ob#ect repositor" is getting huge due to the no. of ob#ects being added. If the si%e of
'b#ect repositor" increases too much then it decreases the performance of QTP while
recogni%ing a ob#ect.
>. !hen "ou don?t want to use ob#ect repositor" at all. !ell the first *uestion would be wh" not
'b#ect repositor"; 9onsider the following scenario which would help understand wh" not 'b#ect
repositor"
3cenario :: 3uppose we have a web application that has not been developed "et. ,ow QTP for
recording the script and adding the ob#ects to repositor" needs the application to be up$ that
would mean waiting for the application to be deplo"ed before we can start of with ma&ing QTP
scripts. )ut if we &now the descriptions of the ob#ects that will be created then we can still start
off with the script writing for testing
3cenario 2: 3uppose an application has > navigation buttons on each and ever" page. <et the
buttons be 19ancel2$ 1)ac&2 and 1,e+t2. ,ow recording action on these buttons would add >
ob#ects per page in the repositor". @or a :0 page flow this would mean >0 ob#ects which could
have been represented #ust b" using > ob#ects. 3o instead of adding these >0 ob#ects to the
repositor" we can #ust write > descriptions for the ob#ect and use it on an" page.
A. Bodification to a test case is needed but the 'b#ect repositor" for the same is (ead onl" or in
shared mode i.e. changes ma" affect other scripts as well.
C. !hen "ou want to ta&e action on similar t"pe of ob#ect i.e. suppose we have 20 te+tbo+es on
the page and there names are in the form t+tD:$ t+tD2$ t+tD> and so on. ,ow adding all 20 the
'b#ect repositor" would not be a good programming approach.
Eow to use Descriptive programming;
There are two wa"s in which descriptive programming can be used
:. )" creating properties collection ob#ect for the description.
2. )" giving the description in form of the string arguments.
:. )" creating properties collection ob#ect for the description.
To use this method "ou need first to create an empt" description
Dim ob#DDesc F,ot necessar" to declare
3et ob#DDesc 8 Description.9reate
,ow we have a blan& description in 1ob#DDesc2. -ach description has > properties 1,ame2$
1Galue2 and 1(egular -+pression2.
ob#DDesc41html tag26.value8 1I,PT2
!hen "ou use a propert" name for the first time the propert" is added to the collection and when
"ou use it again the propert" is modified. )" default each propert" that is defined is a regular
e+pression. 3uppose if we have the following description
ob#DDesc41html tag26.value8 1I,PT2
ob#DDesc41name26.value8 1t+t.H2
This would mean an ob#ect with html tag as I,PT and name starting with t+t. ,ow actuall" that
1.H2 was considered as regular e+pression. 3o$ if "ou want the propert" 1name2 not to be
recogni%ed as a regular e+pression then "ou need to set the 1regulare+pression2 propert" as
@.<3-
ob#DDesc41html tag26.value8 1I,PT2
ob#DDesc41name26.value8 1t+t.H2
ob#DDesc41name26.regulare+pression8 1t+t.H2
This is how of we create a description. ,ow below is the wa" we can use it
)rowser41)rowser26.Page41Page26.!eb-dit4ob#DDesc6.set 1Test2
!hen we sa" .!eb-dit4ob#DDesc6 we define one more propert" for our description that was not
earlier defined that is it?s a te+t bo+ 4because QTPs !eb-dit bo+es map to te+t bo+es in a web
page6.
If we &now that we have more than : element with same description on the page then we must
define 1inde+2 propert" for the that description
9onsider the ETB< code given below
I,PT t"pe82te+tbo+2 name82t+tD,ame2
I,PT t"pe82te+tbo+2 name82t+tD,ame2
,ow the html code has two ob#ects with same description. 3o distinguish between these 2 ob#ects
we will use the 1inde+2 propert". Eere is the description for both the ob#ect
@or :st te+tbo+:
ob#DDesc41html tag26.value8 1I,PT2
ob#DDesc41name26.value8 1t+tD,ame2
ob#DDesc41inde+26.value8 102
@or 2nd te+tbo+:
ob#DDesc41html tag26.value8 1I,PT2
ob#DDesc41name26.value8 1t+tD,ame2
ob#DDesc41inde+26.value8 1:2
9onsider the ETB< 9ode given below:
I,PT t"pe82te+tbo+2 name82t+tD,ame2
I,PT t"pe82radio2 name82t+tD,ame2
!e can use the same description for both the ob#ects and still distinguish between both of them
ob#DDesc41html tag26.value8 1I,PT2
ob#DDesc41name26.value8 1t+tD,ame2
!hen I want to refer to the te+tbo+ then I will use the inside a !eb-dit ob#ect and to refer to the
radio button I will use the description ob#ect with the !eb(adio7roup ob#ect.
)rowser41)rowser26.Page41Page26.!eb-dit4ob#DDesc6.set 1Test2 F(efers to the te+t bo+
)rowser41)rowser26.Page41Page26.!eb(adio7roup4ob#DDesc6.set 1Test2 F(efers to the radio
button
)ut if we use !eb-lement ob#ect for the description then we must define the 1inde+2 propert"
because for a webelement the current description would return two ob#ects.
Eierarch" of test description:
!hen using programmatic descriptions from a specific point within a test ob#ect hierarch"$ "ou
must continue to use programmatic descriptions
from that point onward within the same statement. If "ou specif" a test ob#ect b" its ob#ect
repositor" name after other ob#ects in the hierarch" have
been described using programmatic descriptions$ Quic&Test cannot identif" the ob#ect.
@or e+ample$ "ou can use )rowser4Desc:6.Page4Desc:6.<in&4desc>6$ since it uses programmatic
descriptions throughout the entire test ob#ect hierarch".
Iou can also use )rowser45Inde+56.Page4Desc:6.<in&4desc>6$ since it uses programmatic
descriptions from a certain point in the description 4starting
from the Page ob#ect description6.
Eowever$ "ou cannot use )rowser4Desc:6.Page4Desc:6.<in&45-+ample:56$ since it uses
programmatic descriptions for the )rowser and Page ob#ects but
then attempts to use an ob#ect repositor" name for the <in& test ob#ect 4Quic&Test tries to locate
the <in& ob#ect based on its name$ but cannot
locate it in the repositor" because the parent ob#ects were specified using programmatic
descriptions6.
7etting 9hild 'b#ect:
!e can use description ob#ect to get all the ob#ects on the page that matches that specific
description. 3uppose we have to chec& all the chec&bo+es present on a web page. 3o we will first
create an ob#ect description for a chec&bo+e and then get all the chec&bo+es from the page
Dim ob#D9h&Desc
3et ob#D9h&Desc8Description.9reate
ob#D9h&Desc41html tag26.value 8 1I,PT2
ob#D9h&Desc41t"pe26.value 8 1chec&bo+2
Dim all9hec&bo+es$ single9hec&)o+
3et all9hec&bo+es 8 )rowse41)rowser26.Page41Page26.9hild'b#ects4ob#D9h&Desc6
@or each single9hec&)o+ in all9hec&bo+es
single9hec&)o+.3et 1',2
,e+t
The above code will chec& all the chec& bo+es present on the page. To get all the child ob#ects
we need to specif" an ob#ect description i.e. we can?t use the string arguments that will be
discussed later in the 2nd wa" of using the programming description.
Possible 'peration on Description 'b#ect
9onsider the below code for all the solutions
Dim ob#D9h&Desc
3et ob#D9h&Desc8Description.9reate
ob#D9h&Desc41html tag26.value 8 1I,PT2
ob#D9h&Desc41t"pe26.value 8 1chec&bo+2
Q: Eow to get the no. of description defined in a collection
.: ob#D9h&Desc.9ount F!ill return 2 in our case
Q: Eow to remove a description from the collection
.: ob#D9h&Desc.remove 1html tag2 Fwould delete the html tag propert" from the collection
Q: Eow do I chec& if propert" e+ists or not in the collection;
.: The answer is that it?s not possible. )ecause whenever we tr" to access a propert" which is
not defined its automaticall" added to the collection. The onl" wa" to determine is to chec& its
value that is use a if statement 1if ob#D9h&Desc41html tag26.value 8 empt" then2.
Q: Eow to browse through all the properties of a properties collection;
.: Two wa"s
:st:
@or each desc in ob#D9h&Desc
,ame8desc.,ame
Galue8desc.Galue
(- 8 desc.regulare+pression
,e+t
2nd:
@or i80 to ob#D9h&Desc.count / :
,ame8 ob#D9h&Desc4i6.,ame
Galue8 ob#D9h&Desc4i6.Galue
(- 8 ob#D9h&Desc4i6.regulare+pression
,e+t
2. )" giving the description in form of the string arguments.
Iou can describe an ob#ect directl" in a statement b" specif"ing propert":8value pairs describing
the ob#ect instead of specif"ing an ob#ect?s
name. The general s"nta+ is:
Test'b#ect45Propert",ame::8Propert"Galue:5$ 5...5 $ 5Propert",ameJ:8Propert"GalueJ56
Test'b#ectKthe test ob#ect class could be !eb-dit$ !eb(adio7roup etcL.
Propert",ame:8Propert"GalueKthe test ob#ect propert" and its value. -ach propert":8value pair
should be separated b" commas and *uotation
mar&s. ,ote that "ou can enter a variable name as the propert" value if "ou want to find an
ob#ect based on propert" values "ou retrieve during a run session.
9onsider the ETB< 9ode given below:
I,PT t"pe82te+tbo+2 name82t+tD,ame2
I,PT t"pe82radio2 name82t+tD,ame2
,ow to refer to the te+tbo+ the statement would be as given below
)rowser41)rowser26.Page41Page26.!eb-dit41,ame:8t+tD,ame2$2html tag:8I,PT26.set 1Test2
.nd to refer to the radio button the statement would be as given below
)rowser41)rowser26.Page41Page26.!eb(adio7roup41,ame:8t+tD,ame2$2html
tag:8I,PT26.set 1Test2
If we refer to them as a web element then we will have to distinguish between the 2 using the
inde+ propert"
)rowser41)rowser26.Page41Page26.!eb-lement41,ame:8t+tD,ame2$2html
tag:8I,PT2$2Inde+:8026.set 1Test2 F (efers to the te+tbo+
)rowser41)rowser26.Page41Page26.!eb-lement41,ame:8t+tD,ame2$2html
tag:8I,PT2$2Inde+:8:26.set 1Test2 F (efers to the radio button

Вам также может понравиться