TC中等待測試對象的問題 軟件測試
在編寫自動化測試腳本時,等待測試對象出現,然后針對該測試對象執行相關的操作,是一個常見的腳本設計問題。在TC中,有如下幾種方法:
(1)Web頁面的等待。
在QTP中,可以使用Browser().page().sync 的方法來等待某個頁面完成加載,而在TC中,可使用Page.ToUrl、Page.Wait 或 WaitPage 等方法,例如下面的例子用于等到指定URL地址所指的頁面加載完成并出現指定的頁面元素:
' Obtain the Page object
' We assume that the browser window belongs to the IExplore process, not Explorer
Set p1 = Sys.Process("iexplore")
Set w1 = p1.Window("IEFrame", "*", 1).Window("Shell DocObject View", "", 1).Window("Internet Explorer_Server", "", 1)
Set Pg = w1.Page("*")
' Opening the page
Pg.ToUrl "my url"
' Waiting for frames
waitFrm = true
While waitFrm
For i = 0 To Pg.document.frames.ChildCount - 1
Set Frame = Pg.document.frames.Child(i)
If Frame.Exists Then
waitFrm = waitFrm And (Frame.readyStateValue <> 4)
End If
Next
If waitFrm Then
BuiltIn.Delay(100)
End If
WEnd
...
(2)等待進程。
等待某個進程出現,可使用WaitProcess方法,例如下面的例子:
Set p = Sys.WaitProcess("winword", 2000)
If p.Exists Then' if the process exists ...
...
(3)等待窗口。
等待窗口創建完畢,可以使用WaitWindow方法,例如下面的例子:
Set p = Sys.Process("Notepad")
' Waits 10 seconds for the window
Set w = p.WaitWindow("*", "Open*", -1, 10000)
If w.Exists Then
w.Activate
Log.Picture w, "Open dialog picture"
Else
Log.Warning "Incorrect window"
End If
(4)等待對象屬性。
等待某個對象的屬性等于指定的狀態值,可使用WaitProperty方法,例如下面的例子:
Set btn = Sys.Process("MyApp").Window("TMainFrm","MyApplication *").Child(2)
If btn.WaitProperty("Enabled", True, 2000) Then
' Button is enabled
Else
' Button is disabled
WEnd
(5)等待某個兒子對象的出現。
等待測試對象下的某個兒子對象的出現可用WaitChild方法,例如下面的例子:
If Obj.WaitChild(ChildObjectName, 0).Exists Then
' Child object exists
Else
' There is no such a child
End If
原文轉自:http://www.anti-gravitydesign.com