0

I am working on an automated testing framework in WPF. I am finding that while it is possible to automate most things using the basic WPF automation framework, it is very difficult to get down to the fine grained details of what is happening at the UI level. I need to be able to see things like the properties of the DataContext, properties of controls, and so on. I know this is possible because Snoop can do it. Snoop allows you to traverse the entire Visual Tree of any WPF app. I need this functionality. So, I wrote this code:

public async Visual GetAppRootVisual()
{
    var allProcesses = Process.GetProcesses();
    var filteredProcess = allProcesses.Where(p => p.ProcessName.Contains(ProcessSearchText)).First();
    var windowHandle = filteredProcess.MainWindowHandle;
    var hwndSource = HwndSource.FromHwnd(windowHandle);
    return hwndSource.RootVisual;
}

The code works up until the second last line. The second last line returns null, but I can't figure out why. The windowHandle is returned, but the HwndSource is not returned. What is going wrong here?

Christian Findlay
  • 6,770
  • 5
  • 51
  • 103
  • When do you call this code? Maybe before the handle is created. There is an event for that, sorry no Visual Studio around. – Maciek Świszczowski Oct 23 '18 at 08:09
  • The handle has definitely been created. I can get the handle. What I am seeing is that other apps like Snoop are actually injecting themselves in to the external app like malware would (but not in a malicous way). I'm wondering if I need to do the same thing... – Christian Findlay Oct 24 '18 at 01:23

0 Answers0