我的 AutoIt 脚本会发送一个点击和按键列表来自动化一个旧的闭源应用程序。
它有错误,所以我想知道如何调试 AutoIt 脚本。或者至少输出脚本的行号(以显示实时执行的代码)。
我的 AutoIt 脚本会发送一个点击和按键列表来自动化一个旧的闭源应用程序。
它有错误,所以我想知道如何调试 AutoIt 脚本。或者至少输出脚本的行号(以显示实时执行的代码)。
如何调试 AutoIt 代码?
根据文档 - 介绍 - AutoIt 语法检查器(Au3Check):
检查脚本的语法错误。
例子:
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7
在执行和编译之前报告(几乎)每个非运行时错误。
… 告诉我哪些代码是实时执行的…
根据文档 - 功能参考 -AutoItSetOption()
:
TrayIconDebug
如果启用,则在托盘图标提示中显示当前脚本行以帮助调试。
例子:
AutoItSetOption("TrayIconDebug", 1)
#AutoIt3Wrapper_Run_Debug_Mode=Y
运行带有控制台调试的脚本。
由于前置包含文件,编译脚本的行号(在错误消息中)与原始 .au3 文件不匹配。根据文档 - SciTE4AutoIt3 - Au3Stripper:
如果没有错误,运行 Au3Stripper 在包含所有源(脚本和任何#include 文件)的同一文件夹中创建一个剥离的源文件 Scriptname_Stripped
例子:
#AutoIt3Wrapper_Run_Au3Stripper=y
已编译脚本的错误消息中的行号与scriptname_stripped.au3
现在(编译后出现)中的行号匹配。
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()
查看数组变量的结构和内容。如果断言失败,则显示一条消息
相关。用于回归测试和验证边缘情况和假设(或在意外情况下简单地中止)。相关功能包括:
IsBinary()
IsBool()
IsDllStruct()
IsFloat()
IsHWnd()
IsInt()
IsNumber()
IsPtr()
IsString()
IsDeclared()
IsFunc()
IsKeyWord()
FileExists()
通常函数返回-(或组合)返回值或-错误代码。非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
或更具体地说:
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
返回值启用如下结构:
If Not SomeFunction() Then
; Handle here.
EndIf
; Continuation of script here.
将错误代码与短信相关联可能会有所帮助。如果在多个相互调用的相关函数之间共享时特别有用,这些函数透明地返回遇到的错误代码(例如,一些从依赖FindOnPage()
项返回)。$ERR_PAGELOAD
LoadPage()
In SciTE from Tools select “Trace: Add trace lines”. This will add a ConsoleWrite to each line if nothing is selected. If you select some code first it will add ConsoleWrite lines to what you have selected.
If you are getting an error in your compiled code you can add this to the top of your script before compiling it. When it errors out it will give you the right line number in your script.
#Au3Stripper_Parameters=/mo