QTP自传之对象库编程

时间:2023-03-09 14:29:08
QTP自传之对象库编程

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

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

QTP自传之对象库编程

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

QTP自传之对象库编程

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

 <html>
<head>
<title>web对象演示</title>
<meta http-equiv="Content-type" content="text/html" charset="utf-8">
<style>
.content{
width:260px;
height:30px;
}
.edit{
width:170px;
border-color: gray;
border-width: 1px;
}
span{
width:70px;
}
table{
border: 1px solid black;
padding:0;
margin:0 auto;
border-collapse: collapse;
} td{
border: 1px solid black;
font-size:12px;
padding: 3px 3px 3px 8px;
color: black;
}
</style>
</head>
<body>
<form action="user-info.php" method="post">
<div class="content">
<span>用户名</span><input type="text" name="username" class="edit">
</div>
<div class="content">
<span>邮箱</span><input type="mail" name="mail" class="edit">
</div>
<div class="content">
<span>个人介绍</span><textarea rows="5" cols="20" class="edit"></textarea>
</div>
<div class="content">
<br><span>开发语言</span>
<select name="language">
<option value ="php">php</option>
<option value ="java">java</option>
<option value ="vbs">vbs</option>
<option value ="python">python</option>
</select>
</div>
<div class="content">
<br><span>爱好</span>
<input type="checkbox" name="hobby[0]" value="电影" id="film">电影
<input type="checkbox" name="hobby[1]" value="游戏" id="game">游戏
<input type="checkbox" name="hobby[2]" value="阅读" id="read">阅读
</div>
<div class="content">
<br><span>性别</span>
<input type="radio" name="man" value="男" checked="checked">男
<input type="radio" name="woman" value="女">女
</div>
<div class="content">
<br><span>点此提交</span>
<input type="submit" name="sub" value="提交" class="button1">
</div>
</form>
<div class="content">
<span>友情链接</span><a href="http://www.baidu.com">百度</a>
</div>
</body>
</html>
<html>
<head>
<title>用户信息示</title>
<meta http-equiv="Content-type" content="text/html" charset="utf-8">
<style>
table{
border: 1px solid black;
padding:0;
margin:0 auto;
border-collapse: collapse;
} td{
border: 1px solid black;
font-size:20px;
padding: 3px 3px 3px 8px;
color: black;
}
</style>
</head>
<body>
<table>
<tr><td>用户名</td><td>邮箱</td><td>性别</td><td>爱好</td><td>开发语言</td></tr>
<?php
if($_POST["hobby"]){
foreach($_POST["hobby"] as $value){
$hobbies.=$value;
}
} if($_POST["sub"]){
echo "<tr><td>".$_POST["username"]."</td><td>".$_POST["mail"]."</td><td>".$_POST["sex"]."</td><td>".$hobbies."</td><td>".$_POST["language"]."</td></tr>";
}
?>
</table>
</body>
</html>

将对象添加至对象库

QTP自传之对象库编程

编写代码的两种方式

  • 拖动对象自动生成代码

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

QTP自传之对象库编程

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

  • 步骤生成器(F7)

QTP自传之对象库编程

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

QTP自传之对象库编程

QTP自传之对象库编程

QTP自传之对象库编程

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

QTP自传之对象库编程

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

QTP自传之对象库编程

获取对象属性的方法

  • 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中的代码是一致的。

QTP自传之对象库编程

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

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

For i= to tb.count-
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 获取的是对象在脚本运行时的临时属性值。

QTP自传之对象库编程

开始吧

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

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.

QTP自传之对象库编程

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方法即可,一个简单的填写验证代码如下。

 With Browser("web对象演示")
With .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 With .Page("用户信息示")
username=.WebTable("用户名").GetCellData(,)
mail=.WebTable("用户名").GetCellData(,)
sex=.WebTable("用户名").GetCellData(,)
End With
End With
If username="test" and mail="11111@qq.com" and sex="男" Then
msgbox "OK"
End If