通常QTP是通過對象庫來識別不同的對象,而描述性編程是QTP另外一種能夠識別對象的途徑,它不依賴于對象庫,通過增加一些對象的描述來識別對象的。
說明:本例子是以Flight飛機訂票系統的登陸界面為測試頁面進行描述的。
步驟一:錄制腳本
Dialog("Login").WinEdit("Agent Name:").Set "Holly"
Dialog("Login").WinEdit("Password:").SetSecure "46ef0dc7efe5834c73673898279af1204fea51a7"
Dialog("Login").WinButton("Cancel").Click
共錄制3步操作,輸入Agent Name, Password, 點擊Cancel按鈕
步驟二:初級描述性編程
Dialog("Regexpwndtitle:=Login").WinEdit("Attached text:=Agent Name:").Set "Holly"
Dialog("Regexpwndtitle:=Login").WinEdit("Attached text:=Password:").Set “Mercury”
Dialog("Regexpwndtitle:=Login").WinButton("Class Name:=WinButton", "text:=Cancel").Click
在這里要注意有三點:
1)如果需要兩個以上特性來描述一個對象,需要使用逗號(,)對描述性語言進行分割
2)使用:=來連接屬性和屬性值,并且:=兩邊不能有空格
3)使用SPY查看對象的屬性名和屬性值(Tools -> Object Spy)
步驟三:描述性編程提高
Dim descEditLogin
Set descEditLogin = Description.Create()
descEditLogin("Class Name").Value = "Dialog"
descEditLogin("Regexpwndtitle").Value = "Login"
Dialog(descEditLogin).WinEdit("Attached text:=Agent Name:").Set "Holly"
Dialog(descEditLogin).WinEdit("Attached text:=Password:").Set "Mercury"
Dialog(descEditLogin).WinButton("Class Name:=WinButton", "text:=Cancel").Click
在這里需要注意有兩點:
1)把經常使用到的對象定義為一個對象變量,方便以后調用,減少代碼工作量和錯誤
2)使用SPY獲取對象的屬性和屬性值
步驟四:使用自定義的環境變量
在File>>Settings>>Environment中選擇user-defined,增加一個變量
dlgLogin = “Login”
這樣腳本可以被修改為:
Dim descEditLogin
Set descEditLogin = Description.Create()
descEditLogin("Class Name").Value = "Dialog"
descEditLogin("Regexpwndtitle").Value = Environment.Value("dlgLogin")
Dialog(descEditLogin).WinEdit("Attached text:=Agent Name:").Set "Holly"
Dialog(descEditLogin).WinEdit("Attached text:=Password:").Set "Mercury"
Dialog(descEditLogin).WinButton("Class Name:=WinButton", "text:=Cancel").Click
當然,參數化的方式很多,這邊介紹的是使用環境變量
步驟五:從XML文件導入環境變量
<Enviroment>
<Variable>
<Name>dlgLogin</Name>
<Value>Login</Value>
</Variable>
</Environment>
可以使用手工導入,也可以使用LoadFromFile自動導入
總結:
優點是當對象的一些屬性變更后,腳本更容易維護。
比如說對于一個通用對象,比如save, reset, cancel等按鈕,一個頁面有3個,30個頁面就有90個對象,
假如save變成保存,reset變成重置,cancel變成取消,那么對象庫就會產生很大的變動。
而使用了描述性編程只需要在導入的XML文件中修改一個值就可以了。
當然描述性編程的作用遠遠不止這些,這次只是拋磚引玉,希望大家共同進步。
延伸閱讀
文章來源于領測軟件測試網 http://www.kjueaiud.com/