Friday, April 22, 2016

Executing Powershell commands through TestComplete

Powershell is an extremely powerful toolbox and the ability to use it through TestComplete adds a whole new dimension to efficiently automating testing and related activities.

Here is a VBScript example of what you might do using TestComplete and Powershell.

Library function to execute Powershell command:

FUNCTION lib_run_pwsh_cmd(ByVal pre_cmd, ByVal psh_cmd)
  DIM wsh : SET wsh = CreateObject("WScript.shell")
  DIM launch_pshell : launch_pshell = "powershell.exe -noexit -ExecutionPolicy ByPass"
  DIM final_pshCommand : final_pshCommand = launch_pshell & " " & pre_cmd & ";" & psh_cmd & ";exit"
  Log.Message("PShell command : " & final_pshCommand)
  r = wsh.run(final_pshCommand,,true)
END FUNCTION


The launch_pshell command fires up the powershell command window with a -noexit flag which means the window will stay open after the execution is done. I choose to do that so in case something doesn't go right, I can see the output in the window to debug.

The pre_cmd is any powershell command that I need to execute before doing the actual operation. e.g. change directory to a particular location i.e. cd "C:\TC11Projects"

the psh_cmd is the actual powershell command/script that I want to execute. e.g. GenerateData.ps1 -data_descriptor "US_PHONE_NUMBERS"

This executes the script GenerateData.ps1 and passes the argument for the data_descriptor parameter.

The final_pshCommand shows what is called command chaining in command line execution which means it executes all the commands separated by ';' one after the other.

The 'exit' command in the end exits powershell.

Here is how I would call the above Function:

SUB TestPowershellFunction
DIM preCmd, pshCmd
preCmd = "cd C:\TC11Projects"
pshCmd = "GenerateData.ps1 -data_descriptor 'US_PHONE_NUMBERS'"
CALL lib_run_pwsh_cmd(preCmd,pshCmd)
END SUB


Wednesday, March 2, 2016

Creating testing Deliverables using TestComplete 2 - Test Log

TestComplete gives plenty of features to log the progress and significant outcomes of a test run using Logging in TestComplete.

To add to this functionality and have a customized deliverable at the end of every test run, I customized logging to log to a text file the progress and results from my test run.

Here is a VBScript sample.
'--------------------------------------------------------------------------------------
'LOG FILE UTILTIES
DIM theLogFile

FUNCTION Log_Start(ByVal msg_start, ByVal filePath)
  CALL lib_CreateFile(filePath)
  SET theLogFile = lib_OpenFileForWriting(filePath)
  theLogFile.WriteLine(now & vbTab & "INF" & vbTab & msg_start)
END FUNCTION

FUNCTION Log_End(ByVal msg_end)
  CALL Log_Write("INFO",msg_end)
  theLogFile .Close
END FUNCTION

FUNCTION Log_Write(ByVal Msg_Type, ByVal Msg)
  SELECT CASE UCASE(Msg_Type)
    CASE "INFO"
      theLogFile.WriteLine (now & vbTab & "INF" & vbTab & Msg)
    CASE "WARNING"
      theLogFile.WriteLine (now & vbTab & "WRN" & vbTab & Msg)
    CASE "ERROR"
      theLogFile.WriteLine (now &  vbTab & vbTab & "ERR" & vbTab & vbTab & Msg)   
    CASE "SUCCESS"
      theLogFile.WriteLine (now & vbTab & vbTab & "SCS" & vbTab & vbTab & Msg)
    CASE "BREAK"
      theLogFile.WriteLine (Msg)
    CASE ELSE
      Log.Error("Could not recognize message type:" & Msg_Type)   
  END SELECT
END FUNCTION

'--------------------------------------------------------------------------------------

The functions  lib_CreateFile and  lib_OpenFileForWriting are library functions I wrote that use the VBScript FileSystemObject to create a file and open the file for writing text respectively. Here is the documentation for that.

Here is how I use the function and how the log file looks like which I submit when asked, "What did/does your automation test test?"

FUNCTION Login
' Log into the website
 CALL Log_Write("INFO", "Logging into the webpage")
IF LoginSuccess THEN
 CALL Log_Write("SUCCESS", "Logging into the webpage")
ELSE
 CALL Log_Write("ERROR", "Logging failed")
END IF
END FUNCTION

I keep track of passed and failed test cases and log them too along with a final testing status at the end

Sample Log file created:

11/11/2015 8:34:39 AM    INF    LOGGING START : 11/11/2015 8:34:39 AM
11/11/2015 8:34:40 AM    INF    <<<<<<< TEST CASE >>>>>>>
11/11/2015 8:34:40 AM    INF    Test Identifier:     CardActivation_01-ActivateCard_ValidSrNo_FD
11/11/2015 8:34:40 AM    INF    >>>>>>> TEST CASE >>>>>>>
11/11/2015 8:35:38 AM    INF    STEP::Verifying if displayed page is :LoginHome
11/11/2015 8:35:41 AM        SCS        STEP::Verifying if displayed page is :LoginHome
11/11/2015 8:35:41 AM    INF    Access: Logout from the VTS website
11/11/2015 8:35:46 AM    INF    STEP::Verifying if displayed page is :Home
11/11/2015 8:35:48 AM        SCS        STEP::Verifying if displayed page is :Home
11/11/2015 8:35:48 AM        SCS        Access: Logout from the VTS website
11/11/2015 8:35:48 AM    INF    REPORT:##### CURRENT TEST CASE REPORT #####
11/11/2015 8:35:48 AM    INF    REPORT:Test Complete : True
11/11/2015 8:35:48 AM    INF    REPORT:Test Passed : True
11/11/2015 8:35:48 AM    INF    REPORT:# of Test Steps : 3
11/11/2015 8:35:48 AM    INF    REPORT:# of Test Points Passed : 3
11/11/2015 8:35:48 AM    INF    REPORT:# of Test Errors Found : 0
11/11/2015 8:35:48 AM    INF    REPORT:#####    FINAL TEST STATUS    #####
11/11/2015 8:35:48 AM    INF    REPORT:Test:CardActivation_01-ActivateCard_ValidSrNo_FD:COMPLETE ; PASSED.
11/11/2015 8:35:48 AM    INF    REPORT##### END TEST STATUS #####
'
'
'
11/11/2015 8:37:15 AM    INF    -----------------------------------------------------------------
11/11/2015 8:37:15 AM    INF    ##### FINAL #####
11/11/2015 8:37:15 AM    INF    ##### TEST REPORT #####
11/11/2015 8:37:15 AM    INF    REPORT:: Total Tests Run = 2
11/11/2015 8:37:15 AM    INF    REPORT:: Pass/Fail Ratio = 2/0
11/11/2015 8:37:15 AM    INF    REPORT:: Total Test Points = 8
11/11/2015 8:37:15 AM    INF   ######################################################
11/11/2015 8:37:15 AM    INF    LOGGING END : 11/11/2015 8:37:15 AM

Tuesday, March 1, 2016

Creating testing Deliverables using TestComplete 1 - Browser Images

Here is what I use to create deliverables after I finish an automated testing.

1) Having screenshots to submit for a web application.

I created this library routine Click_Picture that takes a picture of the entire browser screen(even parts that are not currently visible on the screen) and stores them at a desired location.

1) picNum starting from 1000 helps sort the pictures in the folder in the order it was taken. You can use/change/discard it as you desire.

2)  fldrPath_TestRun is a folder I create at the beginning of each test to save any files related to my current test run.

3) strPicName is a short descriptor for the picture that is being taken. A convention I use is 'B' means before and 'A' means after. e.g.
'A_Login' means after login
'B_SubmitForm' means before submitting the form.


 4) PagePicture is a method that TestComplete provides to capture a complete image of the tested object.

5) lib_FileCopy is any library function that can copy the picture from where TestComplete saves it to the desired folder.


DIM picNum: picNum = 1000

DIM fldrPath_TestRun

fldrPath_TestRun = "C:\TC11Projects\03012016_103216AM"

PUBLIC FUNCTION Save_PagePicture(ByVal strPicName)

  DIM src_Path, dest_Path
  picName = picNum & "_" & TRIM(strPicName)

  src_Path = Log.Picture(Sys.browser.Page("*").PagePicture, "Saving page picture")                                              
    dest_Path = fldrPath_TestRun & "\" & picName & ".png"                                               
    CALL lib_FileCopy(src_Path, dest_Path)
    Log.Message("SAVED PICTURE:")
    Log.Message(dest_Path)

END FUNCTION

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









Thursday, January 28, 2016

Automation Testing Framework

https://prezi.com/y1b2igrhyozt/fat/

Email me if you have any questions.