How would you generate your own customized Logs during the execution of script for Exceptions/Information cases?
This post would provide some information and scripts to generate customized logs in QTP. First let us discuss what is the advantage in having customized logs for our scripts.
- Assume that you have 1000 scripts to be executed. And you don't want your script to be stopped in between in case of any error(s) by using On Error Resume Next, but you would like to catch these exceptions and store it at some place so that you can have a look at those errors later, you can use these customized logging functions.
- Also if you wish to add series of logging statements about the way your script is being executed based on certain inputs, you can add these logging statements where ever required in your script.
- Once the script execution is complete, you can have a look at these log files and see if there are any errors during your script execution and see how the script ran.
1. Create a VBS file(say for example Logs.Vbs) with a Class for Log. This class needs to have a series of Sub statements for starting the Log, Ending the Log, Adding Exception and for adding Inline statements. Please have a look at the following code:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Class ClsLogger
Public v_StepName, objLogger
'Description : Function used to initiate the log file
'Arguments : sStepName(String) -- Name of the step(can be any text)
Public Sub LogStart(ByVal sStepName)
Dim objFSO
v_StepName=sStepName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogger=objFSO.OpenTextFile("C:\QTP\Scripts\Logs.txt", 8, True)
objLogger.WriteLine(vbcrlf)
objLogger.WriteLine(Now() & " '" & sStepName & "' execution has been Started" )
End Sub
'Description : Function used to end the log file
'Arguments : N/A
Public Sub LogEnd()
objLogger.WriteLine(Now() & " '" & v_StepName & "' execution has been Completed" )
objLogger.WriteLine(vbcrlf)
objLogger.Close
SET objLogger = NOTHING
SET objFSO = NOTHING
End Sub
'Description: Function used to capture any exception in log file that is occurred during script execution
'Arguments 1: sStepName(String)-- Name of the step(can be any text)
'Arguments 2: sException(String)-- Exception that needs to be logged
Public Sub LogException(ByVal sStepName, ByVal sException)
objLogger.WriteLine(Now() & " Exception has been raised on step '" & sStepName & "': " & sException & Err.Description)
objLogger.WriteLine(vbcrlf)
Err.Clear
End Sub
'Description: Function used to write any statement in log file
'Arguments: sLine(String)-- Statement to be written in log(can be any text)
Public Sub LogWrite(sLine)
objLogger.WriteLine(Now() & " " & sLine)
objLogger.WriteLine(vbcrlf)
End Sub
End Class
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
2. Once the logging class creation is completed, create a separate Vbs file(say for example variables.vbs) to declare the class as a variable. You Can add all your variables related to your scripts to the same Vbs file.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim objScriptLogger
Set objScriptLogger=New ClsLogger
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Why do we need to write 2 separate statements as above when we can write a single statement as Dim objScriptLogger As New ClsLogger?
This is because if it is declared in a single statement, VB would treat that variable as an auto-instantiated variable and as a result where ever it is used, VB would check whether it should be instantiated which would degrade the speed by adding some additional CPU Cycles
3.Now in your Script for each Action, at the top add the following line:
objScriptLogger.LogStart "Action <Action Name> "
4. In the same way add the following line at the end of each Action:
objScriptLogger.LogEnd
5. If you need to capture any statement in your log file for any action, use the following statement in those actions:
objScriptLogger.LogWrite "Step number 5 has been executed successfully"
6. For capturing Exceptions in Log files, use "On Error Resume Next" at the top of your action. And then use the following set of statements before LogEnd statement:
IF Err.Number<>0 Then
objScriptLogger.LogException "Error occurred while executing <Action Name>",""
End If
Note: Make sure that you attach both these Vbs files to your actions before executing them.
ReplyDeleteThis is really nice. Thanks for sharing this informative article.
Custom Phone Cases