功能]
1.啟動一個VB的
windows application。設置好Excel文件和
QTP的安裝路徑。
2.啟動腳本進行
測試,測試代碼是QTP自帶的訂票系統。
[腳本的參數設定]
腳本中有2個參數-EXCEL文件和QTP安裝路徑從VBS文件傳入。在QTP中設定如下:
1.在KeyWord view界面。在Action1上點擊右鍵,選Action properties,彈出 Action properties對話框。在其中添加2個入參。如下圖所示:

2.設置測試參數
Test->Test Settings->Parameters.設置2個入參。如下圖所示:

3.將Action參數和Test參數關聯起來
1.在KeyWord view界面。在Action1上點擊右鍵,選擇Action Call properties

點Vaiue

在腳本中使用以下語句可以取得2個入參:
filename= Parameter("InAction1")
QtpPath= Parameter("InAction2")
[腳本部分]
Dim conn,rst,filename,coboname
Dim user,passwd
'filename="C:\DATA.xls"
filename= Parameter("InAction1")
QtpPath= Parameter("InAction2")
'datatale.import(filename)
Set conn= createobject("ADODB.Connection")
'msgbox filename
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source="&filename&";Extended Properties='Excel 8.0;hdr=yes'"
Set rst= createobject("ADODB.Recordset")
' Excelファイルのデータを読み込む
rst.Open "select * from [sheet1$] " ,conn,1,1
' table WHCT0717のデータをセットする
While Not rst.EOF
systemutil.run QtpPath&"\samples\flight\app\flight4a.exe",""
user = rst.fields("user")
passwd = rst.fields("password")
Dialog("ログイン").WinEdit("代理店名:").Set (user)
' Dialog("ログイン").WinEdit("代理店名:").Type micTab
Dialog("ログイン").WinEdit("パスワード:").set(passwd)
Dialog("ログイン").WinButton("OK").Click
reporter.filter=0
If ( Dialog("ログイン").Dialog("フライト予約").WinButton("OK").Exist(2) ) Then
text = Dialog("ログイン").Dialog("フライト予約").GetVisibleText
reporter.ReportEvent micFail ,"Load Error",text
Dialog("ログイン").Dialog("フライト予約").WinButton("OK").Click
Dialog("ログイン").WinButton("キャンセル").Click
else
Window("フライト予約").close
end if
rst.MoveNext
Wend
rst.close
腳本部分最主要的是注意QTP對象的選擇和使用。(Tools-->object Repository 對象的添加和刪除)
[VB application]
以下是Button1單擊的觸發事件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim qtApp
Dim qtTest
Dim qtResultsOpt
Dim pDefColl, pDef, rtParams, rtParam1, rtParam2
Dim QtpTestPath
qtApp = CreateObject("QuickTest.Application")
qtApp.Launch()
qtApp.Visible = True '使得QTP的程序可見
' 設置運行屬性
QtpTestPath = QtpPath.Text & "\Tests\Test3" '設置腳本路徑
qtApp.Options.Run.RunMode = "Fast"
qtApp.Open(QtpTestPath, True) ' Open the test in read-only mode
' set run settings for the test
qtTest = qtApp.Test
qtTest.Settings.Run.OnError = "NextStep"
qtResultsOpt = CreateObject("QuickTest.RunResultsOptions")
qtResultsOpt.ResultsLocation = "D:\program files\Mercury Interactive\QuickTest Professional\Tests\Test3" ' 設置放結果的地方
pDefColl = qtApp.Test.ParameterDefinitions
Dim cnt = pDefColl.Count
Dim Indx = 1
While Indx <= cnt
pDef = pDefColl.Item(Indx)
Indx = Indx + 1
End While
rtParams = pDefColl.GetParameters()
rtParam1 = rtParams.Item("InParameter1")
rtParam1.Value = DataFileName.Text
' MsgBox(TextBox1.Text)
rtParam2 = rtParams.Item("InParameter2")
rtParam2.Value = QtpPath.Text
qtTest.Run(, True, rtParams) ' Run the test
'MsgBox(rtParams.Item("OutParameter1").Value)
'qtTest.Close ' Close the test
qtResultsOpt = Nothing ' Release the Run Results Options object
qtTest = Nothing ' Release the Test object
qtApp = Nothing ' Release the Application object
qtTest.quit()
End Sub
以上幾個部分雖然不是很多,然因為剛剛開始接觸QTP,所以花了我近一天時間。主要是改成手動處理后
對象的取得和邏輯判斷的問題比較多,花了不少時間。希望對大家有用。