UI Automation-WindowPattern

来源:互联网 发布:黑客帝国数字雨 算法 编辑:程序博客网 时间:2024/05/21 14:08

感谢原作者的提醒  http://blog.csdn.net/lynn_yan/article/category/611860


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

示例:

public static void launchCal()

        {

            //Launch Calculator

            Process proc = Process.Start(@"C:/WINDOWS/system32/calc.exe");

            Thread.Sleep(2000);

 

            //Recognize Calculator Window  "Calculator"

            AutomationElement window = AutomationElement.RootElement.FindFirst(TreeScope.Children, newPropertyCondition(AutomationElement.NameProperty, "Calculator"));

 

            Assert.IsNotNull(window);

            Thread.Sleep(2000);

 

            WindowPattern window1pattern = Utility.UIA.ControlPattern.Windowpattern.GetWindowPattern(window);

            window1pattern.SetWindowVisualState(WindowVisualState.Minimized);

            Thread.Sleep(2000);

 

            window1pattern.SetWindowVisualState(WindowVisualState.Normal);

            Thread.Sleep(2000);}

    public static WindowPattern GetWindowPattern(AutomationElement element)

        {

                object currentPattern;

                if (!element.TryGetCurrentPattern(WindowPattern.Pattern, out currentPattern))

                {

                    throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the WindowPattern.",

                       element.Current.AutomationId, element.Current.Name));

                }

                return currentPattern as WindowPattern;

        }

 


0 0
原创粉丝点击