提交 abe58e48 编写于 作者: T Tom Meschter

Add a helper method for getting a dialog

Right now a couple of methods use `DialogHelpers.FindDialog` to try to locate an open dialog, and then throw an `InvalidOperationException` if they don't find it. This is going to be a pretty common pattern when working with dialogs and controls within dialogs, so I've refactored it into a new `DialogHelpers.GetOpenDialog` method.

I've also updated the `Editor_InProc.PressDialogButton` method to make use of the `AutomationElement` extension methods.
上级 f559eab6
using System;
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
......@@ -11,6 +13,23 @@ namespace Microsoft.VisualStudio.IntegrationTest.Utilities.InProcess
{
public static class DialogHelpers
{
/// <summary>
/// Returns an <see cref="AutomationElement"/> representing the open dialog with automation ID
/// <paramref name="dialogAutomationName"/>.
/// Throws an <see cref="InvalidOperationException"/> if an open dialog with that name cannot be
/// found.
/// </summary>
public static AutomationElement GetOpenDialog(int visualStudioHWnd, string dialogAutomationName)
{
var dialogAutomationElement = FindDialog(visualStudioHWnd, dialogAutomationName, isOpen: true);
if (dialogAutomationElement == null)
{
throw new InvalidOperationException($"Expected the {dialogAutomationName} dialog to be open, but it is not.");
}
return dialogAutomationElement;
}
public static AutomationElement FindDialog(int visualStudioHWnd, string dialogAutomationName, bool isOpen)
{
return Retry(
......
......@@ -122,6 +122,8 @@ public string GetLineTextBeforeCaret()
return text.Substring(0, bufferPosition.Position - line.Start);
});
public string GetLineTextAfterCaret()
=> ExecuteOnActiveView(view =>
{
......@@ -420,11 +422,7 @@ public void VerifyDialog(string dialogAutomationId, bool isOpen)
public void DialogSendKeys(string dialogAutomationName, string keys)
{
var dialogAutomationElement = DialogHelpers.FindDialog(GetDTE().MainWindow.HWnd, dialogAutomationName, isOpen: true);
if (dialogAutomationElement == null)
{
throw new InvalidOperationException($"Expected the {dialogAutomationName} dialog to be open, but it is not.");
}
var dialogAutomationElement = DialogHelpers.GetOpenDialog(GetDTE().MainWindow.HWnd, dialogAutomationName);
dialogAutomationElement.SetFocus();
SendKeys.SendWait(keys);
......@@ -432,29 +430,10 @@ public void DialogSendKeys(string dialogAutomationName, string keys)
public void PressDialogButton(string dialogAutomationName, string buttonAutomationName)
{
var dialogAutomationElement = DialogHelpers.FindDialog(GetDTE().MainWindow.HWnd, dialogAutomationName, isOpen: true);
if (dialogAutomationElement == null)
{
throw new InvalidOperationException($"Expected the {dialogAutomationName} dialog to be open, but it is not.");
}
Condition condition = new PropertyCondition(AutomationElement.AutomationIdProperty, buttonAutomationName);
var buttonAutomationElement = dialogAutomationElement.FindFirst(TreeScope.Descendants, condition);
var dialogAutomationElement = DialogHelpers.GetOpenDialog(GetDTE().MainWindow.HWnd, dialogAutomationName);
if (buttonAutomationElement == null)
{
throw new InvalidOperationException($"Could not find the {buttonAutomationName} button in the {dialogAutomationName} dialog");
}
if (buttonAutomationElement.TryGetCurrentPattern(InvokePattern.Pattern, out var invokePattern))
{
(invokePattern as InvokePattern).Invoke();
}
else
{
throw new InvalidOperationException($"The element {buttonAutomationName} does not have an InvokePattern. Please make sure that it is the correct control");
}
var buttonAutomationElement = dialogAutomationElement.FindDescendantByAutomationId(buttonAutomationName);
buttonAutomationElement.Invoke();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册