Disconnecting From Remote Desktop While Running Automated Tests

Applies to TestLeft 15.40, last modified on March 17, 2022

When you close a Remote Desktop session on a remote computer, it locks out the computer, displaying the logon screen. In the locked mode, the user session on the computer does not have GUI, and any GUI tests on that computer 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. All programs and tests on the remote computer will continue running normally.

You can automate the disconnection procedure using a batch file.

  • Connect to your remote computer.

  • On the remote computer, 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 TestLeft Tests via Remote Desktop
Running TestLeft Tests on Remote Computers

Highlight search results