常用QTP函数合集
'**************************************************************************
*****************************************************
'有用的没用的都丢到一起,可能会有你想要的,原本是分为FileOper、DataOper、We
bOper(基于SAFFRON)、Win32Oper和ErrorOper五个文件
'后面三个超级啰嗦超级长的废物可能别人用不到,不过构造思路比较清晰,大家可以
DIY一下,或许你会觉得很方便,至少可以不用CheckPoint
'**************************************************************************
*****************************************************
classArray = Split("Browser,Page,Frame",",")
descArray = Split("micclass:=Browser,micclass:=Page,micclass:=Frame,",",")
objectArray = Split("Link,WebButton,WebList,WebEdit,Image", ",")
objectDescArray =
Split("micclass:=Link,micclass:=WebButton,micclass:=WebList,micclass:=WebEd
it,micclass:=Image", ",")
'**************************************************************************
*****************************************************
'设计说明:关闭所有打开的IE
'程序输入:
'程序输出:
'设计人员:LIUYI027
'设计时间:2011-01-08
'调用举例:CloseBrowsers
'**************************************************************************
*****************************************************
Public Sub CloseAllBrowser
Set Wshshell = CreateObject("Wscript.Shell")
Set DialogObject = Description.Create()
DialogObject("micclass").Value = "Dialog"
Set Windows32Dialog = Desktop.ChildObjects(DialogObject)
dlNum = Windows32Dialog.Count - 1
For v = 0 to dlNum
Windows32Dialog(v).Close
Next
Set Windows32Dialog = Nothing
Set theBrowser = Browser("micclass:=Browser", "index:=0")
While theBrowser.Exist(0)
theBrowser.Close
'有些系统页面可能在关闭的时候会有提示对话框出现
waitNx = 1
Do While waitNx < 5
Set Windows32Dialog = Desktop.ChildObjects(DialogObject)
dlNum = Windows32Dialog.Count - 1
For v = 0 to dlNum
dlText = Windows32Dialog(v).GetROProperty("regexpwndtitle")
Wshshell.AppActivate(dlText)
Wait(1)
Wshshell.SendKeys "{ENTER}"
Next
Set Windows32Dialog = Nothing
waitNx = waitNx + 1
Loop
Report
Pass,"使用CloseAllOpenedBrowsers函数页面关闭成功","当前页面关闭成功!"
Wend
Set theBrowser = Nothing
Set DialogObject = Nothing
Set Wshshell = Nothing
End Sub
'**************************************************************************
*****************************************************
'设计说明:最大化IE浏览器
'程序输入:无
'程序输出:无
'设计人员:LIUYI027
'设计时间:2011-01-08
'调用举例:MaximizeBrowser
'**************************************************************************
*****************************************************
Sub MaximizeBrowser
Set BrowserObject = Description.Create()
BrowserObject("NativeClass").Value = "IEFrame"
Set WindowsBrowser = Desktop.ChildObjects(BrowserObject)
brNum = WindowsBrowser.Count - 1
For i = 0 To brNum
ieVersion = WindowsBrowser(i).GetROProperty("version")
wndTitle = WindowsBrowser(i).GetROProperty("title")
Set bjectBrowser = Browser("micclass:=Browser", "index:="&i)
If Instr(ieVersion,6) > 0 Then
Window("regexpwndclass:=IEFrame","index:=0","text:="&wndTitle&".*").M
aximize
Else
WindowsBrowser(i).Maximize
End If
Set bjectBrowser = Nothing
Next
Set WindowsBrowser = Nothing
Set BrowserObject = Nothing
End Sub
'**************************************************************************
*****************************************************
'设计说明:根据对象属性列表和属性值列表匹配Browser对象,该函数基本无用… …
'程序输入:对象属性列表和属性值列表,列表使用英文半角的逗号分隔
'程序输出:创建对象
'设计人员:LIUYI027
'设计时间:2011-01-08
'调用举例:GenerateBrowserObject("name,title","保险业务管理系统,保险业务管
理系统")
'**************************************************************************
*****************************************************
Public Function GenerateBrowser(p_Attlist,p_Keylist)
AttArray = Split(p_Attlist,",")
KeyArray = Split(p_Keylist,",")
exeStr = "Browser("
If UBound(AttArray) <> UBound(KeyArray) Then
Report
Fail,"使用GenerateBrowserObject函数参数输入错误","对象属性的个数应该与
其对应的属性值个数相等!"
Exit Function
End If
For inx = 0 to UBound(AttArray)
exeStr =
exeStr&Chr(34)&AttArray(inx)&":=.*"&KeyArray(inx)&".*"&Chr(34)&","
Next
Execute "Set MyObject = "&exeStr&Quote("index:=0")&")"
If MyObject.Exist(0) Then
Report
Pass,"使用GenerateBrowserObject函数构造对象成功","按照Browser对象属性列
表【"&p_Attlist&"】,属性值列表【"&p_Keylist&"】,生成Browser对象成功!
"
Else
Report
Fail,"使用GenerateBrowserObject函数构造对象失败","按照Browser对象按照属
性列表【"&p_Attlist&"】,属性值列表【"&p_Keylist&"】,匹配Browser对象失
败!"
ExitRun
End If
Set GenerateBrowser = MyObject
End Function
'**************************************************************************
*****************************************************
'设计说明:初始化所有打开的Browser页面,不厌设计复杂只为稳定高效
'程序输入:无
'程序输出:初始化成功或者失败
'设计人员:LIUYI027
'设计时间:2011-01-08
'调用举例:BrowserSync
'**************************************************************************
*****************************************************
Public Function SyncronizeBrowser()
Set MyBrowser = Browser("micclass:=Browser", "index:=0")
If MyBrowser.Exist(0) Then
MyBrowser.Sync
Do Until MyBrowser.GetROProperty("url") <> "" And
MyBrowser.GetROProperty("name") <> ""
Delay 50
Loop
Else
Set MyBrowser = Nothing
SyncronizeBrowser = False
Report Warning ,"初始化页面失败","页面初始化失败,需要重新操作!"
Exit Function
End If
Set MyBrowser = Nothing
SyncronizeBrowser = True
End Function
'**************************************************************************
*****************************************************
'设计说明:打开指定的地址,并且初始化页面,特别说明:对于地址栏出现一次性se
ssionid的网页不可用该函数
'程序输入:url地址
'程序输出:初始化成功或者失败
'设计人员:LIUYI027
'设计时间:2011-01-08
'调用举例:print SyncronizeSepecifiedURL("www.baidu.com")
'**************************************************************************
*****************************************************
Public Function NavigateBrowser(para_myuri)
CloseAllBrowser
Set IEBrowser = CreateObject("InternetExplorer.Application")
IEBrowser.Visible = True
IEBrowser.Navigate para_myuri
Set bjectBrowser = Browser("micclass:=Browser", "index:=0")
Do Until SyncronizeBrowser() = True
Wait(1)
Loop
actualurl = ObjectBrowser.GetROProperty("url")
'下面这个判断主要是为了解决初始化地址跳转问题,如果URL发生变化会导致对象
属性发生变化从而导致运行错误。
If actualurl <> para_myuri Then
ObjectBrowser.Close
Set IEBrowser = Nothing
Set IEBrowser = CreateObject("InternetExplorer.Application")
IEBrowser.Visible = True
IEBrowser.Navigate actualurl
If Not SyncronizeBrowser() Then
Report
Warning,"使用NavigateBrowser函数IE初始化失败","打开指定页面【"&myuri&
"】在初始化的时候失败!"
Set MyBrowser = Nothing
Set IEBrowser = Nothing
Exit Function
End If
End If
Set bjectBrowser = Nothing
Set IEBrowser = Nothing
Report
Pass,"使用NavigateBrowser函数IE初始化成功","打开指定页面【"¶_myuri&"
】并且初始化成功!"
NavigateBrowser = True
End Function
'**************************************************************************
*****************************************************
'设计说明:处理弹出对话框,主要用于弹出的可预知性能够得到控制的地方,未知弹
出需引用其他函数处理
'程序输入:选择对话框操作:是/否/确认/取消/确定等等,是否需要向结果中添加提
示信息的报告
'程序输出:结果报告
'设计人员:LIUYI027
'设计时间:2011-01-08
'调用举例:HandleDialog "确认","Y"
'**************************************************************************
*****************************************************
Public Function HandleDialog(regexpName,needAlertInfo)
If Trim(regexpName) = "" Then
regexpName = "无需匹配的按钮!"
End If
Set Wshshell = CreateObject("Wscript.Shell")
Set DialogObject = Description.Create()
DialogObject("micclass").Value = "Dialog"
Set WindowsDialog = Desktop.ChildObjects(DialogObject)
dlNum = WindowsDialog.Count - 1
If dlNum < 0 Then
Set WindowsDialog = Nothing
Set DialogObject = Nothing
Report micDone,"没有任何弹出框","不需要进行对话框的处理!"
Exit Function
End If
For inx = 0 to dlNum
If needAlertInfo = "Y" Or needAlertInfo = True Then
Set StaticObject = Description.Create()
StaticObject("micclass").Value = "Static"
Set WindowsStatic = WindowsDialog(inx).ChildObjects(StaticObject)
stNum = WindowsStatic.Count
disMessage = WindowsStatic(stNum - 1).GetROProperty("text")
Report micDone,"获取网页对话框信息成功:",disMessage
Set WindowsStatic = Nothing
Set StaticObject = Nothing
End If
dialogTitle = WindowsDialog(inx).GetROProperty("text")
Set WinButtonObject = Description.Create()
WinButtonObject("micclass").Value = "WinButton"
Set WindowsButton = WindowsDialog(inx).ChildObjects(WinButtonObject)
wbNum = WindowsButton.Count - 1
For binx = 0 to wbNum
btName = WindowsButton(binx).GetROProperty("text")
If Instr(btName,regexpName) > 0 Then
WindowsButton(binx).Click
Report
Pass,"函数HandleDialog点击指定按钮成功","按照指定的按钮名称【"®e
xpName&"】查找并点击按钮成功!"
Exit For
End If
If binx = wbNum And Instr(btName,regexpName) = 0 Then
Wshshell.AppActivate(dialogTitle)
Wait(0)
Wshshell.SendKeys "{ENTER}"
Report
micWarning,"函数HandleDialog点击按钮","没有匹配到指定按钮,对已经弹
出的对话框直接使用默认操作!"
End If
Next
Set WindowsButton = Nothing
Set WinButtonObject = Nothing
Next
Set WindowsDialog = Nothing
Set DialogObject = Nothing
Set Wshshell = Nothing
End Function
'**************************************************************************
*****************************************************
'设计说明:SAFFRON框架引用以及部分改造,函数分流之后的部分
'程序输入:参见各个函数
'程序输出:
'设计人员:
'设计时间:2011-01-08
'调用举例:
'**************************************************************************
*****************************************************
Public Function GenerateDescription (classString,isModleWindow)
classNx = IndexOf(classArray, classString)
If classNx >= 0 Then
'增加对模态窗口的支持
If isModleWindow = "Y" Or isModleWindow = True Then
descString = "Window("&Quote("nativeclass:=Internet
Explorer_TridentDlgFrame")&")."
Else
descString = classArray(0)&"("&Quote(descArray(0))&")."
End If
If classNx >= 1 Then
descString = descString + classArray(1)&"("&Quote(descArray(1))&")."
If 2 >= classNx Then
If hasFrameValue <> "" Then
descString = descString +
classArray(2)&"("&Quote(descArray(2))&","&Quote("name:="&hasFrame
Value)&")."
End If
End If
End If
End If
GenerateDescription = descString
End Function
'**************************************************************************
**********************************************
Public Function GenerateObjectDescription (objClassName, otherAtt)
objNx = IndexOf(objectArray, objClassName)
objNameString = ""
If objNx <> -1 Then
objNameString =
objClassName&"("&Quote(objectDescArray(objNx))&","&Quote(otherAtt)&","&
Quote("index:=0")&")."
End If
GenerateobjectDescription = objNameString
End Function
'**************************************************************************
**********************************************
Public Function ObjectWorkUnderFrame(frameName)
hasFrameValue = frameName
End Function
'**************************************************************************
**********************************************
Public Function ObjectNotWorkUnderFrame()
hasFrameValue = ""
End Function
'**************************************************************************
**********************************************
Public Function VerifyObject (objectClassName, text,isModleWindow)
rval = false
localDesc = ""
estr = ""
If hasFrameValue <> "" Then
localDesc = GenerateDescription(classArray(2),isModleWindow)
Else
localDesc = GenerateDescription(classArray(1),isModleWindow)
End If
Select Case objectClassName
Case "Page"
Execute "rval =
"&GenerateDescription(classArray(1),isModleWindow)&"Exist (0)"
If rval Then
Execute "title =
"&GenerateDescription(classArray(1),isModleWindow)&"GetROProperty("
&Quote("title")&")"
If title = text Then
rval = true
Else
rval = false
End If
End If
Case "CurrentFrame"
If hasFrameValue <> "" Then
estr = "rval = "&localDesc
End If
Case "Link"
estr = "rval = "&localDesc&GenerateObjectDescription("Link",
"innertext:=.*"&text&".*")
Case "WebButton"
estr = "rval = "&localDesc&GenerateObjectDescription("WebButton",
"value:=.*"&text&".*")
Case "WebList"
estr = "rval = "&localDesc&GenerateObjectDescription("WebList",
"name:=.*"&text&".*")
Case "WebEdit"
estr = "rval = "&localDesc&GenerateObjectDescription("WebEdit",
"name:=.*"&text&".*")
End Select
If estr <> "" Then
Execute estr + "Exist (0)"
End If
If rval Then
Report micDone, objectClassName&"匹配对象成功",
"对象【"&objectClassName&"】【 "&Quote(text)&" 】查询成功!"
VerifyObject = True
Else
Report Warning, objectClassName&"匹配对象失败",
TAG: