Monday, February 22, 2016

Log monitoring using TestComplete (Windows example)

TestComplete provides excellent all round test automation features. Some AUT (application under test) might be writing to log files. Validation of those log entries or kicking off particular tests when certain log entries are observed might become necessary.
While TestComplete allows file handling using the scripting language libraries, here is one way of using more relevant utilities to do the work and make things efficient.

Example:
Monitor a text log file till you come across an error line (with work "Error"). When that happens send an email to a particular address notifying the recipient of the error.

1) Use windows PowerShell to monitor the log:
We can use TestComplete to kick off a PowerShell prompt and have it run the command to tail an existing log file.

Get-Content C:\temp\myLog.txt -Tail 1 -Wait | select-string -pattern 'Error' | Out-File C:\temp\flagError.txt

The above statement monitors the tail end of the log file myLog.txt for a string that matches a pattern error. When the latest file entry is written, if it contains text Error (case insensitive), it will create a file flagError.txt with that entire string. for e.g. If the last line added says 'Error: File not found' then this string will be written into the flagError.txt

VBScript Example:
FUNCTION monitor_LogTail(ByVal checkWord, ByVal filePath_Log)
  DIM wsh : SET wsh = CreateObject("WScript.shell")
  DIM launch_pshell : launch_pshell = "powershell.exe -noexit -ExecutionPolicy ByPass"

  DIM monitorCommand : monitorCommand = """Get-Content " & filePath_Log & " -Tail 1 -Wait | select-string -pattern '" & checkWord & "' | Out-File C:\temp\flagError.txt"""
  DIM final_pshCommand : final_pshCommand = launch_pshell & " " & monitorCommand
  Log.Message("PShell command : " & final_pshCommand)
  r = wsh.run(final_pshCommand,,true)
END FUNCTION


SUB tst_LogMonitor
  CALL monitor_LogTail("Error", "C:\temp\myLog.txt")
END SUB


2) After kicking off the above command in the shell, TestComplete can be used to monitor the folder 'C:\temp' for the existence of the file flagError.txt

3) As soon as the file get's created, the shell prompt is closed from TestComplete

4) Open the file flagError.txt if we need the error string in TestComplete.  If we don't need the error string, the file's creation signals what we are looking for in the log.

5) Delete the file flagError.txt. Continue processing next set of instructions in the test.


No comments:

Post a Comment