cancel
Showing results for 
Search instead for 
Did you mean: 

VBScript Using a function to run an unstable function/sub multiple times

colsson
Occasional Contributor

VBScript Using a function to run an unstable function/sub multiple times

Hello!

 

Do you use any utility-function to attempt a call of an unstable function/sub multiple times in TestComplete using VBScript?

Here's a proposal:

Function tryTimes(times, unitName, functionName, arguments)
  fullFunctionName = unitName + "." + functionName
  lastIndex = UBound(arguments)
  Dim argumentsAsStrings()
  Redim argumentsAsStrings(lastIndex)
  For index = 0 To lastIndex
    argumentsAsStrings(index) = "arguments(" + CStr(index) + ")"
  Next
  argumentsString = Join(argumentsAsStrings, ", ")
  functionCall = fullFunctionName + "(" + argumentsString + ")"
  attempts = 0
  success = False
  value = Null
  errorDescription = Null
  While attempts < times And Not success
    Err.Clear
    Dim functionCallResult
    On Error Resume Next
    functionCallResult = Eval(functionCall)
    If Err.Number = 0 Then
      success = True
      value = functionCallResult
    Else
      errorDescription = Err.Description
      Err.Clear
    End If
    attempts = attempts + 1
  Wend
  Dim resultDictionary
  Set resultDictionary = CreateObject("Scripting.Dictionary")
  resultDictionary.add "errorDescription", errorDescription
  resultDictionary.add "success", success
  resultDictionary.add "value", value
  Set tryTimes = resultDictionary
End Function

attempts = 10
Set tryResult = tryTimes(attempts, UnitName, UnstableFunctionOrSubName, Array(Param1, Param2, ...))

 

 

Kind Regards

Christian Olsson

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors