Browser("百度一下,你就知道").Page("百度一下,你就知道").WebEdit("wd").Set DataTable("searchContent",dtGlobalSheet)html
For i=1 to Datatable.GetRowCount
Dialog("Login").WinEdit("Agent Name:").Set DataTable("username", dtGlobalSheet)
Dialog("Login").WinEdit("Password:").Set DataTable("passwd", dtGlobalSheet)
Dialog("Login").WinButton("OK").Click
datatable.GlobalSheet.SetNextRow
Nextsql
MsgBox "提示信息:"&vbCrLf&"操做成功",vbInformation,"Title"數據庫
2.在介紹對象庫前首先要說一下QTP自動化的原理express
(1).封裝被測對象到對象庫數組
(2).對比對象庫裏的對象屬性和運行時的真實被測對象的屬性瀏覽器
(3).對比一致後找到相應的對象ide
Resources-->Object Repository-->Object -->Add Objects to Local函數
mercury.externObj ---- 用於建立EXTERN對象 測試
'#######啓動瀏覽器########
systemutil.Run "iexplore.exe" ui
'#######建立API extern對象##########
Set myExtern = createobject("mercury.externObj")
myExtern.Declare micHWnd, "GetForegroundWindow", "user32.dll", "GetForegroundWindow"
'######使用WIN32 API獲取窗口的句柄######
hwnd = myExtern.GetForegroundWindow()
'########經過獲取的句柄控制瀏覽器窗口並跳轉百度#####
Browser("hwnd:=" & hwnd).Navigate "http://www.baidu.com"
'############釋放對象實例########
Set myExtern = nothing
進入到HKEY_CURRENT_USER\SOFTWARE\MERCURY INTERACTIVE\QUICKTEST PROFESSIONAL\MICTEST\RESERVEDOBJECTS\EXTERN下的PROGID
例1:獲取單元格中的值
thisText = Browser(…).Page(…).Frame.(…).WebTable("sample").GetCellData(2,1)
例2:獲取圖片的名稱
ObjectName = Browser(…).Page(…).Image("Find").GetProperty("Name")
例3:檢查某個對象是否存在,若是存在彈出對話框說明對象存在。
If Browser("Browser").Page("Page").Applet("login.html").JavaEdit("username").Exist Then
MsgBox("The object exists.")
End if
僅有一個頁面時的關閉方法:Window("Text:=Yahoo! - Microsoft Internet Explorer").Close
有多個頁面時的關閉方法:
Dim i
i = 0
while (Window("Text:=Yahoo! - Microsoft Internet Explorer", "index:="&i).exist)
Window("Text:=Yahoo! - Microsoft Internet Explorer", "index:="&i).close
i = i +1
wend
如下語句指定 Mercury Tours 頁面中名爲author且索引值爲 3 的 WebEdit 測試對象。當測試運行時,QTP 將查找具備匹配屬性值的 WebEdit 對象,並輸入文本jojo。
Browser("Mercury Tours").Page("Title:=Mercury Tours").WebEdit("Name:=Author", "Index:=3").Set "jojo"
此外,若是咱們但願在在一個測試或組件中屢次使用相同的Descriptive Programming,則能夠將建立的對象賦值給變量,這樣使用會方便不少。
例如:咱們須要完成下面一系列操做
Window("Text:=HyperSna").WinButton("Caption:=日期").Click
Window("Text:=HyperSna").WinButton("Caption:=時間").Click
Window("Text:=HyperSna").WinButton("Caption:=肯定").Click
那麼,爲了方便其見,咱們能夠將Window("Text:=HyperSna")賦值到一個變量,而後再使用,參見下面的代碼:
Set WinHyper = Window("Text:=HyperSna")
WinHyper.WinButton("Caption:=日期").Click
WinHyper.WinButton("Caption:=時間").Click
WinHyper.WinButton("Caption:=肯定").Click
若是使用了VBScript裏面的With語句,還能夠簡化爲如下代碼:
With Window("Text:=HyperSna")
.WinButton("Caption:=日期").Click
.WinButton("Caption:=時間").Click
.WinButton("Caption:=肯定").Click
End With
Description應用:
在 Properties 集合中填充一組 Property 對象(屬性和值)後,能夠在測試語句中指定用 Properties 對象代替對象名。
例如,假設咱們須要完成如下一個操做:
Window("Error").WinButton("text:=OK", "index:=1").Click
咱們能夠經過Description對象來實現一樣的功能,參加下面的代碼:
Set MyDescription = Description.Create()
MyDescription("text").Value = "OK"
MyDescription("index").Value = 1
Window("Error").WinButton(MyDescription).Click
Set MyDescription = Nothing
Left函數:Left(string, length)
Dim MyString, LeftString
MyString = "VBscrīpt"
LeftString = Left(MyString, 3) ' LeftString contains "VBS".
Mid函數:從字符串中返回指定數目的字符。Mid(string, start[, length])
Dim MyVar
MyVar = Mid("VB scrīpt is fun!", 4, 6) ' MyVar contains "scrīpt".
InStr函數(InStrRev函數相反從最後向前起)
返回指定的字符串在另外一字符串中最早出現的位置。
InStr([start, ]string1, string2[, compare])
參數:
start:起始位置,默認從第一位
string1:主體字符串,從左向右查找。若是string1爲 Null,則返回 Null。
string2:查找的字符串,若是string2爲 Null,則返回 Null。找不到就返回0。
compare:0是二進制比較,1是文本比較。0爲缺省值。我的感受區別就是在大小寫。
例子
Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
MyPos = Instr(4, SearchString, SearchChar, 1) ' A textual comparison starting at position 4. Returns 6.
MyPos = Instr(1, SearchString, SearchChar, 0) ' A binary comparison starting at position 1. Returns 9.
MyPos = Instr(SearchString, SearchChar) ' Comparison is binary by default (last argument is omitted). Returns 9.
MyPos = Instr(1, SearchString, "W") ' A binary comparison starting at position 1. Returns 0 ("W" is not found).
******************
Split 函數
在指定的 delimiter 參數出現的全部位置斷開 String 對象,將其拆分爲子字符串,而後以數組形式返回子字符串。
Split(expression[, delimiter[, count[, compare]]])
參數
expression:主體字符串,也就是要被拆分處的字符或字符串。
delimiter:拆分元素,默認是(" ")
count:Number [可選] 要放入數組中的項目數。
compare:0是二進制比較,1是文本比較。0爲缺省值。
例子
Dim MyString, MyArray, Msg
MyString = "VBscrīptXisXfun!"
MyArray = Split(MyString, "x", -1, 1)
' MyArray(0) contains "VBscrīpt".
' MyArray(1) contains "is".
' MyArray(2) contains "fun!".
Msg = MyArray(0) & " " & MyArray(1)
Msg = Msg & " " & MyArray(2)
MsgBox Msg
環境初始化
Dim loginwin
Set loginwin=descrīption.Create() '建立對象名爲loginwin的對象
loginwin( "regexpwndtitle").value="Login" ,對象屬性及值
Set child=desktop.ChildObjects (loginwin)
For i=1 to child.count
child(i-1).close '循環尋找這個對象,一次關閉
Next
------------------ Set excelObj = CreateObject("Excel.Applicaiton") '建立一個對象 xlsUrl = "C:\QTP\0110\sql-excel-qtp\data.xls" '指定一個已經存在的XSL文件路徑 excelObj.workbooks.open(xlsUrl) '打開EXCEL Set sheetNew = excelObj.sheets.item(2) '設置將數據放在EXCEL中的那一頁上(索引從1開始) '相關的數據庫操做 Set conn = CreateObject("adodb.connection") set rs = createobject("adodb.recordset") conn.open "Provider=SQLOLEDB.1;Password=qaqc;Persist Security Info=True;User ID=test;Initial Catalog=test;Data Source=192.168.0.53" sql="select * from test" rs.open sql,conn Dim i while not rs.eof user_id = rs("user_id").value user_name = rs("user_name").value sheetNew.cells(i+1,1).value = user_id '列付值 sheetNew.cells(i+1,2).value = user_name '列付值 i = i + 1 rs.movenext wend excelObj.activeworkbook.saveas "C:\QTP\0110\sql-excel-qtp\data.xls" '保存數據 excelObj.quit '關閉對象 set excelObj = nothing '清空對象 rs.close '關閉數據庫鏈接操做 set rs=nothing conn.close set conn = nothing -----這就是導出到EXCEL的所有過程:),後面若是要用到這些數據的話,直接導入進來就能夠了