Descriptive Programming in QTP


什么是 Descriptive programming

先摘一段QTP帮助里的内容:

Using Programmatic Descriptions

When you record an operation on an object, QuickTest adds the appropriate test object to the object repository. Once the object exists in the object repository, you can add statements in the Expert View to perform additional methods on that object. To add these statements, you usually enter the name (not case sensitive) of each of the objects in the object's hierarchy as the object description, and then add the appropriate method.

For example, in the statement below, username is the name of an edit box. The edit box is located on a page with the name Mercury Tours and the page was recorded in a browser with the name Mercury Tours .

Browser("Mercury Tours ").Page("Mercury Tours ").WebEdit("username")

Because each object in the object repository has a unique name, the object name is all you need to specify. During the run session, QuickTest finds the object in the object repository based on its name and parent objects, and uses the stored test object description for that test object to identify the object in your Web site or application.

You can also instruct QuickTest to perform methods on objects without referring to the object repository or to the object's name. To do this, you provide QuickTest with a list of properties and values that QuickTest can use to identify the object or objects on which you want to perform a method.

Such a programmatic description can be very useful if you want to perform an operation on an object that is not stored in the object repository. You can also use programmatic descriptions in order to perform the same operation on several objects with certain identical properties, or in order to perform an operation on an object whose properties match a description that you determine dynamically during the run session.

For example, suppose you are testing a Web site that generates a list of potential employers based on biographical information you provide, and offers to send your resume to the employer names you select from the list. You want your test to select all the employers displayed in the list, but when you design your test, you do not know how many check boxes will be displayed on the page, and you cannot, of course, know the exact object description of each check box. In this situation, you can use a programmatic description to instruct QuickTest to perform a Set "ON" method for all objects that fit the description: HTML TAG = input, TYPE = check box.

There are two types of programmatic descriptions. You can either list the set of properties and values that describe the object directly in a test statement, or you can add a collection of properties and values to a Description object, and then enter the Description object name in the statement.

Entering programmatic descriptions directly into your statements may be the easier method for basic object description needs. However, in most cases, the Description object method is more powerful and more efficient.

这段话说出了Descriptive programming的最主要的作用。倒数第二段之中提到的,你可以直接在程序之中使用描述性代码实现,抑或是使用Description object。这里想探讨的是使用Description object的方法。而在QTP之中使用的Description object就是Descriptive Object

什么是 Description Object

简单看来Descriptive Object就是一个Properties collection objectObject包含propertypropValue,可以通过property很容易的访问对应的propValue。一个简单的例子(从QTP帮助之中摘录):
set EditDesc = Description.Create()

EditDesc("Name").Value = "userName"

EditDesc("Index").Value = "0"

Browser("Welcome: Mercury").Page("Welcome: Mercury").WebEdit(EditDesc).Set "MyName"

...
例子之中创建了一个Descriptive对象,这是一个Collection的类型对象,支持一般的add,remove等方法。例子之中向这个创建的Descriptive Object增加了2个属性。

详细的使用说明可以参考QTP的帮助。

QTPObject Identification

QTP回放的时候是通过对象的一些PROPERTIESIDENTITY录制时的对象的。在OBJECT REPOSITORY之中,每个对象相应会有一个OBJECT NAME用来标示这个OBJECT的,好像我们这样访问网页之中的一个EDIT对象:

Browser("Mercury Tours").Page("Mercury Tours").WebEdit("username")

上面这个例子使用了OBJECT REPOSITORYOBJECT REPOSITORY纪录了每个OBJECTname和用来定位的属性组,来定位OBJECT的。如果有些对象是动态生成的,这样这种OBJECT就不会存在于OBJECT REPOSITORY之中,则怎么来定位这些OBJECT呢。或者有没有什么方法可以不使用OBJECT REPOSITORY而直接访问相对应的OBJECT呢?

什么时候和为什么要使用Descriptive programming?

一般有以下几种情况我们会使用Descriptive programming,包括上面提到的:

1、存在动态对象
2、不想使用OBJECT REPOSITORY
3、性能考虑,OBJECT REPOSITORY太大会影响效率

怎么使用Descriptive programming?

之前提到的两种方法,一种是直接在语句中使用对象描述字串,一种是使用Descriptive对象。

1、直接使用对象描述字串
      语法是 TestObject("property1:=value1","property2:=value2",...,"propertyn:=valuen")
    
示例:
增加新评论的HTML代码

<div class = "comments">

添加新评论

<br>

    <div id="Anthem_PostComment.ascx_CommentUpdatePanel__"><div id="PostComment.ascx_CommentUpdatePanel">
        

        <table id="CommentForm" class = "commentsTable" cellSpacing="2" cellPadding="0"  border="0" >



        <tr>

               <td width="75">大名</td>

               <td><input name="PostComment.ascx:tbName" type="text" value="black_sam" maxlength="32" id="PostComment.ascx_tbName" disabled="disabled" Size="40" style="width:322px;" /></td>

               <td><span controltovalidate="PostComment.ascx_tbName" errormessage="Please enter your name" id="PostComment.ascx_RequiredFieldValidator2" evaluationfunction="RequiredFieldValidatorEvaluateIsValid" initialvalue="" style="color:Red;visibility:hidden;">Please enter your name</span></td>

        </tr>

        <tr>
               <td>网址</td>

               <td><input name="PostComment.ascx:tbUrl" type="text" maxlength="256" id="PostComment.ascx_tbUrl" Size="40" style="width:322px;" /></td>

               <td></td>

        </tr>

            <div id="PostComment.ascx_AntiBotImageRegion">

              

                <tr>

                    <td>

                        校验码:</td>
                    <td colspan="2">

                        <input name="PostComment.ascx:tbAntiBotImage" type="text" id="PostComment.ascx_tbAntiBotImage" style="width:75px;" />

                        <span display="Dynamic" id="PostComment.ascx_valAntiBotImage" evaluationfunction="CustomValidatorEvaluateIsValid" style="color:Red;display:none;">检验码无效!</span>

                        <span id="Anthem_PostComment.ascx_imgAntiBotImage__"><img id="PostComment.ascx_imgAntiBotImage" src="/AntiBotImage.aspx?abi=1314&amp;id=1771402" alt="" align="absmiddle" border="0" /></span>

                        <span id="Anthem_PostComment.ascx_lbCreateAntiBotImage__"><a onclick="javascript:Anthem_FireCallBackEvent(this,event,'PostComment.ascx:lbCreateAntiBotImage','',false,'','','',true,null,null,null,true,true);return false;" id="PostComment.ascx_lbCreateAntiBotImage" href="javascript:__doPostBack('PostComment.ascx$lbCreateAntiBotImage','')">看不清,换一张</a></span>

                    </td>

                </tr>

           

        </div>
        <tr>

               <td colSpan="3"><br>评论&nbsp;

                       <span controltovalidate="PostComment.ascx_tbComment" errormessage="Please enter a comment" id="PostComment.ascx_RequiredFieldValidator3" evaluationfunction="RequiredFieldValidatorEvaluateIsValid" initialvalue="" style="color:Red;visibility:hidden;">Please enter a comment</span><BR>

                       <textarea name="PostComment.ascx:tbComment" rows="6" cols="50" id="PostComment.ascx_tbComment" style="width:400px;"></textarea>

               </td>

        </tr>



        <tr>
               <td colspan=3><span id="Anthem_PostComment.ascx_btnSubmit__"><input type="submit" name="PostComment.ascx:btnSubmit" value="提交" onclick="javascript:Anthem_FireCallBackEvent(this,event,'PostComment.ascx:btnSubmit','',true,'','','正在处理...',false,null,null,null,true,true);return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;PostComment.ascx:btnSubmit&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" language="javascript" id="PostComment.ascx_btnSubmit" class="Button" /></span></td>

        </tr>

        <tr><td colSpan="3"><span id="PostComment.ascx_Message" style="color:Red;"></span></td></tr> 

</table>


就拿网址这个输入框作示范

<input name="PostComment.ascx:tbUrl" type="text" maxlength="256" id="PostComment.ascx_tbUrl" Size="40" style="width:322px;" />



我们这样访问这个WebEdit对象

Browser("Browser").Page("Page").WebEdit("Name:=PostComment.ascx:tbUrl","html tag:=INPUT").set "www.sina.com.cn"



2、使用Description Object



同样是上面的例子,我们用Description Object重写如下:



dim obj_desc

set obj_desc = Description.Create
 
 
  
   
 
 
Obj_desc("name").value = "PostComment.ascx:tbUrl"
  
  
Obj_desc("html tag").value = "INPUT"
 
 
  
   
 
 
Browser("Browser").Page("Page").WebEdit(obj_desc).set "www.sina.com.cn"
 
 
  
   
 
 
缺省所有的属性的value都是一个正则表达式,如果想完全匹配的话,可以设置正则表达式为false
 
 
  
   
 
 
Obj_desc("html tag").RegularExpression = False
 
 
  
   
 
 
正则表达式的使用可以参照QTP的使用帮助,有详细的说明。
 
 
  
   
 
 
对于具有类似属性的对象数组,我们也可以使用Description Object来实现:
 
 
  
   
 
 
<INPUT type=”text” name=”txt_name>
<INPUT type=”text” name=”txt_name>
这是两个具有相同属性的两个文本框,默认我们可以使用index属性来区分这两个表面看来具有属性的对象
 
 
  
   
 
 
dim obj_desc

set obj_desc = Description.Create
 
 
  
   
 
 
第一个文本框
Obj_desc("html tag").value = "INPUT"
Obj_desc("name").value = "txt_name"
  
  
Obj_desc("index").value = "0"
  
  
第二个文本框
Obj_desc("html tag").value = "INPUT"
Obj_desc("name").value = "txt_name"
  
  
Obj_desc("index").value = "1"
  
  
 
 
  
   
 
 
如果我们要把网页上的所有checkbox都设置为True,怎么办呢?
 
 
  
   
 
 
dim obj_desc

set obj_desc = Description.Create
Obj_desc("html tag").value = "INPUT"
Obj_desc("type").value = "checkbox"
  
  

  
  
   
    
  
  
dim allcheckbox,onecheckbox
  
  
set allcheckbox = Browser("Browser").Page("Page").ChildObjects(obj_desc)
 
 
  
   
 
 
for each onecheckbox in allcheckbox
  
  onecheckbox.set "ON"
 
 
  
   
 
 
next
 
 
  
   
 
 

 

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值