Clipboard.GetText() not working with Agent?

Ranorex Spy, Recorder, and Studio.
semate
Posts: 13
Joined: Tue Jul 03, 2018 7:42 am

Clipboard.GetText() not working with Agent?

Post by semate » Mon Mar 04, 2019 5:57 pm

Hi there!

Is it me or is Clipboard.GetText() not working when the test application is executed remotely via Agent. Works locally.
Some more details: To get Putty output I use the Copy All To Clipboard Menu function. If I try to access it later with Clipboard.GetText() it comes up with an Error Message - but only if I run it via the Agent - locally it works just fine.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11865
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Clipboard.GetText() not working with Agent?

Post by Support Team » Mon Mar 04, 2019 9:55 pm

Hi semate,

I did a quick test and was not able to duplicate this behavior. The below code ran perfectly on both my local and agent machines. All runs pulled the different clipboard text for each machine properly. Is something else affecting the clipboard text on the remote machine? What is the error message you are receiving?

Code: Select all

Report.Info(Clipboard.GetText());
Cheers,
Ned
.
Image

User avatar
Stub
Posts: 229
Joined: Fri Jul 15, 2016 1:35 pm

Re: Clipboard.GetText() not working with Agent?

Post by Stub » Tue Mar 05, 2019 9:10 am

That happens to me all the time. We're still running Ranorex v8.2.1 (simply because I have not had the time to try to update things) and our tests run on a dedicated physical machine. Clipboard.GetText hurls exceptions so often that what I ended up doing was surrounding the whole clipboard operation in a 'try a few times' loop. If it fails the first time, it appears to succeed on the subsequent attempt.

We also have a lot of problems with Clipboard.SetText throwing, "Requested Clipboard operation did not succeed." This is more down to something locking the clipboard. I've started detecting this issue and reporting on who's locking the clipboard. In our case it's usually TightVNC which I use to connect to the remote machine running our Ranorex tests.

Code: Select all

        public static void HandleClipboardException (Exception exc)
        {
        	Process process_locking_clipboard = GetProcessLockingClipboard();
        	Report.Log(ReportLevel.Error, "Clipboard Error", exc.Message);
        	Report.Log(ReportLevel.Error, "Clipboard Error", $"The process apparently locking the clipboard is: {process_locking_clipboard.ProcessName} ({process_locking_clipboard.Id})");
        }
        
        [DllImport("user32.dll", SetLastError = true)]
	static extern IntPtr GetOpenClipboardWindow();

	[DllImport("user32.dll", SetLastError = true)]
	static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

	private static Process GetProcessLockingClipboard()
	{
    		int processId;
    		GetWindowThreadProcessId(GetOpenClipboardWindow(), out processId);

		return Process.GetProcessById(processId);
	}

ahoisl
Certified Professional
Certified Professional
Posts: 175
Joined: Fri Sep 07, 2007 8:16 am

Re: Clipboard.GetText() not working with Agent?

Post by ahoisl » Tue Mar 05, 2019 11:35 am

Just as a note: The Clipboard class is part of the default .NET Framework, i.e. it is not something Ranorex itself provides.

Anyway, interacting with the clipboard is prone to throwing exceptions, because it is a shared resource for the whole system. The .NET Framework itself already tries to get data from it in a loop; I suggest creating a utility method and doing the same thing again with e.g. 100ms wait time between tries.

Regards,
Alex
Ranorex Team