QTP描述性编程个人经验
不过,里面描述的内容是正确的,至于实例不能运行,那应该是多方面的小小失误造成的。
好了,以上是我开始时的困境,相信我不是第一也不会是最后,所以把最近QTP描述性编程的成果和感受与大家分享一下,希望对也想学QTP描述性编程的同志有所启发……
所谓QTP描述性编程,就是指用编程的方式来实现QTP的功能,而不是用录制的方式。这里初学QTP失很容易问为什么。是的,为什么要这样,最好的答案就是你用描述性编程的方式来试试吧,在这个过程中,你会感悟到。我个人的感悟来看:1.不需要维护大量测试对像库
2.方便维护;
3.很多功能需要用描述性编程来实现,仅用录制的方法,无法做到或很复杂;
4.减少测试文件容量。
对个人来讲,用QTP描述性编程会使你的QTP技能上几个大台阶,上升到高手是必修课,它需要用到很多在录制过程中的不用的方法和函数,这样使你的VBS大大的进步。
QTP描述性编程语法格式:
对象类名("属性名:=属性值","属性名:=属性值"....).控件类名("属性名:=属性值",...).方法
如:以QTP自带的订票Flight Samples为例
dialog("text:=login").WinEdit("attached text:=Password:").Set "mercury"
说明:Dialog是登录页面的类名;login是登录页面的标题(可以唯一识别,就只要一个属性及可);
WinEdi是密码输入框的控制类名,attached text:=Password表示属性attached text的属性是password:(简单点就是密码输入框的名称);
Set "mercury,就是用SET方法输入密码:mercury
以下面是我用QTP描述性编程写的Flight Samples中,正确登录,输入正确订票信息到Insert Order之前的代码,有兴趣的话,拿到QTP里试试。
'正确登录
dialog("text:=login").WinEdit("attached text:=agent name:").Set "aaaaa"
dialog("text:=login").WinEdit("attached text:=Password:").Set "mercury"
dialog("text:=login").winbutton("text:=Ok").Click
dialog("text:=login").WinEdit("attached text:=agent name:").Set "aaaaa"
dialog("text:=login").WinEdit("attached text:=Password:").Set "mercury"
dialog("text:=login").winbutton("text:=Ok").Click
正确订票
Window("text:=flight reservation").WinButton("window id:=6").Click
window("text:=flight reservation").ActiveX("acx_name:=MaskEdBox").Type "101011"
window("text:=flight reservation").WinComboBox("attached text:=Fly From:").Select "Denver"
window("text:=flight reservation").WinComboBox("attached text:=Fly To:").Select "Frankfurt"
Window("text:=flight reservation").winButton("text:=Flight").Click
Window("text:=flight reservation").WinButton("window id:=6").Click
window("text:=flight reservation").ActiveX("acx_name:=MaskEdBox").Type "101011"
window("text:=flight reservation").WinComboBox("attached text:=Fly From:").Select "Denver"
window("text:=flight reservation").WinComboBox("attached text:=Fly To:").Select "Frankfurt"
Window("text:=flight reservation").winButton("text:=Flight").Click
'选择航班
dialog("text:=flights table").WinList("attached text:=From","Window ID:=2001").SetTOProperty"selection","13628 DEN 02:09 PM FRA 02:53 PM AF $124.80"
Dim FlightArray,FlightSelection
FlightSelection=dialog("text:=flights table").WinList("attached text:=From","window ID:=2001").GetRoProperty("selection")
FlightArray=Split(FlightSelection," ",-1,1)
dialog("text:=flights table").Winbutton("text:=OK").Click
'检查订票信息和所选航班信息是否一致
window("text:=Flight reservation").WinEdit("attached text:=Flight no:","Window ID:=1024").CheckProperty "text",FlightArray(0),100
window("text:=Flight reservation").WinEdit("attached text:=Departure Time:","Window ID:=2012").CheckProperty "text",FlightArray(2),100
window("text:=Flight reservation").WinEdit("attached text:=Arrival Time:","Window ID:=1028").CheckProperty "text",FlightArray(4),100
Window("text:=flight reservation").WInEdit("attached text:=Airline:","window id:=1017").CheckProperty "text",FlightArray(5),100
window("text:=Flight reservation").WinEdit("attached text:=Price:","Window ID:=1030").CheckProperty "regexpwndtitle",FlightArray(6),100
window("text:=Flight reservation").WinEdit("attached text:=Total:","Window ID:=1031").CheckProperty "regexpwndtitle",FlightArray(6),100
dialog("text:=flights table").WinList("attached text:=From","Window ID:=2001").SetTOProperty"selection","13628 DEN 02:09 PM FRA 02:53 PM AF $124.80"
Dim FlightArray,FlightSelection
FlightSelection=dialog("text:=flights table").WinList("attached text:=From","window ID:=2001").GetRoProperty("selection")
FlightArray=Split(FlightSelection," ",-1,1)
dialog("text:=flights table").Winbutton("text:=OK").Click
'检查订票信息和所选航班信息是否一致
window("text:=Flight reservation").WinEdit("attached text:=Flight no:","Window ID:=1024").CheckProperty "text",FlightArray(0),100
window("text:=Flight reservation").WinEdit("attached text:=Departure Time:","Window ID:=2012").CheckProperty "text",FlightArray(2),100
window("text:=Flight reservation").WinEdit("attached text:=Arrival Time:","Window ID:=1028").CheckProperty "text",FlightArray(4),100
Window("text:=flight reservation").WInEdit("attached text:=Airline:","window id:=1017").CheckProperty "text",FlightArray(5),100
window("text:=Flight reservation").WinEdit("attached text:=Price:","Window ID:=1030").CheckProperty "regexpwndtitle",FlightArray(6),100
window("text:=Flight reservation").WinEdit("attached text:=Total:","Window ID:=1031").CheckProperty "regexpwndtitle",FlightArray(6),100