QTP自传之对象库编程

对象库编程是我们平时工作中使用最多的编程方式,在自动化脚本开发中起到举足轻重的作用,与描述性性编程相比,更直接和易于维护,今天就和大家简单的聊聊如何进行对象库编程。

既然是对象库编程,肯定要对已存在于对象库中的对象进行编程,先准备将要使用的网页,我们将上一节的例子稍加修改,做出一个简单的用户调查页面。

点击【提交】,跳转到用户信息显示页面。

依然采用wamp环境,下面是页面代码,很简单。

  1 <html>
  2     <head>
  3         <title>web对象演示</title>
  4         <meta http-equiv="Content-type" content="text/html" charset="utf-8">
  5         <style>
  6             .content{
  7                 width:260px;
  8                 height:30px;
  9             }
 10             .edit{
 11                 width:170px;
 12                 border-color: gray;
 13                 border-width: 1px;
 14             }
 15             span{
 16                 width:70px;
 17             }
 18             table{  
 19                 border: 1px solid black;  
 20                 padding:0;   
 21                 margin:0 auto;  
 22                 border-collapse: collapse;  
 23             }  
 24           
 25             td{  
 26                 border: 1px solid black;   
 27                 font-size:12px;  
 28                 padding: 3px 3px 3px 8px;  
 29                 color: black;  
 30             }
 31         </style>
 32     </head>
 33     <body>
 34         <form action="user-info.php" method="post">
 35             <div class="content">
 36                 <span>用户名</span><input type="text" name="username" class="edit">
 37             </div>
 38             <div class="content">
 39                 <span>邮箱</span><input type="mail" name="mail"  class="edit">
 40             </div>
 41              <div class="content">
 42                 <span>个人介绍</span><textarea rows="5" cols="20" class="edit"></textarea>
 43             </div>
 44              <div class="content">
 45                  <br><span>开发语言</span>
 46                 <select name="language">
 47                     <option value ="php">php</option>
 48                     <option value ="java">java</option>
 49                     <option value ="vbs">vbs</option>
 50                     <option value ="python">python</option>
 51                 </select>
 52             </div>
 53             <div class="content">
 54                  <br><span>爱好</span>
 55                 <input type="checkbox" name="hobby[0]" value="电影" id="film">电影
 56                 <input type="checkbox" name="hobby[1]" value="游戏" id="game">游戏
 57                 <input type="checkbox" name="hobby[2]" value="阅读" id="read">阅读
 58             </div>
 59             <div class="content">
 60                  <br><span>性别</span>
 61                 <input type="radio" name="man" value="男" checked="checked"> 62                 <input type="radio" name="woman" value="女"> 63             </div>
 64             <div class="content">
 65                  <br><span>点此提交</span>
 66                 <input type="submit" name="sub" value="提交" class="button1">
 67             </div>
 68         </form>
 69         <div class="content">
 70                 <span>友情链接</span><a href="http://www.baidu.com">百度</a>
 71         </div>
 72     </body>
 73 </html> 
 74 <html>
 75     <head>
 76         <title>用户信息示</title>
 77         <meta http-equiv="Content-type" content="text/html" charset="utf-8">
 78         <style>
 79             table{  
 80                 border: 1px solid black;  
 81                 padding:0;   
 82                 margin:0 auto;  
 83                 border-collapse: collapse;  
 84             }  
 85           
 86             td{  
 87                 border: 1px solid black;   
 88                 font-size:20px;  
 89                 padding: 3px 3px 3px 8px;  
 90                 color: black;  
 91             }
 92         </style>
 93     </head>
 94     <body>
 95         <table>
 96             <tr><td>用户名</td><td>邮箱</td><td>性别</td><td>爱好</td><td>开发语言</td></tr>
 97             <?php 
 98                 if($_POST["hobby"]){
 99                     foreach($_POST["hobby"] as $value){
100                         $hobbies.=$value;
101                     } 
102                 }
103                
104                 if($_POST["sub"]){
105                     echo "<tr><td>".$_POST["username"]."</td><td>".$_POST["mail"]."</td><td>".$_POST["sex"]."</td><td>".$hobbies."</td><td>".$_POST["language"]."</td></tr>";
106                 }  
107             ?>
108         </table>
109     </body>
110 </html>
View Code

 

将对象添加至对象库

编写代码的两种方式

  • 拖动对象自动生成代码

切换左侧窗口至Available Keywords模式,将对象拖动至Expert View中。

可以看到,自动生成了一行代码,并给出了WebButton对象的基本方法Click。如果需要其他的方法,可以把.Click删除,在WebButton("提交")后输入"."即可。

  • 步骤生成器(F7)

Category中选择“Test Object”,点击Object行最右侧的按钮,选择“提交”,点击【OK】,Operation 选择 “Click”,点击【OK】。

其实还有一种编写代码的方式,那就是手动写全部代码。输入Browser和左括号会自动完成Browser("web对象演示"),在输入.Page左括号,如下图。

选择“web对象演示”,接着输入.WebButton("提交").后会显示当前对象即WebButton("提交")可以使用的方法。

获取对象属性的方法

  • GetTOProperty

获取对象库中对象的属性值,使用方法:对象.GetTOProperty(属性名)。

print  Browser("web对象演示").Page("web对象演示").WebButton("提交").GetTOProperty ("type")
print  Browser("web对象演示").Page("web对象演示").WebButton("提交").GetTOProperty ("name")
print  Browser("web对象演示").Page("web对象演示").WebButton("提交").GetTOProperty( "outerhtml")

 

运行后结果如图,与html中的代码是一致的。

如果想获取对象的全部属性,可以使用GetTOProperties方法。

set tb=Browser("web对象演示").Page("web对象演示").WebButton("提交").GetTOProperties()

For i=0 to tb.count-1
    name=tb(i).Name
    Value=tb(i).Value
    print name & "=" & Value
Next

 

  • SetTOProperty

设置对象库中对象的属性值,使用方法:对象.SetTOProperty(属性名,值)。

Browser("web对象演示").Page("web对象演示").WebButton("提交").SetTOProperty "type","edit"
Browser("web对象演示").Page("web对象演示").WebButton("提交").SetTOProperty "name","编辑框"
print Browser("web对象演示").Page("web对象演示").WebButton("提交").GetTOProperty("type")
print Browser("web对象演示").Page("web对象演示").WebButton("提交").GetTOProperty("name")

 

需要注意的是,这里对对象库中属性值的修改是临时的,在脚本运行结束时会自动还原。

  • GetROProperty

获取对象运行时的属性,使用方法:对象.GetTOProperty(属性名)。

Set tb=Browser("web对象演示").Page("web对象演示").WebEdit("用户名")
print  "对象库中Value="&tb.GetTOProperty("value")
tb.Set "测试"
print  "运行时对象库中Value="&tb.GetTOProperty("value")
print "运行时对象Value=" &tb.GetROProperty("value")

 

结果如下图,输入框中输入值以后,对象库中的value属性依然为空,GetROProperty 获取的是对象在脚本运行时的临时属性值。

开始吧

完成文章开始图片中输入效果的代码如下

Browser("web对象演示").Page("web对象演示").WebEdit("用户名").Set "test"
Browser("web对象演示").Page("web对象演示").WebEdit("邮箱").Set "11111@qq.com"
Browser("web对象演示").Page("web对象演示").WebEdit("个人介绍").Set "haha"
Browser("web对象演示").Page("web对象演示").WebList("开发语言").Select "java"
Browser("web对象演示").Page("web对象演示").WebCheckBox("电影").Set "ON"
Browser("web对象演示").Page("web对象演示").WebCheckBox("游戏").Set "ON"
Browser("web对象演示").Page("web对象演示").WebRadioGroup("性别").Select ""
Browser("web对象演示").Page("web对象演示").WebButton("提交").Click

 

如果感觉看的不舒服可以按下Ctrl+W,自动转换成with模式,还原按下Ctrl+Shit+W.

With Browser("web对象演示").Page("web对象演示")
    .WebEdit("用户名").Set "test"
    .WebEdit("邮箱").Set "11111@qq.com"
    .WebEdit("个人介绍").Set "haha"
    .WebList("开发语言").Select "java"
    .WebCheckBox("电影").Set "ON"
    .WebCheckBox("游戏").Set "ON"
    .WebRadioGroup("性别").Select "男"
    .WebButton("提交").Click
End With

如何验证结果

点击【提交】以后就跳转到了信息显示页面,如何验证信息的正确性呢?很简单,调用WebTable对象的GetCellData方法即可,一个简单的填写验证代码如下。

 1 With Browser("web对象演示")
 2     With .Page("web对象演示")
 3         .WebEdit("用户名").Set "test"
 4         .WebEdit("邮箱").Set "11111@qq.com"
 5         .WebEdit("个人介绍").Set "haha"
 6         .WebList("开发语言").Select "java"
 7         .WebCheckBox("电影").Set "ON"
 8         .WebCheckBox("游戏").Set "ON"
 9         .WebRadioGroup("性别").Select ""
10         .WebButton("提交").Click
11     End With
12 
13     With .Page("用户信息示")
14         username=.WebTable("用户名").GetCellData(2,1)
15         mail=.WebTable("用户名").GetCellData(2,2)
16         sex=.WebTable("用户名").GetCellData(2,3)
17     End With
18 End With
19 If username="test" and mail="11111@qq.com" and sex="" Then
20     msgbox "OK"
21 End If

 

posted @ 2013-11-27 21:32  huntstack  阅读(369)  评论(0编辑  收藏  举报