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.


Tuesday, February 2, 2016

Connecting to Oracle Database from VB Script

Recently while trying to connect to an Oracle DB using VBScript within TestComplete, I ran across the error "Oracle client and networking components were not found. These components are supplied by Oracle Corporation and are part of the Oracle Versi on 7.3.3 or later client software installation."

I was using the following code:

FUNCTION test_sql
DIM ado_Connection, ado_RecordSet
SET ado_Connection = CreateObject("ADODB.Connection")

SET ado_RecordSet = CreateObject("ADODB.Recordset")
Dim strSQLQuery: strSQLQuery = "SELECT * FROM ABC where rownum < '2'"
Dim strDBDesc: strDBDesc = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=11.1.111.111)(PORT=1521)))(CONNECT_DATA=(SID=DB_QA)))"
Dim strConnection:strConnection = "Provider=OraOLEDB.Oracle;Data Source=" & strDBDesc & ";User ID=uid;Password=pwd;"
 

ado_Connection.ConnectionString = strConnection

ado_RecordSet.Open strSQLQuery, ado_Connection, adOpenForwardOnly, adLockReadOnly, adCmdText
 

'Process Recordset obtained

END FUNCTION


These are the steps I used to 'finally' (phew!) get it to work.

TestComplete uses 32 bit ODBC libraries.

1) Download and unzip to any directory
 ODAC121024Xcopy_32bit.zip

2) Open the readme.html and follow instructions to install 'all' components and then add the the install directory and the install directory's bin subdirectory to the system PATH environment variable.

3) Open the 32 bit ODBC data source administrator from 'C:\Windows\SysWOW64' (file:odbcad32.exe)

4) Click Add

5) Find 'Microsoft ODBC for Oracle' and it.

Run the above code. It works