分享几个傻瓜函数,用于交互判断
本帖最后由 lyscser 于 2010-12-12 21:17 编辑用法看注释,原理就是全部遍历,按关键字去搜寻,可能就是执行效率低一些……主要看被测系统页面框架设计的复杂度了
没怎么仔细测试,如果有问题自己改过,顺便回帖告诉我就行了,不用批斗……
'**************************************************************************************************************************************
'设计说明:
'判断一般性的js层抛出的控制提示,根据用户选择如何做后续处理,请注意,该程序只能用于预期之外的提示处理,预期之内必须自行判断
'如果不关心页面提示信息是什么,只想把提示信息抓出来,那么匹配关键字输入空值即可
'程序输入:
'respath------截图文件保存路径
'judgeKeyWord------用于进行匹配的关键字信息,可用英文半角的逗号分隔,只有所有关键都在页面找到才视为运行通过
'isExitRun-------对于匹配失败的情况,选择是否彻底退出运行
'程序输出:截图文件
'设计人员:LIUYI027
'设计时间:2011-12-05
'调用举例:Call JudgeErrorForDialog("D:\","请,登录","Y")
'**************************************************************************************************************************************
Public Sub JudgeErrorForDialog(respath,judgeKeyWord,isExitRun)
'判断用户传入参数
IfTrim(judgeKeyWord) = "" Then
theKeyArray = "用户选择不做关键字匹配"&Replace(Date,"/","-")&"_"&Replace(Time,":","-")
emptyPara = True
End If
theKeyArray = Split(judgeKeyWord,",")
IfTrim(Replace(isExitRun,"y","Y")) = "Y"Or isExitRun = True Then
isExitRun = True
ElseIf Trim(Replace(isExitRun,"n","N")) = "N" Or isExitRun = False Then
isExitRun = False
Else
isExitRun = True
Reporter.ReportEvent micWarning,"请尽量使用【Y/N】来作为您参数","由于本次输入无效,程序将自动选择在无法完全匹配的时候自动退出运行,请了解!"
End If
Set Wshshell = CreateObject("Wscript.Shell")
'初始化所有打开的IE,以便确认所有的弹出窗口都已经展现在页面上
Set BrowserObject = Description.Create()
BrowserObject("micclass").Value = "Browser"
Set WindowsBrowser = Desktop.ChildObjects(BrowserObject)
brNum = WindowsBrowser.Count
IfbrNum < 1 Then
Set WindowsBrowser = Nothing
Set BrowserObject = Nothing
Set Wshshell = Nothing
Exit Sub
Else
For bindex = 0 to brNum - 1
WindowsBrowser(bindex).Sync
Next
End If
Set WindowsBrowser = Nothing
Set BrowserObject = Nothing
Set DialogObject = Description.Create()
DialogObject("micclass").Value = "Dialog"
Set WindowsDialog = Desktop.ChildObjects(DialogObject)
dlNum = WindowsDialog.Count
IfdlNum < 1 Then
Set WindowsDialog = Nothing
Set DialogObject = Nothing
Set WindowsBrowser = Nothing
Set BrowserObject = Nothing
Set Wshshell = Nothing
IfemptyPara = True Then
Reporter.ReportEvent micPass,"没有需要判断的对象","提交之后系统没有任何弹出的页面信息提示!"
Else
Reporter.ReportEvent micWarning,"没有需要判断的对象","提交之后系统没有任何弹出的页面信息提示!"
End If
Exit Sub
End If
For dindex = 0 to dlNum - 1
dlTitle = WindowsDialog(dindex).GetROProperty("text")
nameByTime = Replace(Date,"/","-")&"_"&Replace(Time,":","-")&".png"
fileName = respath&Environment.Value("TestName")&"_"&Environment.Value("ActionName")&"_"&nameByTime
'对于弹出的下载窗口,需要用单独的程序处理,错误判断中不做处理,直接关闭
IfINStr(dlTitle,"下载") > 0 Or INStr(dlTitle,"安装") > 0 Or INStr(dlTitle,"另存为") > 0 Or INStr(dlTitle,"保存为") > 0Then
WindowsDialog(dindex).Close
End If
'Windows GUI直接处理掉,不在判断范围之内,如果需要使用则请自行修改(注释掉)这一段
Set Win32Object = Description.Create()
Win32Object("micclass").Value = "WinObject"
Set WindowsObject = WindowsDialog(dindex).ChildObjects(Win32Object)
woNum = WindowsObject.Count
IfwoNum > 0 Then
For windex = 0 to woNum - 1
winMessage = WindowsObject.GetROProperty("text")
IfNot Trim(winMessage) = "" Then
Reporter.ReportEvent micDone,"程序不做匹配判断的提示信息:",winMessage
End If
Next
Wshshell.AppActivate(dlTitle)
WindowsDialog(dindex).CaptureBitmap fileName
Wshshell.AppActivate(dlTitle)
Wshshell.SendKeys "{ENTER}"
End If
Set WindowsObject = Nothing
Set Win32Object = Nothing
Next
'重新Count页面上的非下载窗口个数
Set WindowsDialog = Nothing
Set WindowsDialog = Desktop.ChildObjects(DialogObject)
diaNum = WindowsDialog.Count
maxCount = 0
For dlindex = 0 to diaNum - 1
dlTitle = WindowsDialog(dlindex).GetROProperty("text")
nameByTime = Replace(Date,"/","-")&"_"&Replace(Time,":","-")&".png"
fileName = respath&Environment.Value("TestName")&"_"&Environment.Value("ActionName")&"_"&nameByTime
'对于弹出的信息提示窗口,需要获取其提示信息,以供后续选择处理方式
Set StaticObject = Description.Create()
StaticObject("micclass").Value = "Static"
Set WindowsStatic = WindowsDialog(dlindex).ChildObjects(StaticObject)
stNum = WindowsStatic.Count
For sindex = 0 to stNum - 1
disMessage = WindowsStatic(sindex).GetROProperty("text")
arrindex = 0
For arrindex = 0 To UBound(theKeyArray)
IfINStr(disMessage,theKeyArray(arrindex)) > 0 Then
maxCount = maxCount + 1
Reporter.ReportEvent micDone,"关键字匹配成功","关键字【 "&theKeyArray(arrindex)&" 】匹配成功!"
End If
arrindex = arrindex + 1
Next
Next
Wshshell.AppActivate(dlTitle)
WindowsDialog(dlindex).CaptureBitmap fileName
Wshshell.AppActivate(dlTitle)
Wshshell.SendKeys "{ENTER}"
Next
IfmaxCount < UBound(theKeyArray) + 1 Then
Reporter.ReportEvent micFail,"函数【JudgeErrorForDialog】关键字匹配失败","您需要匹配【 "&(UBound(theKeyArray) + 1)&" 】个关键字,页面上出现了【 "&maxCount&" 】个!"
IfisExitRun = True Then
Set WindowsStatic = Nothing
Set StaticObject = Nothing
Set WindowsDialog = Nothing
Set DialogObject = Nothing
Set Wshshell = Nothing
ExitRun
End If
Else
Reporter.ReportEvent micPass,"函数【JudgeErrorForDialog】关键字匹配成功","您需要匹配【 "&(UBound(theKeyArray) + 1)&" 】个关键字,页面上出现了【 "&maxCount&" 】个!"
End If
Set WindowsStatic = Nothing
Set StaticObject = Nothing
Set WindowsDialog = Nothing
Set DialogObject = Nothing
Set Wshshell = Nothing
End Sub '**************************************************************************************************************************************
'设计说明:
'页面抛出未封装的RuntimeExcptions,一般是应用出错或者环境异常所致,对于这种情况程序直接截图之后退出运行,不可选择
'不同系统使用开发的习惯有所不同,例如有使用WebTable存放错误信息,有使用页面短文本结合Link详细文本的方式,请自主改造
'程序输入:
'respath------截图文件保存路径
'myKeyWords------用于进行匹配的关键字信息,可用英文半角的逗号分隔,只要有任意关键字在页面找到都视为发现异常,运行退出
'程序输出:截图文件
'设计人员:LIUYI027
'设计时间:2011-12-05
'调用举例:Call JudgePageExceptions("D:\","Excetion,EXCEPTION,exception,ORA-,详细情况")
'**************************************************************************************************************************************
Public Sub JudgePageExceptions(respath,myKeyWords)
Set Wshshell = CreateObject("Wscript.Shell")
'因为一旦出错立刻停止运行,不会出现多次截图导致的文件名冲突,故文件名只赋一次值
nameByTime = Replace(Date,"/","-")&"_"&Replace(Time,":","-")&".png"
fileName = respath&Environment.Value("TestName")&"_"&Environment.Value("ActionName")&"_"&nameByTime
'如果输入为空,则组合一个不大可能出现的错误信息出来,想必不会哪个系统出这种Exception的:)
If Trim(theKeyArray) = "" Then
myKeyWords = myKeyWords&"用户选择不做关键字匹配"&Replace(Date,"/","-")&"_"&Replace(Time,":","-")
End If
theKeyArray = Split(myKeyWord,",")
Set BrowserObj = Description.Create()
BrowserObj("micclass").Value = "Browser"
Set Win32Browser = Desktop.ChildObjects(BrowserObj)
brNum = Win32Browser.Count
IfbrNum < 1 Then
Set WindowsBrowser = Nothing
Set BrowserObject = Nothing
Set Wshshell = Nothing
Exit Sub
End If
For bindex = 0 to brNum - 1
Win32Browser(bindex).Sync
Set PageObj = Description.Create()
PageObj("micclass").value = "Page"
Set Win32Page = Win32Browser(bindex).ChildObjects(PageObj)
pgNum = Win32Page.Count
For pindex = 0 to pgNum - 1
Set FrameObj = Description.Create()
FrameObj("micclass").Value = "Frame"
Set Win32Frame = Win32Page(pindex).ChildObjects(FrameObj)
frNum = Win32Frame.Count
'对于页面上的出错信息,如果存在使用LINK链接的错误文本信息则点开并且截图,链接名称为需要匹配的关键字之一
For findex = 0 to frNum- 1
Set LinkObj = Description.Create()
LinkObj("micclass").Value = "Link"
Set Win32Link = Win32Frame(findex).ChildObjects(LinkObj)
liNum = Win32Link.Count
For lindex = 0 to liNum - 1
linkText = Win32Link(lindex).GetROProperty("text")
For aindex = 0 To UBound(theKeyArray)
theKeyWord = theKeyArray(aindex)
brTit = Win32Browser(bindex).GetROProperty("title")
IfInstr(lindex,theKeyWord) > 0 Then
Win32Link(lindex).Click
Win32Browser(bindex).Sync
Wshshell.AppActivate(brTit)
Win32Browser(bindex).CaptureBitmap fileName
Reporter.ReportEvent micFail,"应用系统出错","JAVA运行时错误!"
Set Win32Link = Nothing
Set LinkObj = Nothing
Set Win32Frame = Nothing
Set FrameObj = Nothing
Set Win32Page = Nothing
Set PageObj = Nothing
Set Win32Browser = Nothing
Set BrowserObj = Nothing
Set Wshshell = Nothing
ExitRun
End If
Next
Next
Set TableObj = Description.Create()
TableObj("micclass").Value = "WebTable"
Set Win32Table = Win32Frame(findex).ChildObjects(TableObj)
tbNum = Win32Table.Count
For tindex = 0 to tbNum - 1
'如果错误信息包装在复杂的Table里面,则可以考虑去遍历每一个单元格的值,但是遍历之前可以根据这种复杂的特点更快的定位Table的位置以提高运行效率
tabText = Win32Table(tindex).GetCellData(1,1)
For aindex = 0 To UBound(theKeyArray)
theKeyWord = theKeyArray(aindex)
If Instr(tabText,theKeyWord) > 0 Then
Reporter.ReportEvent micFail,"应用系统运行时出错",tabText
Wshshell.AppActivate(brTit)
Win32Browser(bindex).CaptureBitmap fileName
Set Win32Table = Nothing
Set TableObj = Nothing
Set Win32Frame = Nothing
Set FrameObj = Nothing
Set Win32Page = Nothing
Set PageObj = Nothing
Set Win32Browser = Nothing
Set BrowserObj = Nothing
Set Wshshell = Nothing
ExitRun
End If
Next
Next
Set Win32Table = Nothing
Set TableObj = Nothing
Next
Set Win32Frame = Nothing
Set FrameObj = Nothing
Next
Set Win32Page = Nothing
Set PageObj = Nothing
Next
Set Win32Browser = Nothing
Set BrowserObj = Nothing
Set Wshshell = Nothing
Reporter.ReportEvent misPass,"提交之后没有出现任何异常","函数【JudgePageExceptions】已经遍历页面每一个角落,没有发现任何异常信息!"
End Sub
'**************************************************************************************************************************************
'设计说明:提交之后页面弹出二级子页面或者主页面上的提示信息,系统后台的响应结果,一般用于程序内部逻辑控制
'程序输入:
'respath------截图文件保存路径
'errSpecify------错误特征关键字,使用英文半角的逗号分隔,只要有任意关键字出现都视为发现异常,所以使用关键字时请尽量精准
'orgBrowserNum--------在调用此函数的页面操作未发生之前,已经打开的操作页面IE个数
'isExitRun--------是否退出运行
'程序输出:截图文件
'设计人员:LIUYI027
'设计时间:2011-12-05
'调用举例:Call JudgeBrowserErrInfo("D:\","请更正如下错误",1,"Y")
'**************************************************************************************************************************************
Public Sub JudgeBrowserErrInfo(respath,errSpecify,orgBrowserNum,isExitRun)
Set Wshshell = CreateObject("Wscript.Shell")
'判断用户参数输入
IfTrim(errSpecify) = "" Then
errSpecify = "用户选择不做关键字匹配"&Replace(Date,"/","-")&"_"&Replace(Time,":","-")
End If
IfTrim(Replace(isExitRun,"y","Y")) = "Y"Or isExitRun = True Then
isExitRun = True
ElseIf Trim(Replace(isExitRun,"n","N")) = "N" Or isExitRun = False Then
isExitRun = False
Else
isExitRun = True
Reporter.ReportEvent micWarning,"请尽量使用【Y/N】来作为您参数","由于本次输入无效,程序将自动替您选择遇到错误退出运行,请了解!"
End If
theKeyArray = Split(errSpecify,",")
Set BrowserObj = Description.Create()
BrowserObj("micclass").Value = "Browser"
Set Win32Browser = Desktop.ChildObjects(BrowserObj)
brNum = Win32Browser.Count
pairCount = 0
IfbrNum < 1 Then
Set WindowsBrowser = Nothing
Set BrowserObject = Nothing
Set Wshshell = Nothing
Exit Sub
End If
For bindex = 0 to brNum - 1
Win32Browser(bindex).Sync
Set PageObj = Description.Create()
PageObj("micclass").value = "Page"
Set Win32Page = Win32Browser(bindex).ChildObjects(PageObj)
pgNum = Win32Page.Count
brTit = Win32Browser(k).GetROProperty("title")
For pindex = 0 to pgNum - 1
Set TabObj = Description.Create()
TabObj("micclass").value = "WebTable"
Set Win32Tab = Win32Page(pindex).ChildObjects(TabObj)
tbNum = Win32Tab.Count
'对每次出现的错误提示都提交报告,并记录匹配成功的次数
For tindex = 0 to tbNum - 1
tabText = Win32Tab(tindex).GetROProperty("text")
For aindex = 0 To UBound(theKeyArray)
theKeyWord = theKeyArray(aindex)
If Instr(tabText,theKeyWord) > 0 Then
pairCount = pairCount + 1
Reporter.ReportEvent micFail,"页面提交操作时系统提示:",tabText
End If
aindex = aindex + 1
Next
Next
IfpairCount > 0 Then
Wshshell.AppActivate(brTit)
Win32Browser(bindex).CaptureBitmap fileName
'如果用户选择发现错误之后退出运行则退出运行
IfisExitRun = "Y" Then
Set Win32Tab = Nothing
Set TabObj = Nothing
Set Win32Page = Nothing
Set PageObj = Nothing
Set Win32Browser = Nothing
Set BrowserObj = Nothing
Set Wshshell = Nothing
ExitRun
End If
Else
Reporter.ReportEvent micPass,"页面提交之后没有发现错误","函数【JudgeBrowserErrInfo】已经遍历页面每一个角落,没有发现任何异常信息!"
End If
Set Win32Tab = Nothing
Set TabObj = Nothing
Next
Set Win32Page = Nothing
Set PageObj = Nothing
'关闭多余的IE页面
If brNum > orgBrowserNum Then
For brindex = orgBrowserNum to brNum - 1
Win32Browser(brindex).Close
Next
End If
Next
Set Win32Browser = Nothing
Set BrowserObj = Nothing
Set Wshshell = Nothing
End Sub
顺带说一句,只能在QTP里面执行,别把它们弄成vbs在那跑,跑不成滴 :victory:非常感谢
页:
[1]