UI Automation-TogglePattern

来源:互联网 发布:回合制源码 编辑:程序博客网 时间:2024/05/17 23:56

支持TogglePattern的控件有CheckBoxTreeView中的button控件的那个。

TogglePattern控件模式用于支持可以循环通过一组状态并在设置后保持某种状态的控件。控件必须按以下顺序循环通过其ToggleStateonoffIndeterminate

示例:

        public static void check_Toggle()

        {

            //Launch Photoshow touch

            Process photo = Process.Start(@"C:/Program Files/Roxio/PhotoShowTouch/PhotoShowTouchV2_2008.exe");

            Thread.Sleep(10000);

 

            //Launch settings Dialogue

            AutomationElement window = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "PhotoShow Touch"));

            Thread.Sleep(2000);

 

            AutomationElement settingbutton = window.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "_SettingBtn"));

            Assert.IsNotNull(settingbutton);

            Thread.Sleep(2000);

            InvokePattern settinginvokepattern = Utility.UIA.ControlPattern.Invokepattern.GetInvokePattern(settingbutton);

            settinginvokepattern.Invoke();

            Thread.Sleep(5000);

 

            //Select Checkbox

 

            AutomationElement checkbox1 = window.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "_chkDownloadTime"));

            Thread.Sleep(2000);

 

            TogglePattern checkbox1pattern = Utility.UIA.ControlPattern.Togglepattern.GetTogglePattern(checkbox1);

            if (checkbox1pattern.Current.ToggleState.ToString() == "Off")

            {

                checkbox1pattern.Toggle();

                Thread.Sleep(2000);

            }

        }

 

TogglePattern Helper

 

public static TogglePattern GetTogglePattern(AutomationElement targetControl)

            {

                TogglePattern togglePattern = null;

 

                try

                {

                    togglePattern =

                        targetControl.GetCurrentPattern(TogglePattern.Pattern)

                        as TogglePattern;

                }

                catch (InvalidOperationException)

                {

                    // object doesn't support the TogglePattern control pattern

                    Log.Error(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the  TogglePattern.",

   targetControl.Current.AutomationId, targetControl.Current.Name));

                    return null;

                }

 

                return togglePattern;

            }

 

原创粉丝点击