【问题标题】:UI automation for office 2013Office 2013 的 UI 自动化
【发布时间】:2015-08-25 10:31:32
【问题描述】:

我需要在 Office 2013 中自动按文件->信息->“检查问题”。 我设法用代码按下文件按钮:

AutomationElement window = AutomationElement.FromHandle(Window.Handle);
AutomationElementCollection buttons = window.FindAll(TreeScope.Descendants, new PropertyCondition(
AutomationElement.ControlTypeProperty, ControlType.Button));
AutomationElement file=buttons.Cast<AutomationElement>().FirstOrDefault(x => x.Current.Name == "File Tab");
InvokePattern ipClickLoadSettings = (InvokePattern)file.GetCurrentPattern(InvokePattern.Pattern);
ipClickLoadSettings.Invoke();

如何按“检查问题”按钮或信息窗口中的任何其他按钮?

谢谢

【问题讨论】:

    标签: c# office-2013


    【解决方案1】:

    我使用 Inspect SDK 工具查看了 Word 2013 UI,这表明一些相关的 UI 可以通过 UIA Invoke 模式以编程方式调用,但有些则不能。相反,需要选择或扩展其他 UI。所以我只是写了下面的测试代码来做下面的……

    1. 调用文件选项卡。
    2. 选择信息项。
    3. 扩展检查问题 UI。
    4. 调用检查辅助功能按钮。

    虽然代码做了一些假设(并且仅适用于 Word 的英文版本),但它似乎能够调用 Check Accessibility UI ok。

    在我的测试中,我使用了 Windows 附带的非托管 UIA API,而不是托管的 .NET UIA API。我使用使用 tlbimp.exe 工具生成的包装器从 C# 代码调用 Windows UIA API。

    这就是我为生成包装器所做的...

    "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\x64\tlbimp.exe" c:\windows\System32\UIAutomationCore.dll /out:Interop.UIAutomationCore .dll

    如果以下步骤对您不起作用,请告诉我,我可以调查一下。

    谢谢,

    男人

    IUIAutomation uiAutomation = new CUIAutomation8();
    
    IUIAutomationElement rootElement = uiAutomation.GetRootElement();
    
    // Assume the first child of the root element with a ClassName of 
    // "OpusApp" is the Word window we're interested in.
    int propertyIdClassName = 30012; // UIA_ClassNamePropertyId
    
    IUIAutomationCondition conditionWordApp =
        uiAutomation.CreatePropertyCondition(
            propertyIdClassName, "OpusApp");
    
    IUIAutomationElement wordElement =
        rootElement.FindFirst(
            TreeScope.TreeScope_Children,
            conditionWordApp);
    
    // Find the File Tab beneath the Word element. Use the AutomationId 
    // to find the button rather than the Name, because AutomationId will 
    // not be localized.
    int propertyAutomationId = 30011; // UIA_AutomationIdPropertyId
    
    IUIAutomationCondition conditionFileTab =
        uiAutomation.CreatePropertyCondition(
            propertyAutomationId,
            "FileTabButton");
    
    // Cache the Invoke pattern when we get the FileTab element, so 
    // that we don't have to make another cross-process call later to 
    // get the pattern.
    int patternIdInvoke = 10000; // UIA_InvokePatternId
    IUIAutomationCacheRequest cacheRequestInvokePattern = 
        uiAutomation.CreateCacheRequest();
    cacheRequestInvokePattern.AddPattern(patternIdInvoke);
    
    IUIAutomationElement fileTabElement =
        wordElement.FindFirstBuildCache(
            TreeScope.TreeScope_Descendants,
            conditionFileTab,
            cacheRequestInvokePattern);
    
    // Now invoke the tab.
    IUIAutomationInvokePattern invokePatternFileTab = 
        fileTabElement.GetCachedPattern(patternIdInvoke);
    invokePatternFileTab.Invoke();
    
    // Note that sometimes when making calls like this, it may be necessary to 
    // Thread.Sleep() for a short time here, to give the target app a chance to 
    // create and show the UI being invoked.
    
    // Find the Info item. Unfortunately the item has no AutomationId, 
    // so use other properties to find it. For this test, just use the 
    // localizable Name and ControlType. (So this means this code won't 
    // work for non-English builds of Word.)
    
    int propertyIdName = 30005; // UIA_NamePropertyId
    
    IUIAutomationCondition conditionInfoItemName =
        uiAutomation.CreatePropertyCondition(
            propertyIdName, "Info");
    
    IUIAutomationCondition conditionInfoItemClassName =
        uiAutomation.CreatePropertyCondition(
            propertyIdClassName, "NetUIRibbonTab");
    
    IUIAutomationCondition conditionInfoItem = uiAutomation.CreateAndCondition(
        conditionInfoItemName, conditionInfoItemClassName);
    
    int patternIdSelectionItem = 10010; // UIA_SelectionItemPatternId
    
    IUIAutomationCacheRequest cacheRequestSelectionItemPattern = 
        uiAutomation.CreateCacheRequest();
    cacheRequestSelectionItemPattern.AddPattern(patternIdSelectionItem);
    
    IUIAutomationElement infoItemElement =
        wordElement.FindFirstBuildCache(
            TreeScope.TreeScope_Descendants,
            conditionInfoItem,
            cacheRequestSelectionItemPattern);
    
    // Now select the Info item, to show the "Check for issues" UI.
    IUIAutomationSelectionItemPattern selectionItemPatternInfoItem = 
        infoItemElement.GetCachedPattern(patternIdSelectionItem);
    selectionItemPatternInfoItem.Select();
    
    // Now find the "Check for issues" element. This element also has no
    // AutomationId, so just search for the Name and ClassName again.
    IUIAutomationCondition conditionInfoCheckForIssuesName =
        uiAutomation.CreatePropertyCondition(
            propertyIdName, "Check for Issues");
    
    IUIAutomationCondition conditionCheckForIssuesClassName =
        uiAutomation.CreatePropertyCondition(
            propertyIdClassName, "NetUIAnchor");
    
    IUIAutomationCondition conditionCheckForIssues = 
        uiAutomation.CreateAndCondition(
            conditionInfoCheckForIssuesName, conditionCheckForIssuesClassName);
    
    int patternIdExpandCollapse = 10005; // UIA_ExpandCollapsePatternId
    
    // Expand the "Check for issues" UI, to show the "Check Accessibility" 
    // button.
    IUIAutomationCacheRequest cacheRequestExpandCollapsePattern = 
        uiAutomation.CreateCacheRequest();
    cacheRequestExpandCollapsePattern.AddPattern(patternIdExpandCollapse);
    
    IUIAutomationElement checkForIssuesElement =
        wordElement.FindFirstBuildCache(
            TreeScope.TreeScope_Descendants,
            conditionCheckForIssues,
            cacheRequestExpandCollapsePattern);
    
    IUIAutomationExpandCollapsePattern expandCollapsePatternCheckForIssues = 
        checkForIssuesElement.GetCachedPattern(patternIdExpandCollapse);
    expandCollapsePatternCheckForIssues.Expand();
    
    // Finally find the "Check Accessibility" element. This element also has no
    // AutomationId, so once again, just search for the Name and ClassName.
    IUIAutomationCondition conditionInfoCheckAccessibilityName =
        uiAutomation.CreatePropertyCondition(
            propertyIdName, "Check Accessibility");
    
    IUIAutomationCondition conditionCheckAccessibiltyClassName =
        uiAutomation.CreatePropertyCondition(
            propertyIdClassName, "NetUITWBtnMenuItem");
    
    IUIAutomationCondition conditionCheckAccessibility = 
        uiAutomation.CreateAndCondition(
            conditionInfoCheckAccessibilityName, 
            conditionCheckAccessibiltyClassName);
    
    IUIAutomationElement checkAccessibilityElement =
        wordElement.FindFirstBuildCache(
            TreeScope.TreeScope_Descendants,
            conditionCheckAccessibility,
            cacheRequestInvokePattern);
    
    // Invoke this element to check the document's accessibility.
    IUIAutomationInvokePattern invokePatternCheckAccessibility =
        checkAccessibilityElement.GetCachedPattern(patternIdInvoke);
    invokePatternCheckAccessibility.Invoke();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-06
      • 2016-12-27
      相关资源
      最近更新 更多