private AutomationElement GetWindow(string name)
        {
            AutomationElement element = AutomationElement.RootElement.FindFirst(TreeScope.Children,
                                                                                new AndCondition(
                                                                                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window)
                                                                                    , new PropertyCondition(AutomationElement.NameProperty, name)
                                                                                    , new PropertyCondition(AutomationElement.ProcessIdProperty, DesktopController.DesktopProcess.Id)
                                                                                    ));

            //maybe app restart, the process id was changed. need to refrush process id.
            if (element == null)
            {
                DesktopController.InitializeDesktop(_app);
                element =
                    AutomationElement.RootElement.FindFirst(TreeScope.Children,
                                                            new AndCondition(
                                                                new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window)
                                                                , new PropertyCondition(AutomationElement.NameProperty, name)
                                                                , new PropertyCondition(AutomationElement.ProcessIdProperty, DesktopController.DesktopProcess.Id)
                                                                ));
            }
            //still cannot get element
            if (element == null)
            {
                throw new Exception(string.Format("Cannot found window: {0}", name));
            }
            return(element);
        }
        public void Init()
        {
            if (DesktopController.InitializeDesktop(_app))
            {
                _bStartFromTestingTool = true;
            }

            //_regions = new Dictionary<string, string>(_app.Regions.Count);
            foreach (var r in _app.Regions)
            {
                bool     found = false;
                DateTime end   = DateTime.Now.AddMinutes(20);
                while (end > DateTime.Now)
                {
                    AutomationElement win = AutomationElement.RootElement.FindFirst(TreeScope.Children, new AndCondition(
                                                                                        new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
                                                                                        new PropertyCondition(AutomationElement.NameProperty, r.Name),
                                                                                        new PropertyCondition(AutomationElement.ProcessIdProperty, DesktopController.DesktopProcess.Id)
                                                                                        ));
                    if (win != null)
                    {
                        found = true;
                        if (_currentRegionId == null)
                        {
                            _currentRegionId   = r.Id;
                            _currentRegionName = r.Name;
                        }
                        break;
                    }
                }
                if (!found)
                {
                    throw new Exception(string.Format("Cannot find window {0}", r.Name));
                }
            }
        }