QTP實(shí)用函數(shù)(包括WEBTABLE等)
自動(dòng)化測(cè)試 2009-11-17 15:57 閱讀16 評(píng)論0 字號(hào): 大大 中中 小小 DTParameter對(duì)象指的是運(yùn)行時(shí)DataTable Sheet中的列對(duì)象。
注意:所有應(yīng)用于DTParameter對(duì)象的方法僅適用于run-time DataTable對(duì)象。所有對(duì)run-time DataTable對(duì)象的改變只影響測(cè)試結(jié)果,不影響design-time Data Table。
1. Property
Name
描述
返回run-time Data Table的列名。
語法
DTParameter.Name
示例
下面的例子使用“Name”方法返回run-time Data Table中新創(chuàng)建的列的名稱,并將列名寫入Report。
Dim paramname
paramname = DataTable.LocalSheet.AddParameter("Food", "pizza").Name
Reporter.ReportEvent 1, "The New Parameter name is", paramname
RawValue
描述
獲取當(dāng)前行指定列所對(duì)應(yīng)的單元格的原始數(shù)據(jù)。原始數(shù)據(jù)是尚未計(jì)算處理過的數(shù)據(jù),如單元格中的公式內(nèi)容等。
語法
DTParameter.RawValue
本語法與DataTable.RawValueParameterID[,SheetID]用途是一樣的,但是使用方法不同。
示例
下面的例子使用RawValue屬性來獲取run-time Data Table的“ActionA”表“Date”列的當(dāng)前行所對(duì)應(yīng)的單元格中的公式。本例中的返回值應(yīng)該是“=Now()“。
FormulaVal=DataTable.GetSheet("ActionA").GetParameter("Date").RawValue
Value
描述
這個(gè)屬性是列對(duì)象的默認(rèn)屬性。獲取或設(shè)置列的當(dāng)前行所對(duì)應(yīng)的單元格的數(shù)據(jù)。
注意:這個(gè)方法返回的數(shù)據(jù)是計(jì)算后的數(shù)據(jù)。假如單元格中包括公式,則這個(gè)方法返回True或False。
語法
獲取單元格數(shù)據(jù):
DTParameter.Value or DTParameter
設(shè)置單元格數(shù)據(jù):
DTParameter.Value=newvalueor DTParameter=newvalue
示例
下面的例子使用Value來設(shè)置Sheet“ActionA”的“Destination”列的當(dāng)前行所對(duì)應(yīng)的單元格的數(shù)據(jù)。
DataTable.GetSheet("ActionA").GetParameter("Destination").Value="New York"
注意:可以省略本語句中的“Value”,因?yàn)閂alue是DTParameter的默認(rèn)屬性。
ValueByRow
描述
獲取指定列指定行所對(duì)應(yīng)的單元格的數(shù)據(jù)。
語法
DTParameter.ValueByRow(RowNum)
Argument Type Descrīption
RowNum Number 指定行號(hào)。行號(hào)從1開始。
示例
下面的例子使用“ValueByRow”來獲得表“ActionA”的“Destination”列的第4行數(shù)據(jù)。
DataTable.GetSheet("ActionA").GetParameter("Destination").ValueByRow(4)
1.GetCellData函數(shù)
作用:獲取單元格的值
例: rowCount = Browser("xxx ").Page("xxx ").Frame("xxx").WebTable("xxx").RowCount
For counter = 1 To rowCount
text = Browser("xxx").Page("xxx").Frame("xxx").WebTable("xxx").GetCellData(counter,1)
If (text = "xxx") Then
counter = counter - 1
selectNO = "#" & counter
Browser("xxx").Page("xxx").Frame("xxx").WebRadioGroup("xxx").Select selectNO
Exit For
End If
Next
2.把值插入datatable里
例: datatable.setcurrentrow(i)
datatable.value("name","Global")="name"
datatable.value("passwd","Global")="passwd"
3.用代碼來啟動(dòng)瀏覽器
Browser1 = "IE"
StartURL = "www.51testing.com"
IF Browser1 = "IE" THEN
set IE = CreateObject("InternetExplorer.Application")
IE.Visible = true
IE.Navigate StartURL
END IF
4.ExecuteFile函數(shù)
作用:ExecuteFile 可以直接執(zhí)行vbs文件,而不需要將其導(dǎo)入resource中
ExecuteFile FileName
說明:where FileName is the absolute or relative path of your VBscrīpt file.
例:ExecuteFile("F:"test.vbs")
5.Strcomp函數(shù)
作用:比較文本
例:dim strtext1,strtext2,str ,str1,comp1
strtext1 = "xxx"
strtext2 = "xxx"
str = VbWindow("xxx").VbWindow("xxx").VbLabe1("xxx").GetTOProperty("text")
str1= VbWindow("xxx").VbWindow("xxx").VbLabel("xxx").GetTOProperty("text")
comp1=strcomp(strtext1,str,0)
If comp=0 Then
msgbox “這兩個(gè)串相等”
else
msgbox str
End If
6.CaptureBitmap
作用:捕獲屏幕
7. GetROProperty
作用:取對(duì)象屬性值
例:VbWindow("xxx").VbWindow("xxx").VbWindow("xxx").ActiveX("xxx").GetROProperty("TextMatrix(1,0)")
8.ExitAction - 退出當(dāng)前操作,無論其循環(huán)屬性如何。
ExitActionIteration - 退出操作的當(dāng)前循環(huán)。
ExitRun - 退出測(cè)試,無論其循環(huán)屬性如何。
ExitGlobalIteration - 退出當(dāng)前全局循環(huán)。
9.如何使用Excel對(duì)象處理數(shù)據(jù)?
Dim xl
打開excel文件
Function OpenExcelFile(strFilePath)
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open strFilePath
End Function
獲得指定單元格數(shù)據(jù)
Function GetCellData(strSheet,rwIndex,colIndex)
GetCellData = xl.WorkSheets(strSheet).Cells(rwIndex,colIndex)
End Function
填充單元格數(shù)據(jù)
Function PutCellData(strSheet,rwIndex,colIndex,varData)
xl.WorkSheets(strSheet).Cells(rwIndex,colIndex) = varData
End Function
保存并推出
Function SaveAndQuit()
xl.Activeworkbook.save
xl.Quit
Set xl = nothing
End Function
10.連接sql數(shù)據(jù)庫
例im res,cmd,sql
Set Res=createobject("adodb.recordset")
Set Cmd=createobject("adodb.command")
Cmd.activeconnection="rovider=SQLOLEDB.1assword=111111ersist Security Info=True;User ID=sa;Initial Catalog=xhq;Data Source=192.168.191.142" ?。н@句話是連接數(shù)據(jù)庫的數(shù)據(jù)源,要做修改
Cmd.CommandType = 1
sql="selec t * from 表 where name=username"
Cmd.CommandText = sql
Set res = Cmd.Execute()
Set res = nothing
Set cmd.ActiveConnection = nothing
Set Cmd= nothing
11 檢查頁面是否存在 對(duì)象exist可以檢查某一個(gè)頁面是否存在。 代碼: if Browser("…").Page(“…").Exist then ‘在運(yùn)行結(jié)果中顯示的報(bào)告, “micPass”的狀態(tài)是通過, micFail是不通過 reporter.ReportEvent micPass ,“頁面存在“,”通過“ else reporter.ReportEvent micFail ,"頁面不存在“,"不通過" end if
12 防止程序中斷的方法 在回放腳本的時(shí)候,有時(shí)因?yàn)殄e(cuò)誤導(dǎo)致運(yùn)行的腳本中斷,不能自動(dòng)運(yùn)行。為了能達(dá)到真正無人職守的狀態(tài)可以在腳本的最前面加上如下的代碼: On error resume next ‘遇到錯(cuò)誤返回到腳本的下一行繼續(xù)執(zhí)行。 On error goto 0 ‘錯(cuò)誤處理的控制權(quán),平時(shí)是由QTP控制的(這個(gè)叫默認(rèn)的),當(dāng)有on error resume next 時(shí),是交給on error處理,當(dāng)on error goto 0時(shí),就換給QTP
13 同步點(diǎn)的設(shè)定 等待某一對(duì)象出現(xiàn)后繼續(xù)執(zhí)行,為了防止qtp找不到對(duì)象而設(shè)定同步點(diǎn)。有2種方法:1種是用wait加等待的時(shí)間,如wait 5(qtp等待5秒鐘后繼續(xù)執(zhí)行)。另一種方法是等待要執(zhí)行對(duì)象的出現(xiàn),如果出現(xiàn)就繼續(xù)執(zhí)行,否則一直等待,代碼如下: y=......waitproperty("visible",true,10000) If y=true then ‘執(zhí)行下一條語句 else ’對(duì)象不出現(xiàn)就一直等待,直到過了10000秒后程序找不到對(duì)象報(bào)錯(cuò) End if
14 截屏 在優(yōu)化腳本時(shí),如果想查看某一頁面在執(zhí)行后的頁面效果,可以采取截屏的辦法截取當(dāng)前的操作頁面并保存到本地。代碼: desktop. capturebitmap “c:"***.bmp“,ture ’在腳本中想要查看的一行插入,運(yùn)行后圖片保存到設(shè)置的路徑下
15 導(dǎo)入execl文件并參數(shù)化數(shù)據(jù)方法 Qtp自帶了datatable表,可以把要參數(shù)化的數(shù)據(jù)寫在里面,但這樣寫腳本和數(shù)據(jù)不能分離,后期不好維護(hù)腳本?,F(xiàn)在采用從外部導(dǎo)入execl文件的方法導(dǎo)入數(shù)據(jù)。代碼如下: datatable.ImportSheet “D:"..."data.xls”,“sheet1”,“global“ ‘第一個(gè)參數(shù)是要導(dǎo)入文件的路徑,第二個(gè)是execl的第一個(gè)表格,第三個(gè)參數(shù)是在execl的全局范圍內(nèi)查找 另外datatable對(duì)象還有很多操作,比如:獲取表中字段的行數(shù),插入表數(shù)據(jù),刪除數(shù)據(jù)等,下面是獲取表中數(shù)據(jù)的代碼:datatable("A","dtglobalsheet)
16 為描述性編程自動(dòng)創(chuàng)建注釋 當(dāng)希望在每一個(gè)新建action時(shí)都增加一些頭部說明,比如作者、創(chuàng)建日期、說明等信息,那么用action template 來實(shí)現(xiàn)最簡(jiǎn)單快捷。 方法:用記事本等文本編輯器,輸入如下類似的內(nèi)容: 'Company:東方般若 'Date: Date 然后將文件保存為ActionTemplate.mst,并存放到QTP安裝目錄下的dat目錄。
17 導(dǎo)入vbs文件 想要實(shí)現(xiàn)腳本的函數(shù)化,并更好的維護(hù)它,可以把一些公用的函數(shù)寫到vbs里面,用qtp來調(diào)用它。 實(shí)現(xiàn)調(diào)用vbs的方法有2種: 1.函數(shù)executefile加vbs文件的路徑 executefile “c:"..."funcation.vbs” 2.設(shè)置QTP test/settings/resources/+函數(shù)目錄
18 時(shí)間差函數(shù) 統(tǒng)計(jì)兩個(gè)日期時(shí)間段之間的間隔,還有多少小時(shí) Dim timediff timediff=datediff(“H”,now,“2008-8-8” ) Print timediff | |