學習《QTP自動化測試進階》第20章 – 使用QTP開發一個猴子測試工具
“猴子測試”,也叫隨機測試,因爲它的原理是利用測試工具隨機產生鍵盤敲擊和鼠標點擊事件,就像一隻大猩猩在狂敲鍵盤,因此,也叫猴子測試。
產生隨機數:
Dim MyValue, Response
' 初始化隨機數產生器
Randomize
Do Until Response = vbNo
' 隨機選取0到10之間的整數
MyValue = Int((10 * Rnd+0) )
MsgBox MyValue
Response = MsgBox ("重新來一遍? ", vbYesNo)
Loop
通過WMI對象獲取屏幕高度和寬度:
strComputer = "."
' 獲取WMI對象
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
' 通過WMI查詢桌面顯示器的高度和寬度
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_DesktopMonitor",,48)
For Each objItem in colItems
Height = objItem.ScreenHeight
Width = objItem.ScreenWidth
Next
MsgBox Width & "," & Height
調用Win32API的鼠標函數:
' 通過Extern訪問Windows API函數
Extern.Declare micVoid, "MouseEvent","user32.dll","mouse_event",
micInteger,micInteger ,micInteger ,micInteger ,micInteger
' 把鼠標移到該位置
Extern.MouseEvent 80+1,10, 100, 0, 0
' 鼠標左鍵單擊
Extern.MouseEvent 2+4,0,0,0,0
Extern.Declare micInteger, "WindowFromPoint","user32.dll",
"WindowFromPoint",micInteger,micInteger
Msgbox Extern.WindowFromPoint (0,0)
截屏可用:
DeskTop.CaptureBitmap
參考資源:
1、《一個簡單的猴子測試小工具》:
http://blog.csdn.net/Testing_is_believing/archive/2007/12/05/1919200.aspx
2、《使用猴子測試工具》:
http://blog.csdn.net/Testing_is_believing/archive/2007/11/18/1891312.aspx
3、這個網站有很多WMI的例子代碼可以參考:
http://www.robvanderwoude.com/wmiexamples.php