使用 UI Automation 实现自动化测试--4.2

ExpandCollapsePattern

表示以可视方式进行展开(以显示内容)和折叠(以隐藏内容)的 控件。例如ComboBox控件支持ExpandCollapsePattern。

ExpandCollapsePattern有两个主要方法:

Expand()方法:隐藏 AutomationElement 的全部子代节点、控件或内容。

Collapse()方法:显示 AutomationElement 的全部子节点、控件或内容。

      以下代码是用ExpandCollapsePattern来测试ComboBox控件的Expand和Collapse。
  1. using System;
  2. using System.Text;
  3. using System.Diagnostics;
  4. using System.Threading;
  5. using System.Windows.Automation;

  6. namespace UIATest
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Process process = Process.Start(@"F:\CSharpDotNet\AutomationTest\ATP\WpfApp\bin\Debug\WpfApp.exe");
  13.             int processId = process.Id;
  14.             AutomationElement element = FindElementById(processId, "comboBox1");
  15.             ExpandCollapsePattern currentPattern = GetExpandCollapsePattern(element);
  16.             currentPattern.Expand();
  17.             Thread.Sleep(1000);
  18.             currentPattern.Collapse();
  19.         }

  20.         /** <summary>
  21.         /// Get the automation elemention of current form.
  22.         /// </summary>
  23.         /// <param name="processId">Process Id</param>
  24.         /// <returns>Target element</returns>
  25.         public static AutomationElement FindWindowByProcessId(int processId)
  26.         {
  27.             AutomationElement targetWindow = null;
  28.             int count = 0;
  29.             try
  30.             {
  31.                 Process p = Process.GetProcessById(processId);
  32.                 targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
  33.                 return targetWindow;
  34.             }
  35.             catch (Exception ex)
  36.             {
  37.                 count++;
  38.                 StringBuilder sb = new StringBuilder();
  39.                 string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
  40.                 if (count > 5)
  41.                 {
  42.                     throw new InvalidProgramException(message, ex);
  43.                 }
  44.                 else
  45.                 {
  46.                     return FindWindowByProcessId(processId);
  47.                 }
  48.             }
  49.         }


  50.         /** <summary>
  51.         /// Get the automation element by automation Id.
  52.         /// </summary>
  53.         /// <param name="windowName">Window name</param>
  54.         /// <param name="automationId">Control automation Id</param>
  55.         /// <returns>Automatin element searched by automation Id</returns>
  56.         public static AutomationElement FindElementById(int processId, string automationId)
  57.         {
  58.             AutomationElement aeForm = FindWindowByProcessId(processId);
  59.             AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
  60.             new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
  61.             return tarFindElement;
  62.         }

  63.         ExpandCollapsePattern helper#region ExpandCollapsePattern helper

  64.         /** <summary>
  65.         /// Get ExpandCollapsePattern
  66.         /// </summary>
  67.         /// <param name="element">AutomationElement instance</param>
  68.         /// <returns>ExpandCollapsePattern instance</returns>
  69.         public static ExpandCollapsePattern GetExpandCollapsePattern(AutomationElement element)
  70.         {
  71.             object currentPattern;
  72.             if (!element.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out currentPattern))
  73.             {
  74.                 throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the ExpandCollapsePattern.",
  75.                     element.Current.AutomationId, element.Current.Name));
  76.             }
  77.             return currentPattern as ExpandCollapsePattern;
  78.         }

  79.         #endregion
  80.     }
  81. }
复制代码
以下代码为被测程序的xaml文件:
  1. 1<Window x:Class="WpfApp.Window1"
  2. 2    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. 3    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. 4    Title="Window1" Height="219" Width="353">
  5. 5    <Grid>
  6. 6        <ComboBox Name="comboBox1" Height="23" VerticalAlignment="Top" Margin="94,58,0,0" HorizontalAlignment="Left" Width="119">
  7. 7            <ComboBoxItem>kaden</ComboBoxItem>
  8. 8            <ComboBoxItem>sam</ComboBoxItem>
  9. 9        </ComboBox>
  10. 10    </Grid>
  11. 11</Window>
  12. 12
复制代码
Summary

本文主要是对ExpandCollapsePattern 做简单的介绍,并使用ExpandCollapsePattern来操作ComboBox控件,对ComboBox进行Expand和Collapse操作。( 文/ 开着拖拉机
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据UIAutomation封装了很多自定义方法 现在只需要实例化之后 直接调用方法即可完成。比如单击某个按钮,现在只需要直接调用ClickElement,非常实用。 ClickElement 单击指定的自动化元素 DisselectAllDataGridRow 不选中所有行 DisselectDataGridRow 不选中特定的某一行 FocusWindow 获取窗口焦点 GetAllElement 获取指定父自动化元素下的所有激活的控件 GetAllElementDetails 获取指定自动化元素下的详细信息包括AutomationID,ControlType以及Name GetAllMenus 获取所有菜单项 GetAllSubMenus 获取某个菜单下的所有子菜单项 GetColumnsFromGridLine 获取指定行的所有列 GetColumnValuesFromGridLine 获取行的每一列数据 GetControlType(AutomationElement) 获取制动自动化元素的控件类型 GetControlType(TypeOfControl) 获取UIAutomation的控件类型 GetDocumentText 获取document控件的值 GetElementByID 获取父自动化元素下指定元素控件ID的引用 GetElementByName 获取父自动化元素下的指定子元素的引用 GetElementsByControlType 获取父自动化元素下的特定类型的所有自动化元素 GetGridLinesFromDataGrid 获取网格控件的全部行元素的引用 GetHeaderFromDataGrid 获取指定网格控件的标题栏引用 GetMenuBar 获取菜单栏控件 GetMenuByName 通过特定的名称去获取菜单UI自动化元素 GetName 获取指定自动化元素的名称 GetSubMenuByName 获取主菜单下的指定子菜单项的引用 GetValue 获取指定自动化元素的值 GetWindowByName(String) 获取desktop下的指定窗口名称的子UI自动化元素 GetWindowByName(String, AutomationElement) 获取特定父UI自动化元素下的制定窗口名称的子UI自动化元素 GetWindowList() 获取当前桌面根下所有的UI自动化元素下 GetWindowList(AutomationElement) 获取特定父UI自动化元素下的所有窗口的名称 RefindMainApplication 重新获取desktop下的指定窗口的自动化元素引用 SelectAllDataGridRow 选中所有行 SelectDataGridRow(AutomationElement) 选中特定的某一行 SelectDataGridRow(AutomationElement, Boolean) 将特定的DateGridRow加入选中项中 SelectValueInComboBox 从下拉框中选中指定值的项 SelectValueInListBox 从列表中选中指定值的项 SetValue 给予指定自动化元素赋值 以上的方法还不是很完善 正在完善中。如果有什么意见和建议,请发送邮件获取 chenxu7601257@qq.com 如果你看了这个帮助文件之后觉得有用的,请发邮件获取,我将把dll文件给你。谢谢。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值