FlaUI icon indicating copy to clipboard operation
FlaUI copied to clipboard

desktop.FindAllChildren() sometimes just hangs when debugging the test.

Open tetsushmz opened this issue 7 years ago • 1 comments

Hi,

I'm using UIA3 and testing a WPF app on 64 bit Windows 10. I call the function in the subject line lots of times to find a new top level window because the app is a multi-window type like MS OutLook opening multiple new email windows.

The problem I'm seeing is that the tests run fine if I start "Run Tests" in Visual Studio's Test explorer (even though it sometimes takes up to 30 seconds for the method to return). But when I start "Debug Tests", the method sometimes does not seem to return at all - i.e., just hangs. It is an intermittent issue (several to two dozen calls prior to that succeed).

I looked at the source code. It may have something to do with the CacheRequest. I'm not sure.

I'm using Visual Studio 2019 with FlaUI.UIA3 2.0.0-rc1.

Any suggestions would be appreciated.

Thanks.

Tetsu

tetsushmz avatar Apr 19 '19 12:04 tetsushmz

I too encountered this with FindAllChildren() and FindFirstChild() using FlaUI 1.3.1 on a .NET Framework 4.7 project.

I resorted to Win32 window enumeration to meet my needs.

using FlaUI.Core.AutomationElements;
using FlaUI.UIA3;
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Stuff
{
  public static class Win32WindowUtils
  {
    public static Window GetTopLevelWindow(UIA3Automation automation, 
      int desiredProcessId
      string desiredTitle = null)
    {
      const bool nextWindowPlz = true;
      const bool okDoneScanning = false;
      Window result = null;
      EnumDelegate filter = delegate (IntPtr hWnd, int lParam)
      {
        if (!IsWindowVisible(hWnd)) return nextWindowPlz
        uint foundThreadId = GetWindowThreadProcessId(hWnd, out uint foundProcessId);
        if (!(foundThreadId != 0 && foundProcessId != 0)) return nextWindowPlz;
        if ((int)foundProcessId != desiredProcessId) return nextWindowPlz;
        StringBuilder buffer = new StringBuilder(255);
        int nLength = GetWindowText(hWnd, buffer, buffer.Capacity + 1);
        if (nLength == 0) return nextWindowPlz;
        string foundTitle = buffer.ToString();
        if (foundTitle != desiredTitle) return nextWindowPlz;
        try
        {
          var nativeAutomationElement = automation.NativeAutomation.ElementFromHandle(hWnd);
          if (nativeAutomationElement == null) return nextWindowPlz;
          var automationElement = automation.WrapNativeElement(nativeAutomationElement);
          result = automationElement.AsWindow();
          return okDoneScanning;
        }
        catch
        {
          // boo UIAutomation, don't be like that
        }

        return nextWindowPlz;
      };

      EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero);

      return result;
    }

    private delegate bool EnumDelegate(IntPtr hWnd, int lParam);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool IsWindowVisible(IntPtr hWnd);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

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

nathan-schubkegel avatar Aug 23 '19 17:08 nathan-schubkegel