Wait for Process Methods in Popular Test Automation Suites

Due to an imperative need to wait for a process to complete during automated test runs, it was necessary to use a little creativity with a couple of the mainstream test automation suites.  AutoIT, a freeware tool, has this capability built-in... imagine that.  However, Compuware TestPartner and HP Mercury QuickTest Professional apparently don't see a need for this functionality and some extra work was required.  My application did not require extensive error-checking, so each of the samples below is a bare-bones implementation that should be easy for an adept developer to understand and build upon.

 

Compuware TestPartner

Place the following code in a module:
 
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Function KD_LaunchNWait(AppToLaunchNWait As String, ShowWindow As VbAppWinStyle)

    Dim TaskID As Long
    Dim ProcHandle As Long
    Const SYNCHRONIZE = &H100000
    Const STATUS_WAIT_0 As Long = &H0
    Const WAIT_OBJECT_0 As Long = (STATUS_WAIT_0 + 0)
    Const WAIT_INFINITE = -1&
    
    TaskID = Shell(AppToLaunchNWait, ShowWindow)
    If TaskID <> 0 Then
        ProcHandle = OpenProcess(SYNCHRONIZE, False, TaskID)
        If ProcHandle <> 0 Then
            RetVal = WaitForSingleObject(ProcHandle, WAIT_INFINITE)
            CloseHandle ProcHandle
        Else
            MsgBox "Bad ProcHandle", vbOKOnly, "Error"
        End If
    Else
        MsgBox "Bad TaskID", vbOKOnly, "Error"
    End If

End Function
To call a process and wait for it to finish before proceeding:
KD_LaunchNWait "{app to launch}.exe", vbHide

 

HP/Mercury QuickTest Professional

Create a DLL with this as the main export function:
 
__declspec(dllexport) int LaunchnWait(char* apptolaunch)
{
    // name of application to launch is in apptolaunch

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si,sizeof(si));
    si.cb=sizeof(si);
    ZeroMemory(&pi,sizeof(pi));

    if(strlen(apptolaunch)>1)
    {
        if(CreateProcess(NULL,apptolaunch,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
        {
            WaitForSingleObject(pi.hProcess,INFINITE);
            CloseHandle(pi.hProcess);
            CloseHandle(pi.hThread);
            return 1;
        }else return 0;
    }else return 0;
}
To call a process and wait for it to finish before proceeding:

1. Compile the DLL with the name launchnwait.dll or change the references to that name.
2. Declare the externall DLL function once before calling it...

Extern.Declare micInteger, "KDLaunchNWait","{pathto}\launchnwait.dll","LaunchnWait",micString


3. Call the DLL wait function...

Extern.KDLaunchNWait "{app to launch}.exe"

 

 

TestPartner® is a registered trademark of Compuware, Inc.
QuickTest® is a registered trademark of HP/Mercury Interactive Ltd.