在QTP中,是否有可能基于被调用函数的结果来调用函数

时间:2014-08-14 13:23:59

标签: vba vbscript qtp

我正在从我的动作调用函数算法并且正在执行该函数。从该函数我调用另一个函数来操作精确数据。如果我收到错误,这意味着我不想执行function1中的其余代码。我已尝试在两个函数中使用错误恢复,仅在function1中,单独使用function2但是它没有用于该场景。这是一个示例代码。这将从我的行动中调用。另外,我不想每次都从function2传递一个变量来验证err.number并继续执行function1中的步骤

Function arithmetic()

Call div (21,0)

Call add (21,0)

Call sub(21,0)

End function

Function div(a,b)

A=a/b

End function

在这种情况下,我会收到错误消息。所以我不希望其余的代码在函数lik函数add和sub中执行。直接我必须回到行动,并且必须验证另一组数据。

1 个答案:

答案 0 :(得分:0)

要为一系列不应继续进行的依赖动作执行合适的VBScript错误处理,请遵循以下规则:

  1. 将操作序列放入子/函数
  2. 在OERN中调用此子/函数
  3. OERN范围内只有一条风险线
  4. 演示读取,计算/保存和zap(旧)数据的脚本:

    Option Explicit
    
    Dim iRet
    Dim aErr
    On Error Resume Next  ' disable while development to see error line
      iRet = main() ' just *one* risky line in OERN scope
      aErr = Array(Err.Number, Err.Description, Err.Source)
    On Error GoTo 0
    If 0 <> aErr(0) Then
       WScript.Echo "fatal error", aErr(0), "-", aErr(1), "in", aErr(2)
       iRet = 1
    Else
       WScript.Echo "success"
    End If
    WScript.Quit iRet
    
    Function main()
      main = 2
      ReadData
      CalcAndSave
      ZapOld
      main = 0
    End Function
    
    Sub ReadData()
      If WScript.Arguments.Named.Exists("read") Then Err.Raise 123, "ReadData()", "can't read"
      WScript.Echo "reading data"
    End Sub
    
    Sub CalcAndSave()
      If WScript.Arguments.Named.Exists("calc") Then Err.Raise 124, "CalcAndSave()", "can't calc and save"
      WScript.Echo "calc and save data"
    End Sub
    
    Sub ZapOld()
      If WScript.Arguments.Named.Exists("zap") Then Err.Raise 125, "ZapOld()", "can't zap"
      WScript.Echo "zap old data"
      If WScript.Arguments.Named.Exists("surprise") Then
         Dim i : i = 1 / 0
      End If
    End Sub
    

    称之为

    cscript 25309137.vbs /calc
    reading data
    fatal error 124 - can't calc and save in CalcAndSave()
    

    禁用一个且仅限严格本地 OERN以确保您没有隐藏任何错误并在开发时获取行号信息:

    cscript 25309137.vbs /surprise
    reading data
    calc and save data
    zap old data
    E:\trials\SoTrials\answers\25301708\vbs\25309137.vbs(39, 14) Microsoft VBScript runtime er
    o
    

    在您的情况下,风险序列是在循环中完成的。根据规则,演示代码应如下所示:

    Option Explicit
    
    WScript.Quit main()
    
    Function main()
      Dim aErr, n
      For Each n In Array(1, 2, 0, 4)
        On Error Resume Next  ' disable while development to see error line
          arithmetic n ' just *one* risky line in OERN scope
          aErr = Array(Err.Number, Err.Description, Err.Source)
        On Error GoTo 0
        If 0 <> aErr(0) Then
           WScript.Echo "fatal error", aErr(0), "-", aErr(1), "in", aErr(2)
        End If
      Next
      main = 0
    End Function
    
    Sub arithmetic(n)
      divide n
      add    n
      mult   n
    End Sub
    
    Sub divide(n)
      WScript.Echo n, 1 / n
    End Sub
    
    Sub add(n)
      WScript.Echo n, 1 + n
    End Sub
    
    Sub mult(n)
      WScript.Echo n, 1 * n
    End Sub
    

    输出:

    cscript 25309137-2.vbs
    1 1
    1 2
    1 1
    2 0,5
    2 3
    2 2
    fatal error 11 - Division by zero in Microsoft VBScript runtime error
    4 0,25
    4 5
    4 4