My Blog List

Monday, May 4, 2015

Error Handling in UFT - Contd...

See the basic post if you didn't.

Now you wonder, is there a way to implement inside our code.
Here we go;

Browser("myapp").Page("myPage").WebEdit("mytext1").Set "someText1"
If Err.Number <> 0 Then
  Reporter.ReportEvent micFail, "some step", "some description"
  Err.Clear
End If
'after handling the error, continue to next step
Browser("myapp").Page("myPage").WebEdit("mytext2").Set "someText2"
If Err.Number <> 0 Then
  Reporter.ReportEvent micFail, "some step", "some description"
  Err.Clear
End If

So the complexity is, we need to add the error check block after each step which is not good.So what can we do better is;

Function CheckError
  If Err.Number <> 0 Then
    Reporter.ReportEvent micFail, "some step", "some description"
    Err.Clear
  End If
End Function
Browser("myapp").Page("myPage").WebEdit("mytext1").Set "someText1"
Call CheckError
What else we can do still to avoid even these calls but need this error handling.
Using Recovery Scenario:
You can create a Recovery Scenario with the event "Test Run Error" and add this CheckError function as part of recovery function. Now it should call and check for errors, do the recovery steps described as part of your function whenever an error happens.

Also you can write some common function to do all events with error handling like this and use.

Function Simulate(objUICtrl, strValue)
    On Error Resume Next

    Select UCase(objUICtrl.GetROProperty("micClass"))
           Case "WEBEDIT":
                     objUICtrl.Set Trim(strValue)
           Case "WEBLIST":
                     objUICtrl.Select Trim(strValue)
           Case "WEBELEMENT":
                     objUICtrl.Type Trim(strValue)
    End Select

   If Err.Number <> 0 Then
        'Your recovery steps...
   End If
   Err.Clear 'always safety
End Function
RegisterUserFunc "WebEdit", "Set", "Simulate", True
RegisterUserFunc "WebList", "Select", "Simulate", True
RegisterUserFunc "WebElement", "Type", "Simulate", True
Usage:
Browser("myApp").Page("myPage").WebEdit("someText").Simulate "someValue"
Browser("myApp").Page("myPage").WebList("someList").Simulate "someValue"

1 comment:

  1. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium

    ReplyDelete