Disconnecting From Remote Desktop While Running Automated Tests

Applies to TestComplete 12.60, last modified on September 17, 2018
Information in this topic applies to desktop and web applications.

When using Remote Desktop to connect to a remote computer, closing Remote Desktop locks out the computer, displaying the logon screen. In the locked mode, the computer does not have GUI, so any currently running or scheduled GUI tests will fail.

To avoid problems with GUI tests, use the tscon utility to disconnect from Remote Desktop. tscon returns the control to the original local session on the remote computer, bypassing the logon screen. All programs on the remote computer continue running normally, including GUI tests.

To disconnect from Remote Desktop, run the following command on the remote computer (in the Remote Desktop window) as an Administrator:

%windir%\System32\tscon.exe RDP-Tcp#NNN /dest:console

where RDP-Tcp#NNN is the ID of your current Remote Desktop session, for example, RDP-Tcp#0. You can see it in the Windows Task Manager on the Users tab, in the Session column.

You will see the “Your remote desktop session has ended” message, and the Remote Desktop client will close. But all programs and tests on the remote computer will continue running normally.

You can automate the disconnection procedure using a batch file. On the remote computer, do the following:

  • Create a batch file with this code:

    for /f "skip=1 tokens=3" %%s in ('query user %USERNAME%') do (
      %windir%\System32\tscon.exe %%s /dest:console
    )

  • Create a desktop shortcut to this file. To do this, right-click the batch file and select Send to | Desktop (create shortcut).

  • In the shortcut properties, click Advanced and select Run as administrator.

Now, when you need to disconnect from Remote Desktop, double-click this shortcut on the remote computer (in the Remote Desktop window).

You can also call this batch file at the beginning of your tests (provided that the tests are run as an Administrator).

Important Notes
  • tscon leaves the remote computer unlocked, which can reduce the system security. You can lock the computer after the test run is over using the following command:

    Rundll32.exe user32.dll, LockWorkStation
    Do not lock the remote computer while your automated tests are still running. If the tests need to interact with the GUI, they will fail since the operating system will stop drawing the GUI. For more information about this, see Running Tests on Locked Computers.
  • If the rdpclip.exe process is running on the remote computer and the clipboard is not empty when you disconnect from the remote session, the rdpclip.exe process can fail.

    To avoid this issue, you can terminate the rdpclip.exe process before disconnecting from the session.

See Also

Running Tests via Remote Desktop
About Running Tests via Remote Desktop
Running Tests in Minimized Remote Desktop Windows
Running Tests on Locked Computers

Highlight search results