51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1578|回复: 4
打印 上一主题 下一主题

[原创] 分享几个傻瓜函数,用于交互判断

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2010-12-12 21:14:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 lyscser 于 2010-12-12 21:17 编辑

用法看注释,原理就是全部遍历,按关键字去搜寻,可能就是执行效率低一些……主要看被测系统页面框架设计的复杂度了
没怎么仔细测试,如果有问题自己改过,顺便回帖告诉我就行了,不用批斗……

  1. '**************************************************************************************************************************************
  2. '设计说明:
  3. '  判断一般性的js层抛出的控制提示,根据用户选择如何做后续处理,请注意,该程序只能用于预期之外的提示处理,预期之内必须自行判断
  4. '  如果不关心页面提示信息是什么,只想把提示信息抓出来,那么匹配关键字输入空值即可
  5. '程序输入:
  6. '  respath------截图文件保存路径
  7. '  judgeKeyWord------用于进行匹配的关键字信息,可用英文半角的逗号分隔,只有所有关键都在页面找到才视为运行通过
  8. '  isExitRun-------对于匹配失败的情况,选择是否彻底退出运行
  9. '程序输出:截图文件
  10. '设计人员:LIUYI027
  11. '设计时间:2011-12-05
  12. '调用举例:Call JudgeErrorForDialog("D:\","请,登录","Y")
  13. '**************************************************************************************************************************************
  14. Public Sub JudgeErrorForDialog(respath,judgeKeyWord,isExitRun)
  15. '判断用户传入参数
  16. If  Trim(judgeKeyWord) = "" Then
  17.   theKeyArray = "用户选择不做关键字匹配"&Replace(Date,"/","-")&"_"&Replace(Time,":","-")
  18.   emptyPara = True
  19. End If
  20. theKeyArray = Split(judgeKeyWord,",")
  21. If  Trim(Replace(isExitRun,"y","Y")) = "Y"  Or isExitRun = True Then
  22.   isExitRun = True
  23. ElseIf Trim(Replace(isExitRun,"n","N")) = "N" Or isExitRun = False Then
  24.   isExitRun = False
  25. Else
  26.   isExitRun = True
  27.   Reporter.ReportEvent micWarning,"请尽量使用【Y/N】来作为您参数","由于本次输入无效,程序将自动选择在无法完全匹配的时候自动退出运行,请了解!"
  28. End If
  29. Set Wshshell = CreateObject("Wscript.Shell")
  30. '初始化所有打开的IE,以便确认所有的弹出窗口都已经展现在页面上
  31. Set BrowserObject = Description.Create()
  32. BrowserObject("micclass").Value = "Browser"
  33. Set WindowsBrowser = Desktop.ChildObjects(BrowserObject)
  34. brNum = WindowsBrowser.Count
  35. If  brNum < 1 Then
  36.   Set WindowsBrowser = Nothing
  37.   Set BrowserObject = Nothing
  38.   Set Wshshell = Nothing
  39.   Exit Sub
  40. Else
  41.   For bindex = 0 to brNum - 1
  42.    WindowsBrowser(bindex).Sync
  43.   Next
  44. End If
  45. Set WindowsBrowser = Nothing
  46. Set BrowserObject = Nothing
  47. Set DialogObject = Description.Create()
  48. DialogObject("micclass").Value = "Dialog"
  49. Set WindowsDialog = Desktop.ChildObjects(DialogObject)
  50. dlNum = WindowsDialog.Count
  51. If  dlNum < 1 Then
  52.   Set WindowsDialog = Nothing
  53.   Set DialogObject = Nothing
  54.   Set WindowsBrowser = Nothing
  55.   Set BrowserObject = Nothing
  56.   Set Wshshell = Nothing
  57.   If  emptyPara = True Then
  58.    Reporter.ReportEvent micPass,"没有需要判断的对象","提交之后系统没有任何弹出的页面信息提示!"
  59.   Else
  60.    Reporter.ReportEvent micWarning,"没有需要判断的对象","提交之后系统没有任何弹出的页面信息提示!"
  61.   End If
  62.   Exit Sub
  63. End If
  64. For dindex = 0 to dlNum - 1
  65.   dlTitle = WindowsDialog(dindex).GetROProperty("text")
  66.   nameByTime = Replace(Date,"/","-")&"_"&Replace(Time,":","-")&".png"
  67.   fileName = respath&Environment.Value("TestName")&"_"&Environment.Value("ActionName")&"_"&nameByTime
  68.   '对于弹出的下载窗口,需要用单独的程序处理,错误判断中不做处理,直接关闭
  69.   If  INStr(dlTitle,"下载") > 0 Or INStr(dlTitle,"安装") > 0 Or INStr(dlTitle,"另存为") > 0 Or INStr(dlTitle,"保存为") > 0  Then
  70.    WindowsDialog(dindex).Close
  71.   End If
  72.   'Windows GUI直接处理掉,不在判断范围之内,如果需要使用则请自行修改(注释掉)这一段
  73.   Set Win32Object = Description.Create()
  74.   Win32Object("micclass").Value = "WinObject"
  75.   Set WindowsObject = WindowsDialog(dindex).ChildObjects(Win32Object)
  76.   woNum = WindowsObject.Count
  77.   If  woNum > 0 Then
  78.    For windex = 0 to woNum - 1
  79.     winMessage = WindowsObject.GetROProperty("text")
  80.    
  81.     If  Not Trim(winMessage) = "" Then
  82.      Reporter.ReportEvent micDone,"程序不做匹配判断的提示信息:",winMessage
  83.     End If
  84.    Next
  85.    Wshshell.AppActivate(dlTitle)
  86.    WindowsDialog(dindex).CaptureBitmap fileName
  87.    Wshshell.AppActivate(dlTitle)
  88.    Wshshell.SendKeys "{ENTER}"
  89.   End If
  90.   Set WindowsObject = Nothing
  91.   Set Win32Object = Nothing
  92. Next
  93. '重新Count页面上的非下载窗口个数
  94. Set WindowsDialog = Nothing
  95. Set WindowsDialog = Desktop.ChildObjects(DialogObject)
  96. diaNum = WindowsDialog.Count
  97. maxCount = 0
  98. For dlindex = 0 to diaNum - 1
  99.   dlTitle = WindowsDialog(dlindex).GetROProperty("text")
  100.   nameByTime = Replace(Date,"/","-")&"_"&Replace(Time,":","-")&".png"
  101.   fileName = respath&Environment.Value("TestName")&"_"&Environment.Value("ActionName")&"_"&nameByTime
  102.   '对于弹出的信息提示窗口,需要获取其提示信息,以供后续选择处理方式
  103.   Set StaticObject = Description.Create()
  104.   StaticObject("micclass").Value = "Static"
  105.   Set WindowsStatic = WindowsDialog(dlindex).ChildObjects(StaticObject)
  106.   stNum = WindowsStatic.Count
  107.   For sindex = 0 to stNum - 1
  108.    disMessage = WindowsStatic(sindex).GetROProperty("text")  
  109.    arrindex = 0

  110.    For arrindex = 0 To UBound(theKeyArray)
  111.    
  112.     If  INStr(disMessage,theKeyArray(arrindex)) > 0 Then
  113.      maxCount = maxCount + 1
  114.      Reporter.ReportEvent micDone,"关键字匹配成功","关键字【 "&theKeyArray(arrindex)&" 】匹配成功!"
  115.     End If
  116.     arrindex = arrindex + 1
  117.    Next
  118.   Next
  119.   Wshshell.AppActivate(dlTitle)
  120.   WindowsDialog(dlindex).CaptureBitmap fileName
  121.   Wshshell.AppActivate(dlTitle)
  122.   Wshshell.SendKeys "{ENTER}"
  123. Next
  124. If  maxCount < UBound(theKeyArray) + 1 Then
  125.   Reporter.ReportEvent micFail,"函数【JudgeErrorForDialog】关键字匹配失败","您需要匹配【 "&(UBound(theKeyArray) + 1)&" 】个关键字,页面上出现了【 "&maxCount&" 】个!"
  126.   
  127.   If  isExitRun = True Then
  128.    Set WindowsStatic = Nothing
  129.    Set StaticObject = Nothing
  130.    Set WindowsDialog = Nothing
  131.    Set DialogObject = Nothing
  132.    Set Wshshell = Nothing
  133.    ExitRun
  134.   End If
  135. Else
  136.   Reporter.ReportEvent micPass,"函数【JudgeErrorForDialog】关键字匹配成功","您需要匹配【 "&(UBound(theKeyArray) + 1)&" 】个关键字,页面上出现了【 "&maxCount&" 】个!"
  137. End If
  138. Set WindowsStatic = Nothing
  139. Set StaticObject = Nothing
  140. Set WindowsDialog = Nothing
  141. Set DialogObject = Nothing
  142. Set Wshshell = Nothing
  143. End Sub
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2010-12-12 21:15:47 | 只看该作者
  1. '**************************************************************************************************************************************
  2. '设计说明:
  3. '  页面抛出未封装的RuntimeExcptions,一般是应用出错或者环境异常所致,对于这种情况程序直接截图之后退出运行,不可选择
  4. '  不同系统使用开发的习惯有所不同,例如有使用WebTable存放错误信息,有使用页面短文本结合Link详细文本的方式,请自主改造
  5. '程序输入:
  6. '  respath------截图文件保存路径
  7. '  myKeyWords------用于进行匹配的关键字信息,可用英文半角的逗号分隔,只要有任意关键字在页面找到都视为发现异常,运行退出
  8. '程序输出:截图文件
  9. '设计人员:LIUYI027
  10. '设计时间:2011-12-05
  11. '调用举例:Call JudgePageExceptions("D:\","Excetion,EXCEPTION,exception,ORA-,详细情况")
  12. '**************************************************************************************************************************************
  13. Public Sub JudgePageExceptions(respath,myKeyWords)
  14. Set Wshshell = CreateObject("Wscript.Shell")

  15. '因为一旦出错立刻停止运行,不会出现多次截图导致的文件名冲突,故文件名只赋一次值
  16. nameByTime = Replace(Date,"/","-")&"_"&Replace(Time,":","-")&".png"
  17. fileName = respath&Environment.Value("TestName")&"_"&Environment.Value("ActionName")&"_"&nameByTime

  18. '如果输入为空,则组合一个不大可能出现的错误信息出来,想必不会哪个系统出这种Exception的:)
  19. If Trim(theKeyArray) = "" Then
  20.   myKeyWords = myKeyWords&"用户选择不做关键字匹配"&Replace(Date,"/","-")&"_"&Replace(Time,":","-")
  21. End If  
  22. theKeyArray = Split(myKeyWord,",")
  23.         
  24. Set BrowserObj = Description.Create()
  25. BrowserObj("micclass").Value = "Browser"
  26. Set Win32Browser = Desktop.ChildObjects(BrowserObj)
  27. brNum = Win32Browser.Count
  28. If  brNum < 1 Then
  29.   Set WindowsBrowser = Nothing
  30.   Set BrowserObject = Nothing
  31.   Set Wshshell = Nothing
  32.   Exit Sub
  33. End If

  34. For bindex = 0 to brNum - 1
  35.   Win32Browser(bindex).Sync  
  36.   Set PageObj = Description.Create()
  37.   PageObj("micclass").value = "Page"
  38.   Set Win32Page = Win32Browser(bindex).ChildObjects(PageObj)
  39.   pgNum = Win32Page.Count

  40.   For pindex = 0 to pgNum - 1
  41.    Set FrameObj = Description.Create()
  42.    FrameObj("micclass").Value = "Frame"
  43.    Set Win32Frame = Win32Page(pindex).ChildObjects(FrameObj)
  44.    frNum = Win32Frame.Count
  45.    '对于页面上的出错信息,如果存在使用LINK链接的错误文本信息则点开并且截图,链接名称为需要匹配的关键字之一
  46.    For findex = 0 to frNum  - 1   
  47.     Set LinkObj = Description.Create()
  48.     LinkObj("micclass").Value = "Link"
  49.     Set Win32Link = Win32Frame(findex).ChildObjects(LinkObj)
  50.     liNum = Win32Link.Count
  51.     For lindex = 0 to liNum - 1
  52.      linkText = Win32Link(lindex).GetROProperty("text")
  53.      For aindex = 0 To UBound(theKeyArray)
  54.       theKeyWord = theKeyArray(aindex)
  55.       brTit = Win32Browser(bindex).GetROProperty("title")
  56.       If  Instr(lindex,theKeyWord) > 0 Then
  57.        Win32Link(lindex).Click
  58.        Win32Browser(bindex).Sync
  59.        Wshshell.AppActivate(brTit)
  60.        Win32Browser(bindex).CaptureBitmap fileName
  61.        Reporter.ReportEvent micFail,"应用系统出错","JAVA运行时错误!"
  62.        Set Win32Link = Nothing
  63.        Set LinkObj = Nothing
  64.        Set Win32Frame = Nothing
  65.        Set FrameObj = Nothing
  66.        Set Win32Page = Nothing
  67.        Set PageObj = Nothing
  68.        Set Win32Browser = Nothing
  69.        Set BrowserObj = Nothing
  70.        Set Wshshell = Nothing
  71.        ExitRun
  72.       End If
  73.      Next
  74.     Next
  75.    
  76.     Set TableObj = Description.Create()
  77.     TableObj("micclass").Value = "WebTable"
  78.     Set Win32Table = Win32Frame(findex).ChildObjects(TableObj)
  79.     tbNum = Win32Table.Count
  80.    
  81.     For tindex = 0 to tbNum - 1
  82.      '如果错误信息包装在复杂的Table里面,则可以考虑去遍历每一个单元格的值,但是遍历之前可以根据这种复杂的特点更快的定位Table的位置以提高运行效率     
  83.      tabText = Win32Table(tindex).GetCellData(1,1)
  84.      
  85.      For aindex = 0 To UBound(theKeyArray)
  86.       theKeyWord = theKeyArray(aindex)
  87.       
  88.       If Instr(tabText,theKeyWord) > 0 Then
  89.        Reporter.ReportEvent micFail,"应用系统运行时出错",tabText
  90.        Wshshell.AppActivate(brTit)
  91.        Win32Browser(bindex).CaptureBitmap fileName
  92.        Set Win32Table = Nothing
  93.        Set TableObj = Nothing
  94.        Set Win32Frame = Nothing
  95.        Set FrameObj = Nothing
  96.        Set Win32Page = Nothing
  97.        Set PageObj = Nothing
  98.        Set Win32Browser = Nothing
  99.        Set BrowserObj = Nothing
  100.        Set Wshshell = Nothing
  101.        ExitRun
  102.       End If
  103.      Next
  104.     Next   
  105.     Set Win32Table = Nothing
  106.     Set TableObj = Nothing
  107.    Next
  108.    Set Win32Frame = Nothing
  109.    Set FrameObj = Nothing
  110.   Next
  111.   Set Win32Page = Nothing
  112.   Set PageObj = Nothing
  113. Next
  114. Set Win32Browser = Nothing
  115. Set BrowserObj = Nothing
  116. Set Wshshell = Nothing
  117. Reporter.ReportEvent misPass,"提交之后没有出现任何异常","函数【JudgePageExceptions】已经遍历页面每一个角落,没有发现任何异常信息!"
  118. End Sub

复制代码
回复 支持 反对

使用道具 举报

该用户从未签到

3#
 楼主| 发表于 2010-12-12 21:16:36 | 只看该作者

  1. '**************************************************************************************************************************************
  2. '设计说明:提交之后页面弹出二级子页面或者主页面上的提示信息,系统后台的响应结果,一般用于程序内部逻辑控制
  3. '程序输入:
  4. '  respath------截图文件保存路径
  5. '  errSpecify------错误特征关键字,使用英文半角的逗号分隔,只要有任意关键字出现都视为发现异常,所以使用关键字时请尽量精准
  6. '  orgBrowserNum--------在调用此函数的页面操作未发生之前,已经打开的操作页面IE个数
  7. '  isExitRun--------是否退出运行
  8. '程序输出:截图文件
  9. '设计人员:LIUYI027
  10. '设计时间:2011-12-05
  11. '调用举例:Call JudgeBrowserErrInfo("D:\","请更正如下错误",1,"Y")
  12. '**************************************************************************************************************************************
  13. Public Sub JudgeBrowserErrInfo(respath,errSpecify,orgBrowserNum,isExitRun)
  14. Set Wshshell = CreateObject("Wscript.Shell")
  15. '判断用户参数输入
  16. If  Trim(errSpecify) = "" Then
  17.   errSpecify = "用户选择不做关键字匹配"&Replace(Date,"/","-")&"_"&Replace(Time,":","-")
  18. End If
  19. If  Trim(Replace(isExitRun,"y","Y")) = "Y"  Or isExitRun = True Then
  20.   isExitRun = True
  21. ElseIf Trim(Replace(isExitRun,"n","N")) = "N" Or isExitRun = False Then
  22.   isExitRun = False
  23. Else
  24.   isExitRun = True
  25.   Reporter.ReportEvent micWarning,"请尽量使用【Y/N】来作为您参数","由于本次输入无效,程序将自动替您选择遇到错误退出运行,请了解!"
  26. End If

  27. theKeyArray = Split(errSpecify,",")
  28.         
  29. Set BrowserObj = Description.Create()
  30. BrowserObj("micclass").Value = "Browser"
  31. Set Win32Browser = Desktop.ChildObjects(BrowserObj)
  32. brNum = Win32Browser.Count
  33. pairCount = 0
  34. If  brNum < 1 Then
  35.   Set WindowsBrowser = Nothing
  36.   Set BrowserObject = Nothing
  37.   Set Wshshell = Nothing
  38.   Exit Sub
  39. End If
  40. For bindex = 0 to brNum - 1
  41.   Win32Browser(bindex).Sync
  42.   Set PageObj = Description.Create()
  43.   PageObj("micclass").value = "Page"
  44.   Set Win32Page = Win32Browser(bindex).ChildObjects(PageObj)
  45.   pgNum = Win32Page.Count
  46.   brTit = Win32Browser(k).GetROProperty("title")
  47.   For pindex = 0 to pgNum - 1
  48.    Set TabObj = Description.Create()
  49.    TabObj("micclass").value = "WebTable"
  50.    Set Win32Tab = Win32Page(pindex).ChildObjects(TabObj)
  51.    tbNum = Win32Tab.Count
  52.    '对每次出现的错误提示都提交报告,并记录匹配成功的次数
  53.    For tindex = 0 to tbNum - 1   
  54.     tabText = Win32Tab(tindex).GetROProperty("text")
  55.    
  56.     For aindex = 0 To UBound(theKeyArray)
  57.      theKeyWord = theKeyArray(aindex)
  58.       
  59.      If Instr(tabText,theKeyWord) > 0 Then
  60.       pairCount = pairCount + 1
  61.       Reporter.ReportEvent micFail,"页面提交操作时系统提示:",tabText
  62.      End If
  63.      
  64.      aindex = aindex + 1
  65.     Next
  66.    Next
  67.    If  pairCount > 0 Then
  68.     Wshshell.AppActivate(brTit)
  69.     Win32Browser(bindex).CaptureBitmap fileName
  70.     '如果用户选择发现错误之后退出运行则退出运行   
  71.     If  isExitRun = "Y" Then
  72.      Set Win32Tab = Nothing
  73.      Set TabObj = Nothing
  74.      Set Win32Page = Nothing
  75.      Set PageObj = Nothing
  76.      Set Win32Browser = Nothing
  77.      Set BrowserObj = Nothing
  78.      Set Wshshell = Nothing
  79.      ExitRun
  80.     End If
  81.    Else
  82.     Reporter.ReportEvent micPass,"页面提交之后没有发现错误","函数【JudgeBrowserErrInfo】已经遍历页面每一个角落,没有发现任何异常信息!"
  83.    End If
  84.    
  85.    Set Win32Tab = Nothing
  86.    Set TabObj = Nothing
  87.   Next
  88.   
  89.   Set Win32Page = Nothing
  90.   Set PageObj = Nothing
  91.   
  92.   '关闭多余的IE页面  
  93.   If brNum > orgBrowserNum Then
  94.    For brindex = orgBrowserNum to brNum - 1
  95.     Win32Browser(brindex).Close
  96.    Next
  97.   End If
  98. Next

  99. Set Win32Browser = Nothing
  100. Set BrowserObj = Nothing
  101. Set Wshshell = Nothing
  102. End Sub
复制代码
回复 支持 反对

使用道具 举报

该用户从未签到

4#
 楼主| 发表于 2010-12-12 21:19:18 | 只看该作者
顺带说一句,只能在QTP里面执行,别把它们弄成vbs在那跑,跑不成滴
回复 支持 反对

使用道具 举报

该用户从未签到

5#
发表于 2010-12-12 22:47:30 | 只看该作者
非常感谢
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-11 04:03 , Processed in 0.071714 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表