调试 AutoIt 脚本或获取当前执行的脚本行号

5 autoit script-debugging

我的 AutoIt 脚本发送一个点击和按键列表来自动化旧的封闭源应用程序。

它有错误,所以我想知道如何调试 AutoIt 脚本。或者至少输出脚本的行号(以显示实时执行的代码)。

use*_*124 7

如何调试 AutoIt 代码?

句法

Au3Check

根据文档 - 介绍 - AutoIt 语法检查器(Au3Check)

检查脚本的语法错误。

例子:

#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7
Run Code Online (Sandbox Code Playgroud)

在执行和编译之前报告(几乎)每个非运行时错误。

运行

当前线路

... 告诉我哪些代码是实时执行的 ...

根据文档 - 函数参考 -AutoItSetOption()

TrayIconDebug 如果启用,会在托盘图标提示中显示当前脚本行以帮助调试。

例子:

AutoItSetOption("TrayIconDebug", 1)
Run Code Online (Sandbox Code Playgroud)

编译好的脚本

#AutoIt3Wrapper_Run_Debug_Mode=Y 使用控制台调试运行脚本。

由于前置包含文件,已编译脚本的行号(在错误消息中)与原始 .au3 文件不匹配。根据文档 - SciTE4AutoIt3 - Au3Stripper

如果没有错误,请运行 Au3Stripper 在包含所有源(脚本和任何 #include 文件)的同一文件夹中创建一个剥离源文件 Scriptname_Stripped

例子:

#AutoIt3Wrapper_Run_Au3Stripper=y
Run Code Online (Sandbox Code Playgroud)

已编译脚本的错误消息中的行号与scriptname_stripped.au3now 中的行号(编译后出现)相匹配。

错误代码和返回值

  • ConsoleWrite(). 添加#AutoIt3Wrapper_Change2CUI=y是否要从编译脚本中读取。
    • 使用的宏包括@error,@extended@ScriptLineNumber
    • SciTE4AutoIt3>> Tools>Trace: Add Trace Lines为每一行插入这样的命令。显示当前@error代码(和相关行)。
    • SciTE4AutoIt3> Tools> Debug to Console( Alt+ D) 为当前选定的行插入这样的命令。显示@ScriptLineNumber、返回值和@error代码。第二次执行原始行以获取其返回值(可能不需要)。
  • VarGetType()返回一个变体的内部类型表示
  • _ArrayDisplay() 查看数组变量的结构和内容。
  • 如果需要远程调试,可能会记录到文件数据库
  • 为简化起见,记录 UDF(示例)时出错。

断言

根据文档 - 用户定义的函数参考 -_Assert()

如果断言失败则显示消息

相关的。用于回归测试和验证边缘情况和假设(或在意外情况下简单地中止)。相关功能包括:

错误处理

通常函数返回-(或组合)返回值-错误代码。非0错误代码值(有时为负)表示失败。返回值(如果如此使用)在0(失败)和1(成功)之间交替。

处理出现的错误。脚本自行调试以进行正确的错误处理。避免逐步嵌套If语句。例子:

Global Enum  $RANDOM_FLT, _
             $RANDOM_INT

Global Enum  $ERROR_OK, _
             $ERROR_AIR, _
             $ERROR_WATER, _
             $ERROR_FOOD, _
             $ERROR_ENUM

Global       $g_aError[$ERROR_ENUM]
             $g_aError[$ERROR_OK]    = 'survived'
             $g_aError[$ERROR_AIR]   = 'no air'
             $g_aError[$ERROR_WATER] = 'no water'
             $g_aError[$ERROR_FOOD]  = 'no food'

Global Const $g_sMsgConsole          = 'error:%i - %s\n'

While True
    Live()

    If @error Then
        ConsoleWrite(StringFormat($g_sMsgConsole, @error, $g_aError[@error])); And one of following:
        ContinueLoop
;       ExitLoop
;       Return; If in Local scope.
;       Exit @error
    EndIf

    ; Continuation conditional to successful execution of Live():
    ConsoleWrite(StringFormat($g_sMsgConsole, @error, $g_aError[@error]))
WEnd

Func Live()
    Local Const $iError    = Random($ERROR_OK, $ERROR_ENUM - 1, $RANDOM_INT), _
                $iExtended = 0, _
                $iReturn   = $iError ? 0 : 1

    Return SetError($iError, $iExtended, $iReturn)
EndFunc
Run Code Online (Sandbox Code Playgroud)

或者更具体地说

Live()

Switch @error

    Case $ERROR_AIR
        ; Handle specific error here.

    Case $ERROR_WATER
        ; Handle specific error here.

    Case $ERROR_FOOD
        ; Handle specific error here.

EndSwitch
Run Code Online (Sandbox Code Playgroud)

返回值启用如下结构:

If Not SomeFunction() Then
    ; Handle here.
EndIf
; Continuation of script here.
Run Code Online (Sandbox Code Playgroud)

将错误代码与文本消息相关联可能会有所帮助。如果在多个相互调用的相关函数之间共享,则特别有用,这些函数透明地返回遇到的错误代码(例如,一些从依赖项FindOnPage()返回)。$ERR_PAGELOADLoadPage()


MrA*_*oIt 4

在 SciTE 中,从工具中选择 \xe2\x80\x9cTrace:添加跟踪线\xe2\x80\x9d。如果未选择任何内容,这将向每一行添加一个 ConsoleWrite。如果您首先选择一些代码,它会将 ConsoleWrite 行添加到您选择的内容中。

\n\n

如果您在编译的代码中遇到错误,您可以在编译之前将其添加到脚本的顶部。当它出错时,它会在脚本中为您提供正确的行号。

\n\n
#Au3Stripper_Parameters=/mo\n
Run Code Online (Sandbox Code Playgroud)\n

  • 跟踪线在大多数情况下都可以很好地进行调试。如果它给你一个错误检查,看看你的代码是否有任何 Select 或 Switch 语句。您将需要删除它可能在 Select/Switch 和第一个 Case 之间添加的任何 ConsoleWrite。 (3认同)