title: UI Automation - InvokePattern.Invoke() blocking execution until user interaction in integration tests
body: <h2>Edit: I went with the route of using <a href="https://github.com/FlaUI/FlaUI" rel="nofollow noreferrer">FlaUI</a>. I have updated the tag on this question to include it and also posted my solution as the answer.</h2>
  <p>I'm writing some integration tests for an app that monitors certain windows forms apps to see if those apps are running or crashed.</p>
  <p>One of the requirements is to identify if it's showing <em>error</em> window or <em>information</em> window and act accordingly.</p>
  <p>For eg:</p>
  <img src="https://i.stack.imgur.com/sWrar.png/400" width="450" />
  <p>To mock the case where an <em>information</em> window is showing, I created a test app (as shown above) with a button that would show some <em>information</em> window when clicked.</p>
  <p>This is how my test looks like:</p>
  <pre><code>[Fact]
  public async Task Can_Identify_An_Information_Window()
  {
      //ARRANGE
      // My service that will start the app:
      await _myUIAutomationService.StartAppAsync(&quot;PathToMyTestApp&quot;);
  
      //Find the app window
      var appProcess = Process.GetProcessesByName(&quot;MyTestApp'sProcessName&quot;)[0];
      var appWindow = AutomationElement.FromHandle(appProcess.MainWindowHandle);
  
      //Find the buttons on the app window:
      var buttons = appWindow.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));
      //Grab the button that will show the information window and click it to show the information window:
      foreach (AutomationElement button in buttons)
      {
          if (button.Current.Name == &quot;Open Info Window&quot;)
          {
              // click it to open the information window:
              var invoke = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);
              invoke.Invoke();
          }
      }
  
      //ACT
      // My service that will get the status of the app including the windows it's showing:
      var appstatus = await _myUIAutomationService.GetAppStatusAsync(&quot;MyTestApp'sName&quot;);
      var informationWindow = appstatus.InformationWindows[0];
  
      //ASSERT
      Assert.NotNull(informationWindow);
  }
  </code></pre>
  <p>My issue with this test is that I cannot get past the <code>invoke.Invoke();</code> line until I manually click &quot;OK&quot; on the information window that's showing.</p>
  <p>I even tried the route of attaching the event handler to the button to handle this, but that didn't work out as I could never get into that <code>OnInvoked</code> handler method:</p>
  <pre><code>Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent,
                                          button,
                                          TreeScope.Element,
                                          OnInvoked);
  
  // Handler for information window showing button invoke event:
  private void OnInvoked(object Sender, AutomationEventArgs E)
  {
      // The control never reaches here.
  }
  </code></pre>
  <p>I just need this test to be automatic without manual intervention.</p>
  <p>Any help/ direction would be greatly appreciated.</p>
  <p>Thanks!</p>