Tuesday, July 3

Maintain a Log file in QTP

Hello Friends!

Don't you think its useful to maintain a log file during execution of your QTP script?

I think yes. It's specifically useful when you have long scripts or if your script is calling lots of functions. Log file gives a good grip on your execution. It is good for debugging purpose as well.

In this post we will see how to maintain a log file by using a easy vb function..

Function f_WriteToLogFile(LogMsg)
 Const ForAppending = 8
 Set oFSO = CreateObject("Scripting.FileSystemObject")

 'Check if file already exists. If not, create it.
 If oFSO.FileExists("c:\file_path\filename.txt") = False Then
   Set oFile = oFSO.CreateTextFile("c:\file_path\filename.txt",True)

   Set oFile = Nothing
 End If

  Set oTxtFile = oFSO.OpenTextFile("c:\file_path\filename.txt", ForAppending, True)
  oTxtFile.WriteLine LogMsg
End Function

How to call above function:

For example you have a script of 100 lines. You can call this function after every task/funtion call or in loops to see which condition was true or anything you want to send in your log file.

line 1
line 2
line 15
f_WriteToDriverLogFile("line 15 executed...")

line 16
line 17

once your execution is done, you can open your log file to see all log messages.