Disconnecting From Remote Desktop While Running Automated Tests

Applies to TestComplete 15.64, last modified on May 16, 2024
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 and displays the login 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.

Disconnect Manually

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

%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#5. You can see it in the Windows Task Manager on the Users tab, in the Session column.

Note: The Session column is hidden by default. To show it, right-click somewhere within the row that displays CPU, Memory, and so on, then choose Session in the opened context menu.
Creating Mask

Click the image to enlarge it.

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

Disconnect via a Batch File

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

  1. 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

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

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

  4. Double-click this shortcut on the remote computer (in the Remote Desktop Connection window) or 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