ExpandCollapsePattern

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

ExpandCollapsePattern有两个主要方法:

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

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

      以下代码是用ExpandCollapsePattern来测试ComboBox控件的Expand和Collapse。
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_02Code
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03using System;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03
using System.Text;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03
using System.Diagnostics;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03
using System.Threading;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03
using System.Windows.Automation;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03
namespace UIATest
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_xml_10使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_11
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_12{
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13    
class Program
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15    
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_12{
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        
static void Main(string[] args)
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15        
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_12{
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            Process process 
= Process.Start(@"F:\CSharpDotNet\AutomationTest\ATP\WpfApp\bin\Debug\WpfApp.exe");
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            
int processId = process.Id;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            AutomationElement element 
= FindElementById(processId, "comboBox1");
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            ExpandCollapsePattern currentPattern 
= GetExpandCollapsePattern(element);
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            currentPattern.Expand();
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            Thread.Sleep(
1000);
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            currentPattern.Collapse();
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28        }

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15        
/**//// <summary>
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        
/// Get the automation elemention of current form.
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        
/// </summary>
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        
/// <param name="processId">Process Id</param>
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28        
/// <returns>Target element</returns>

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        public static AutomationElement FindWindowByProcessId(int processId)
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15        
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_12{
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            AutomationElement targetWindow 
= null;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            
int count = 0;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            
try
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15            
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_12{
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13                Process p 
= Process.GetProcessById(processId);
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13                targetWindow 
= AutomationElement.FromHandle(p.MainWindowHandle);
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13                
return targetWindow;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28            }

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            
catch (Exception ex)
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15            
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_12{
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13                count
++;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13                StringBuilder sb 
= new StringBuilder();
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13                
string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13                
if (count > 5)
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15                
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_12{
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13                    
throw new InvalidProgramException(message, ex);
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28                }

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13                
else
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15                
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_12{
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13                    
return FindWindowByProcessId(processId);
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28                }

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28            }

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28        }

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15        
/**//// <summary>
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        
/// Get the automation element by automation Id.
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        
/// </summary>
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        
/// <param name="windowName">Window name</param>
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        
/// <param name="automationId">Control automation Id</param>
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28        
/// <returns>Automatin element searched by automation Id</returns>

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        public static AutomationElement FindElementById(int processId, string automationId)
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15        
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_12{
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            AutomationElement aeForm 
= FindWindowByProcessId(processId);
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            AutomationElement tarFindElement 
= aeForm.FindFirst(TreeScope.Descendants,
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            
new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            
return tarFindElement;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28        }

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14        
ExpandCollapsePattern helper#region ExpandCollapsePattern helper
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15        
/**//// <summary>
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        
/// Get ExpandCollapsePattern
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        
/// </summary>
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        
/// <param name="element">AutomationElement instance</param>
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28        
/// <returns>ExpandCollapsePattern instance</returns>

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13        public static ExpandCollapsePattern GetExpandCollapsePattern(AutomationElement element)
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15        
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_12{
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            
object currentPattern;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            
if (!element.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out currentPattern))
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_14使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_子节点_15            
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_12{
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13                
throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the ExpandCollapsePattern.",
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13                    element.Current.AutomationId, element.Current.Name));
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28            }

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13            
return currentPattern as ExpandCollapsePattern;
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28        }

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_microsoft_13
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28        
#endregion

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation_28    }

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_116}

使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03
      以下代码为被测程序的xaml文件:
使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_UI Automation使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_02Code
 1使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03<Window x:Class="WpfApp.Window1"
 2使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03    Title="Window1" Height="219" Width="353">
 5使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03    <Grid>
 6使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03        <ComboBox Name="comboBox1" Height="23" VerticalAlignment="Top" Margin="94,58,0,0" HorizontalAlignment="Left" Width="119">
 7使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03            <ComboBoxItem>kaden</ComboBoxItem>
 8使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03            <ComboBoxItem>sam</ComboBoxItem>
 9使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03        </ComboBox>
10使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03    </Grid>
11使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03</Window>
12使用UI Automation实现自动化测试--4.2 (ExpandCollapsePattern)_控件_03
Summary

本文主要是对ExpandCollapsePattern 做简单的介绍,并使用ExpandCollapsePattern来操作ComboBox控件,对ComboBox进行ExpandCollapse操作。