public static async Task EnsureFocusAsync(UIElement element, FocusState focusState, UInt32 Attempts = 3) { bool gotFocus = false; while (Attempts > 0 && !gotFocus) { Attempts--; //TestServicesExtensions.EnsureForegroundWindow(); using (var eventTester = new EventTester <UIElement, RoutedEventArgs>(element, "GotFocus")) using (var eventTesterFocusMgr = EventTester <object, FocusManagerGotFocusEventArgs> .FromStaticEvent <FocusManager>("GotFocus")) { UIExecutor.Execute(() => { if (element.FocusState == focusState && FocusManager.GetFocusedElement(element.XamlRoot).Equals(element)) { // The element is already focused with the expected focus state Log.Comment("Focus was already set on this element"); gotFocus = true; } else { Log.Comment("Setting focus to the element..."); element.Focus(focusState); } }); await Task.Delay(100); //eventTester.WaitForNoThrow(TimeSpan.FromMilliseconds(4000)); //eventTesterFocusMgr.WaitForNoThrow(TimeSpan.FromMilliseconds(4000)); gotFocus = gotFocus == false ? eventTester.HasFired : true; } await TestServices.WindowHelper.WaitForIdle(); } //if (!gotFocus) //{ // TestServices.Utilities.CaptureScreen("FocusTestHelper"); //} Verify.IsTrue(gotFocus); }