以后不在此处更新日志了,欢迎测友到新窝找我:http://www.wuchuanhu.cn/

使用AutoIT测试系统登录实例九(注册IE错误处理)

上一篇 / 下一篇  2012-02-13 10:15:36 / 个人分类:AutoIT实战

   AutoIT中经常会使用到_IEErrorHandlerRegister来截获IE操作中产生的错误,其本质还是使用前面介绍到的ObjEvent,所以个人觉得,当做Web自动化测试的时候,可以普遍的使用_IEErrorHandlerRegister来替代ObjEvent。

#Include <IE.au3>

$testURL="http://www.baidu.com"

$OIE = CreateIE($testURL)

 $EventObject=_IEErrorHandlerRegister("MyErrFunc")
 
   $OIE.navigate($testURL)
    $OIE.Visible = True
$doc = _IEDocGetObj($OIE)
$title=$doc.title
Send("!+{SPACE}+X")
CloseIE($OIE)
$EventObject.stop
 
Func CreateIE($URL)
   $OIE = _IECreate($URL,0,1,1,0)
      Return $OIE
   If @error Then MsgBox(1,"错误","启动IE失败")
EndFunc
  
Func CloseIE($OIE)
   $IsClose=_IEQuit($OIE)
   If $IsClose<>1 Then
   ProcessClose($OIE)
   EndIf
EndFunc
  
Func MyErrFunc()
    ; Important: the error object variable MUST be named $oIEErrorHandler
    Local $ErrorScriptline = $oIEErrorHandler.scriptline
    Local $ErrorNumber = $oIEErrorHandler.number
    Local $ErrorNumberHex = Hex($oIEErrorHandler.number, 8)
    Local $ErrorDescription = StringStripWS($oIEErrorHandler.description, 2)
    Local $ErrorWinDescription = StringStripWS($oIEErrorHandler.WinDescription, 2)
    Local $ErrorSource = $oIEErrorHandler.Source
    Local $ErrorHelpFile = $oIEErrorHandler.HelpFile
    Local $ErrorHelpContext = $oIEErrorHandler.HelpContext
    Local $ErrorLastDllError = $oIEErrorHandler.LastDllError
    Local $ErrorOutput = ""
    $ErrorOutput &= "--> COM Error Encountered in " & @ScriptName & @CR
    $ErrorOutput &= "----> $ErrorScriptline = " & $ErrorScriptline & @CR
    $ErrorOutput &= "----> $ErrorNumberHex = " & $ErrorNumberHex & @CR
    $ErrorOutput &= "----> $ErrorNumber = " & $ErrorNumber & @CR
    $ErrorOutput &= "----> $ErrorWinDescription = " & $ErrorWinDescription & @CR
    $ErrorOutput &= "----> $ErrorDescription = " & $ErrorDescription & @CR
    $ErrorOutput &= "----> $ErrorSource = " & $ErrorSource & @CR
    $ErrorOutput &= "----> $ErrorHelpFile = " & $ErrorHelpFile & @CR
    $ErrorOutput &= "----> $ErrorHelpContext = " & $ErrorHelpContext & @CR
    $ErrorOutput &= "----> $ErrorLastDllError = " & $ErrorLastDllError
    MsgBox(0, "COM Error", $ErrorOutput&"注册的事件")
    SetError(1)
    Return
EndFunc 

上面这段脚本会在第15行$EventObject.stop 出现错误,注册的MyErrFunc函数则会捕获这个错误。需要注意的是,如果你想在MyErrFunc函数中输出详细的错误信息,必须给这个函数添加一个参数,如下:
Func MyErrFunc($oError)
    ; Important: the error object variable MUST be named $oIEErrorHandler
       ConsoleWrite("err.number is: " & @TAB & $oError.number & @CRLF & _
            "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            "err.description is: " & @TAB & $oError.description & @CRLF & _
            "err.source is: " & @TAB & $oError.source & @CRLF & _
            "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            "err.retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc 

与_IEErrorHandlerRegister对应的IEErrorHandlerDeRegister()函数则是移除错误截获。


TAG: aslandhu autoit Autoit AutoIT autoIT web自动化测试

 

评分:0

我来说两句

Open Toolbar