Qtp 简明教程

QTP - Error Handling

在 QTP 中处理错误有多种方法。在使用 QTP 时,可能遇到三种可能的错误类型:

There are various ways of handling errors in QTP. There are three possible types of errors, one would encounter, while working with QTP. They are −

  1. Syntax Errors

  2. Logical Errors

  3. Run Time Errors

Error Types

Syntax Errors

语法错误是与 VBScript 语言语法不符的错误或代码片段。语法错误发生在代码编译时,在这些错误得到修复之前无法执行。

Syntax errors are the typos or a piece of the code that does not confirm with the VBscripting language grammar. Syntax errors occur at the time of compilation of code and cannot be executed until the errors are fixed.

若要验证语法,可以使用键盘快捷键 Ctrl+F7,结果显示在下边。如果窗口未显示,则可以导航至“视图”→“错误”。

To verify the syntax, use the keyboard shortcut Ctrl+F7 and the result is displayed as shown below. If the window is not displayed one can navigate to "View" → "Errors".

qtp error handling 1

Logical Errors

如果脚本的语法正确,但是生成了意外的结果,则称为逻辑错误。逻辑错误通常不会中断执行,但是会生成错误的结果。逻辑错误可能由于各种原因导致,例如:错误的假设或需求错误理解,有时还会产生错误的程序逻辑(使用 do-while 代替 do-Until)或无限循环。

If the script is syntactically correct but it produces unexpected results, then it is known as a Logical error. Logical error usually does not interrupt the execution but produces incorrect results. Logical errors could occur due to variety of reasons, viz- wrong assumptions or misunderstandings of the requirement and sometimes incorrect program logics (using do-while instead of do-Until) or Infinite Loops.

检测逻辑错误的一种方法是进行同行评审,还要验证 QTP 输出文件/结果文件,以确保该工具按预期方式执行。

One of the ways to detect a logical error is to perform peer reviews and also verify the QTP output file/result file to ensure that the tool has performed the way it was supposed to do.

RunTime Errors

顾名思义,此类错误发生在运行时。此类错误的原因是脚本尝试执行某项操作但无法执行,而且脚本通常停止执行,因为它无法继续执行。运行时错误的经典示例是:

As the name states, this kind of error happens during Run Time. The reason for such kind of errors is that the script trying to perform something is unable to do so and the script usually stops, as it is unable to continue with the execution. Classic examples for Run Time Errors are −

  1. File NOT found but the script trying to read the file

  2. Object NOT found but the script is trying to act on that particular object

  3. Dividing a number by Zero

  4. Array Index out of bounds while accessing array elements

Handling Run-Time Errors

处理代码中错误有多种方法。

There are various ways to handle errors in the code.

1. Using Test Settings - 可以通过导航至“文件”>>“设置”>>“运行”选项卡来在测试设置中定义错误处置,如下所示。我们可以选择任何指定设置,然后单击“确定”。

1. Using Test Settings − Error handling can be defined the Test Settings by Navigating to "File" >> "Settings" >> "Run" Tab as shown below. We can select any of the specified settings and click "OK".

qtp error handling 2

2. Using On Error Statement - 测试者可以将“On Error”语句用于通知 VBScript 引擎其打算处理运行时错误,而不是允许 VBScript 引擎显示用户不友好的错误消息。

2. Using On Error Statement − The ‘On Error’ statement is used to notify the VBScript engine of intentions to handle the run-time errors by a tester, rather than allowing the VBScript engine to display error messages that are not user-friendly.

  1. On Error Resume Next − On Error Resume Next informs the VBScript engine to process executing the next line of code when an error is encountered.

  2. On error Goto 0 − This helps the testers to turn off the error handling.

3. Using Err Object - 错误对象是 VBScript 内的一个内置对象,可以捕获我们能够方便调试代码的运行时错误编号和错误描述。

3. Using Err Object − Error object is an in-built object within VBScript that captures the run-time error number and error description with which we are able to debug the code easily.

  1. Err.Number − The Number property returns or sets a numeric value specifying an error. If Err.Number value is 0 then No error has occurred.

  2. Err.Description − The Description property returns or sets a brief description about an error.

  3. Err.Clear − The Clear method resets the Err object and clears all the previous values associated with it.

Example

'Call  the function to Add two Numbers Call Addition(num1,num2)

Function Addition(a,b)
   On error resume next
      If NOT IsNumeric(a) or IsNumeric(b) Then
         Print "Error number is  " &  err.number & " and description is :
            " &  err.description
         Err.Clear
         Exit Function
      End If
   Addition = a+b

   'disables error handling
   On Error Goto 0
End function

4. Using Exit Statement − Exit 语句可以与 Err 对象结合使用,以基于 Err.Number 值退出测试、操作或迭代。让我们详细了解每个 Exit 语句。

4. Using Exit Statement − Exit Statements can be used along with Err object to exit from a test or action or iteration based on the Err.Number value. Let us see each one of those Exit statements in detail.

  1. ExitTest − Exits from the entire QTP test, no matter what the run-time iteration settings are.

  2. ExitAction − Exits the current action.

  3. ExitActionIteration − Exits the current iteration of the action.

  4. ExitTestIteration − Exits the current iteration of the QTP test and proceeds to the next iteration.

5. Recovery Scenarios − 遇到错误时,基于特定条件触发恢复场景,并在单独的章节中详细介绍。

5. Recovery Scenarios − Upon encountering an error, recovery scenarios are triggered based on certain conditions and it is dealt in detail in a separate chapter.

6. Reporter Object − Reporter 对象帮助我们将事件报告给运行结果。它帮助我们识别相关的操作/步骤是通过/失败。

6. Reporter Object − Reporter Object helps us to report an event to the run results. It helps us to identify if the concerned action/step is pass/fail.

'Syntax: Reporter.ReportEventEventStatus, ReportStepName, Details,
[ImageFilePath]

'Example
Reporter.ReportEvent micFail, "Login", "User is unable to Login."