Basic error handling - Printable Version +- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums) +-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP) +--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners) +--- Thread: Basic error handling (/Thread-Basic-error-handling) |
Basic error handling - RandomGrin - 10-10-2013 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 RE: Basic error handling - basanth27 - 10-11-2013 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) RE: Basic error handling - RandomGrin - 10-11-2013 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 RE: Basic error handling - basanth27 - 10-12-2013 Well, I have to agree with you to an extent. But, vbscript offers all it could offer . 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. |