使用UI Automation实现自动化测试--4.5 (WindowPattern)

    

WindowPattern

  
      WindowPattern 控件模式用于支持在传统的 图形用户界面 (GUI) 内提供基于基本窗口的功能的控件。必须实现此控件模式的控件的示例包括顶级应用程序窗口、多文档界面 (MDI) 子窗口、大小可调的拆分窗格控件、模式对话框以及气球状帮助窗口。可以使用WindowPattern来对window进行操作,例如验证window是否激活,是否最大化、最小化、正常模式以及关闭window等。
     下面的代码演示了WindowPattern的使用方法:

ContractedBlock.gif ExpandedBlockStart.gif Code
 1using System;
 2using System.Text;
 3using System.Diagnostics;
 4using System.Threading;
 5using System.Windows.Automation;
 6
 7namespace UIATest
 8ExpandedBlockStart.gifContractedBlock.gif{
 9    class Program
10ExpandedSubBlockStart.gifContractedSubBlock.gif    {
11        static void Main(string[] args)
12ExpandedSubBlockStart.gifContractedSubBlock.gif        {
13            Process process = Process.Start(@"F:\CSharpDotNet\AutomationTest\ATP\WpfApp\bin\Debug\WpfApp.exe");
14            int processId = process.Id;
15            AutomationElement element = FindWindowByProcessId(processId);
16            WindowPattern currentPattern = GetWindowPattern(element);
17            
18            //Set window visual state to Maximized
19            currentPattern.SetWindowVisualState(WindowVisualState.Maximized);
20            Thread.Sleep(1000);
21
22            //Set window visual state to Normal
23            currentPattern.SetWindowVisualState(WindowVisualState.Normal);
24            Thread.Sleep(1000);
25
26            //Set window visual state to Minimized
27            currentPattern.SetWindowVisualState(WindowVisualState.Minimized);
28            
29            //Close window
30            currentPattern.Close();            
31        }

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

38        public static AutomationElement FindWindowByProcessId(int processId)
39ExpandedSubBlockStart.gifContractedSubBlock.gif        {
40            AutomationElement targetWindow = null;
41            int count = 0;
42            try
43ExpandedSubBlockStart.gifContractedSubBlock.gif            {
44                Process p = Process.GetProcessById(processId);
45                targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
46                return targetWindow;
47            }

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

57                else
58ExpandedSubBlockStart.gifContractedSubBlock.gif                {
59                    return FindWindowByProcessId(processId);
60                }

61            }

62        }

63
64ContractedSubBlock.gifExpandedSubBlockStart.gif        WindowPattern helper#region WindowPattern helper
65
66ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
67        /// Get WindowPattern
68        /// </summary>
69        /// <param name="element">AutomationElement instance</param>
70        /// <returns>WindowPattern instance</returns>

71        public static WindowPattern GetWindowPattern(AutomationElement element)
72ExpandedSubBlockStart.gifContractedSubBlock.gif        {
73            object currentPattern;
74            if (!element.TryGetCurrentPattern(WindowPattern.Pattern, out currentPattern))
75ExpandedSubBlockStart.gifContractedSubBlock.gif            {
76                throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the WindowPattern.",
77                    element.Current.AutomationId, element.Current.Name));
78            }

79            return currentPattern as WindowPattern;
80        }

81
82        #endregion

83    }

84}

85

本文简单介绍了

WindowPattern 以及 WindowPattern 在测试中的使用方法。

转载于:https://www.cnblogs.com/kangyi/archive/2009/09/16/1568147.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值