Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic error handling
#1
Solved: 10 Years, 10 Months, 1 Week ago
Yeah, I'm a noob...but I have no one else to help me...

What is the elegant/accepted way to do error checking in QTP VB Script? I want the test to not stop on an application error, but rather mark the test as Failed and hopefully give me a clue what the error was.

Seems simple, but I can't think of a good way to do it.
For example, if I do this:

1: On error resume next
2: Dialog("somewindow").Select "something"
2: Dialog("something").WinEdit("afield").Set "hi"
3: Dialog("something").WinEdit("afield2").Set "hi"
...
100: Dialog("something").WinEdit("afield100").Set "howdy"
101: if err.number<>0 then Reporter.ReportEvent micFail, "Entering data in big window", ("Error # " & CStr(Err.Number) & " " & Err.Description)


The problem is the error might be, "could not set value of afield100" when the real error was that we couldn't open the window in Line 2.

Because there is no, "On error Goto line/tag" in QTP VBScript I have no way of knowing what line the error happened on....do I?
I don't want to know the LAST err.number we got, I want to know the FIRST err.number we got.

You don't want to do, "If err.number<>0..." after every single line do you?

About the best I can think of is something like:

1: On error resume next
2: step =1
3: Dialog("somewindow").Select "next window"
4: step =2
5: Dialog("something").WinEdit("afield").Set "hi"
...
99: step = 100
100: Dialog("something").WinEdit("afield100").Set "howdy"
101: if err.number<>0 then Reporter.ReportEvent micFail, "Entering data in big window", "error after Step " & step & err.description

But that is pretty clunky....there must be a better way.
I just want to know what line caused the first error-or just what the first error was- but doing error checking after every line would be ridiculous.

Ideas?

Thanks,
Mark
Reply
#2
Solved: 10 Years, 10 Months, 1 Week ago
Hey Mark,

Very informative research. Yes, I understand your pain. I am working on Continous Integration the NextGen in Test Automation and the criteria for automation is to execute without a break. To Do that we need to have exemplary error handlers and I am not even Close :-)

However, I can share what I have learnt. I think one of the best usage of functions is to individually handle each action. Here is a crude way of how it could be done, I would encourage you to enhance and script it according to your needs.
Code:
Public Function oWebList_SelectVal(WebListObj, Val2)
RetVal = -1
On Error Resume Next
  ' you code here...
  .....
  .....
  If Err.Number <> 0 then
       Msgbox "Fail"    
       Exit Function
  End If
RetVal = 0
End Function

Public Function SelectDialog_WebList(Obj)
RetVal = -1
On Error Resume Next
If Obj.Exists And Obj.Enabled Then
    Call oWebList_SelectVal(Obj, Val2)
  Else
        'Log the error
End If
  If Err.Number <> 0 then
      '''Log Fail
       Exit Function
  End If
RetVal = 0

End Function

' ' ' On your main Code
Set WebListObj = Browser().Page().Weblist()
intRet = SelectDialog_WebList(WebListObj)
If intRet <> 0 Then
  ' ' ' Log Error
End if
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#3
Solved: 10 Years, 10 Months, 1 Week ago
Hey Basanth,

Thanks for the input.

I think you are saying to make your function call for every value I set in the app. That is probably the right way to handle it, but it still means I'm effectively doing, "If Err.Number <> 0 then" after every single line of my code that sets a value. That just seems like a huge amount of overhead to me. It is a lot cleaner looking than having "If Err.Number <> 0 then" after every line though.

I guess I was hoping there was something like, "on error goto MyErrorHandler" which would let me execute a large chunk of code, but still let me know where the error happened or what the error was.

Sounds like there is just no way to do that in QTP. Oh well.

I guess when you buy an almost-free program like QTP you can't have everything ;-).

Thanks,
Mark
Reply
#4
Solved: 10 Years, 10 Months, 1 Week ago
Well, I have to agree with you to an extent. But, vbscript offers all it could offer Smile. However, QTP's log has pretty elaborate error reporting if you use the Reporter.ReportEvent function or even if the object failed it will report the reason. You may want to work on customizing QTP report so that you can add the Err.Values in there.

And for the too many If statements, well, it is basically the net you have to weave to catch the errors. When you are done weaving, what looks like a clutter actually turns out to be an well layered trap. Trust me, it does not slow down execution.

All that said, Error Handling is an challenge and if you could find an impoverished way please do post it across.
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Estimate for preparing a basic test script QTPBeginner48 4 2,941 02-25-2014, 03:45 PM
Last Post: guin.anirban
  Flight reservation basic parameterization not working on qtp 11.5 srs246 0 3,679 01-08-2013, 10:14 AM
Last Post: srs246
  3 basic questions..please help Blizna 5 5,040 07-09-2010, 12:21 PM
Last Post: sasmitakumari
  Visual Basic Application Recording dlopes 2 2,473 07-09-2010, 10:21 AM
Last Post: Saket
  What is the errors handling ? keni 1 2,523 10-26-2009, 09:47 AM
Last Post: Saket

Forum Jump:


Users browsing this thread: 1 Guest(s)