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

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

    下面的代码演示了WindowPattern的使用方法:
  1. 1using System;
  2. 2using System.Text;
  3. 3using System.Diagnostics;
  4. 4using System.Threading;
  5. 5using System.Windows.Automation;
  6. 6
  7. 7namespace UIATest
  8. 8{
  9. 9    class Program
  10. 10    {
  11. 11        static void Main(string[] args)
  12. 12        {
  13. 13            Process process = Process.Start(@"F:\CSharpDotNet\AutomationTest\ATP\WpfApp\bin\Debug\WpfApp.exe");
  14. 14            int processId = process.Id;
  15. 15            AutomationElement element = FindWindowByProcessId(processId);
  16. 16            WindowPattern currentPattern = GetWindowPattern(element);
  17. 17           
  18. 18            //Set window visual state to Maximized
  19. 19            currentPattern.SetWindowVisualState(WindowVisualState.Maximized);
  20. 20            Thread.Sleep(1000);
  21. 21
  22. 22            //Set window visual state to Normal
  23. 23            currentPattern.SetWindowVisualState(WindowVisualState.Normal);
  24. 24            Thread.Sleep(1000);
  25. 25
  26. 26            //Set window visual state to Minimized
  27. 27            currentPattern.SetWindowVisualState(WindowVisualState.Minimized);
  28. 28           
  29. 29            //Close window
  30. 30            currentPattern.Close();           
  31. 31        }
  32. 32
  33. 33        /** <summary>
  34. 34        /// Get the automation elemention of current form.
  35. 35        /// </summary>
  36. 36        /// <param name="processId">Process Id</param>
  37. 37        /// <returns>Target element</returns>
  38. 38        public static AutomationElement FindWindowByProcessId(int processId)
  39. 39        {
  40. 40            AutomationElement targetWindow = null;
  41. 41            int count = 0;
  42. 42            try
  43. 43            {
  44. 44                Process p = Process.GetProcessById(processId);
  45. 45                targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
  46. 46                return targetWindow;
  47. 47            }
  48. 48            catch (Exception ex)
  49. 49            {
  50. 50                count++;
  51. 51                StringBuilder sb = new StringBuilder();
  52. 52                string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
  53. 53                if (count > 5)
  54. 54                {
  55. 55                    throw new InvalidProgramException(message, ex);
  56. 56                }
  57. 57                else
  58. 58                {
  59. 59                    return FindWindowByProcessId(processId);
  60. 60                }
  61. 61            }
  62. 62        }
  63. 63
  64. 64        WindowPattern helper#region WindowPattern helper
  65. 65
  66. 66        /** <summary>
  67. 67        /// Get WindowPattern
  68. 68        /// </summary>
  69. 69        /// <param name="element">AutomationElement instance</param>
  70. 70        /// <returns>WindowPattern instance</returns>
  71. 71        public static WindowPattern GetWindowPattern(AutomationElement element)
  72. 72        {
  73. 73            object currentPattern;
  74. 74            if (!element.TryGetCurrentPattern(WindowPattern.Pattern, out currentPattern))
  75. 75            {
  76. 76                throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the WindowPattern.",
  77. 77                    element.Current.AutomationId, element.Current.Name));
  78. 78            }
  79. 79            return currentPattern as WindowPattern;
  80. 80        }
  81. 81
  82. 82        #endregion
  83. 83    }
  84. 84}
  85. 85
复制代码
本文简单介绍了WindowPattern以及WindowPattern在测试中的使用方法。 文/ 开着拖拉机
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值