Pages

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.

4 comments:

  1. Thanks , looks good except for..you need to add below code before opening the file for appending.

    Set oFile = Nothing


    ReplyDelete
  2. Thanks , looks good except for..you need to add below code before opening the file for appending



    Set oFile = Nothing

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. RV Park Lake Buchanan

    ReplyDelete