12 October 2013

How to detect and pause QTP test execution on a Virtual Machine (VM) if remote disconnects?

If you are running an execution for longer periods over a Virtual Machine (VM) using QTP, you might need to handle the remote disconnections if any, which would otherwise result in test-case failures. Here is a simple code that pauses the execution if you disconnect the remote. This code would keep the execution paused unless you resume it again after taking the remote of the VM. 

1. Commandline Utility 'qwinsta.exe' - this is a small utility that queries and resets remote session.

By default, you will find this utility on Windows 2000+ which includes Windows 2003, Windows XP, Win 7 and Win 8. As an example on a Win 8 machine, the location of this file is the following:


If you fire the command 'qwinsta' from a non-remote console, this is the output that you get:

If you fire the command 'qwinsta' from a remote console, this is the output that you get:

Now, by noting down the difference in the outputs of the 'qwinsta' command when a machine is in remote and disconnected states respectively, we can write a small Vb-script code which is executed by QTP at the start of each test-case or step as per your wish, the execution can be paused. Also, remember that the output might change as per the OS installed. The screenshots above are from a Windows 8 machine.

Also, in-case you get a MS-DOS error saying the command is not valid then copy the file "qwinsta.exe" to another location and directly call that command in the script with the path. In code below, I have copied the utility in C:\ and called it.

    Set sh = createobject("wscript.shell")
    For i = 1 to 1
        Wait 1
        set execv = sh.Exec("cmd.exe /c C:\qwinsta" )
        While  execv.Status<>0
        out =  execv.StdOut.ReadAll
        If Instr(out, "rdp-tcp#") < 1 Then
            sh.Popup "Wait 120 seconds for the remote to reconnect", 23, "Warning - Remote Disconnected"
            set execv = sh.Exec("cmd.exe /c C:\qwinsta" )
            out =  execv.StdOut.ReadAll
            If Instr(out, "rdp-tcp#") < 1 Then
                        Msgbox "Execution paused, remote disconnected. Please click OK to continue."
                    End If
            End If

No comments:

Post a Comment