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

ExpandCollapsePattern

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

ExpandCollapsePattern有两个主要方法:

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

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

      以下代码是用 ExpandCollapsePattern来测试ComboBox控件的Expand和Collapse。
ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Automation;

namespace UIATest
ExpandedBlockStart.gifContractedBlock.gif
{
    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Process process 
= Process.Start(@"F:\CSharpDotNet\AutomationTest\ATP\WpfApp\bin\Debug\WpfApp.exe");
            
int processId = process.Id;
            AutomationElement element 
= FindElementById(processId, "comboBox1");
            ExpandCollapsePattern currentPattern 
= GetExpandCollapsePattern(element);
            currentPattern.Expand();
            Thread.Sleep(
1000);
            currentPattern.Collapse();
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Get the automation elemention of current form.
        
/// </summary>
        
/// <param name="processId">Process Id</param>
        
/// <returns>Target element</returns>

        public static AutomationElement FindWindowByProcessId(int processId)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            AutomationElement targetWindow 
= null;
            
int count = 0;
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Process p 
= Process.GetProcessById(processId);
                targetWindow 
= AutomationElement.FromHandle(p.MainWindowHandle);
                
return targetWindow;
            }

            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                count
++;
                StringBuilder sb 
= new StringBuilder();
                
string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
                
if (count > 5)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
throw new InvalidProgramException(message, ex);
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return FindWindowByProcessId(processId);
                }

            }

        }



ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Get the automation element by automation Id.
        
/// </summary>
        
/// <param name="windowName">Window name</param>
        
/// <param name="automationId">Control automation Id</param>
        
/// <returns>Automatin element searched by automation Id</returns>

        public static AutomationElement FindElementById(int processId, string automationId)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            AutomationElement aeForm 
= FindWindowByProcessId(processId);
            AutomationElement tarFindElement 
= aeForm.FindFirst(TreeScope.Descendants,
            
new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
            
return tarFindElement;
        }


ContractedSubBlock.gifExpandedSubBlockStart.gif        
ExpandCollapsePattern helper#region ExpandCollapsePattern helper

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Get ExpandCollapsePattern
        
/// </summary>
        
/// <param name="element">AutomationElement instance</param>
        
/// <returns>ExpandCollapsePattern instance</returns>

        public static ExpandCollapsePattern GetExpandCollapsePattern(AutomationElement element)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
object currentPattern;
            
if (!element.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out currentPattern))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the ExpandCollapsePattern.",
                    element.Current.AutomationId, element.Current.Name));
            }

            
return currentPattern as ExpandCollapsePattern;
        }


        
#endregion

    }

}

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

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


转载于:https://www.cnblogs.com/kangyi/archive/2009/09/13/1565774.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值