Пример #1
0
        public void Loaded()
        {
            SearchResults        = new ISHAutomationElement[0];
            SavedSettingsWindows = new ObservableCollection <WindowBO>();
            var savedWins     = Settings.Default.Windows;
            var inspectColour = Settings.Default.InspectionColour;

            SelectedColour = new System.Windows.Media.Color()
            {
                A = inspectColour.A,
                R = inspectColour.R,
                G = inspectColour.G,
                B = inspectColour.B
            };

            IsLive = Settings.Default.IsLive;
            if (savedWins != null)
            {
                foreach (string win in savedWins)
                {
                    SavedSettingsWindows.Add(new WindowBO(win, false));
                }
            }
            Elements   = new ObservableCollection <ElementBO>();
            Automation = new UIA3Automation();
            Automation.TransactionTimeout = new TimeSpan(0, 0, 0, 5);
            GetDesktop();
            PropertyChangedEventManager.AddHandler(this, SelectedItemChanged, nameof(SelectedItemInTree));
        }
Пример #2
0
 public WindowBO(ISHAutomationElement automationElement, bool isCurrent)
 {
     AutomationElement = automationElement;
     IsCurrent         = isCurrent;
     UpdateProperties();
     Identifier = !string.IsNullOrEmpty(AutomationId) ? AutomationId : Name;
 }
Пример #3
0
        public void GetDesktop()
        {
            Properties = null;
            Patterns   = null;
            Image      = null;
            Elements   = new ObservableCollection <ElementBO>();
            var item = _automation.GetDesktop();

            SearchResults      = new ISHAutomationElement[0];
            CurrentSearchIndex = 0;
            _treeWalker        = _automation.TreeWalkerFactory.GetControlViewWalker();
            DesktopItem        = item != null ? item : null;
            var items = DesktopItem.FindAllByXPath("*").Select(x => new ElementBO(new SHAutomationElement(x.FrameworkAutomationElement)));

            foreach (var win in items)
            {
                if (SavedSettingsWindows.Any(x => (!string.IsNullOrEmpty(win.AutomationId?.Trim()) && win.AutomationId.StartsWith(x.Identifier)) || (!string.IsNullOrEmpty(win.Name?.Trim()) && win.Name.StartsWith(x.Identifier))))
                {
                    if (!Elements.Any(x => x.AutomationElement.Equals(win.AutomationElement)))
                    {
                        Elements.Add(win);
                        Elements.ToList().ForEach(x => x.IsExpanded = true);
                    }
                }
            }
            if (Elements.Any())
            {
                SelectedItem = Elements.First();
            }

            _dispatcherTimer          = new DispatcherTimer();
            _dispatcherTimer.Tick    += DispatcherTimerTick;
            _dispatcherTimer.Interval = TimeSpan.FromMilliseconds(1);
            _dispatcherTimer.Start();
        }
Пример #4
0
 /// <summary>
 /// Method to convert the element to the given type.
 /// </summary>
 public static SHAutomationElement AsType(this ISHAutomationElement self, Type type)
 {
     if (!type.IsAssignableFrom(typeof(SHAutomationElement)))
     {
         throw new ArgumentException("The given type is not an SHAutomationElement", nameof(type));
     }
     return((SHAutomationElement)Activator.CreateInstance(type, self.FrameworkAutomationElement));
 }
Пример #5
0
        public ISHAutomationElement Find(Func <ConditionFactory, ConditionBase> conditionFunc, TimeSpan timeout, TimeSpan offscreenTimeout, ISHAutomationElement parent = null)
        {
            ISHAutomationElement control = null;
            bool regenerateXPath         = false;

            var condition       = conditionFunc(new ConditionFactory(Automation.PropertyLibrary));
            var conditionString = condition.ToString();

            _loggingService.Info(string.Format("Find called {0}", conditionString));
            bool canUseXpath = !(conditionString.Contains("OR") || conditionString.Contains("NOT"));
            List <(PropertyCondition Value, bool Ignore)> propertyConditions = new List <(PropertyCondition Value, bool Ignore)>();

            if (parent == null)
            {
                if (canUseXpath)
                {
                    propertyConditions = GetPropertyConditions(condition);
                }

                control = GetXPathFromPropertyConditions(propertyConditions);

                if (control == null)
                {
                    regenerateXPath = true;
                    control         = FindFirstDescendant(conditionFunc, timeout: timeout);
                }
            }
            else
            {
                control = parent.FindFirstDescendant(conditionFunc, timeout);
            }

            if (control == null)
            {
                _loggingService.Error(string.Format("Failed to find control by: {0}", conditionFunc(new ConditionFactory(Automation.PropertyLibrary)).ToString()));

                throw new ElementNotFoundException(string.Format("Failed to find control by: {0}", conditionFunc(new ConditionFactory(Automation.PropertyLibrary)).ToString()));
            }

            _loggingService.Info("Find found control");

            SHSpinWait.SpinUntil(() => control.SupportsOnscreen, TimeSpan.FromMilliseconds(500));
            if (control.SupportsOnscreen)
            {
                SHSpinWait.SpinUntil(() => control.IsOnscreen, offscreenTimeout);
                _loggingService.Info("Find OnScreen: " + control.IsOnscreen);
            }
            else
            {
                _loggingService.Info("Find OnScreen is not supported");
            }


            SaveXPathFromControl((SHAutomationElement)control, propertyConditions, regenerateXPath);


            return(control);
        }
        public void DrawHighlight(ISHAutomationElement element)
        {
            ElementHighlighter.UseElementHighlighter = true;
            var drawingcolor = System.Drawing.Color.FromArgb(
                SelectedColour.A, SelectedColour.R, SelectedColour.G, SelectedColour.B);

            ElementHighlighter.HighlightColour = drawingcolor;
            ElementHighlighter.HighlightElement(element as SHAutomationElement);
            ElementHighlighter.UseElementHighlighter = false;
        }
Пример #7
0
        public async void Search()
        {
            SearchResults      = new ISHAutomationElement[0];
            CurrentSearchIndex = 0;
            IsSearching        = true;
            await Task.Run(() =>
            {
                try
                {
                    string xpath = string.Empty;
                    if (SelectedItemInTree != null)
                    {
                        if (SelectedSearchTerm == SHInspectConstants.ControlType)
                        {
                            xpath = $"//{SearchText}";
                        }
                        else if (SelectedSearchTerm == "XPath")
                        {
                            xpath = SearchText;
                        }
                        else
                        {
                            xpath = $"//*[@{SelectedSearchTerm}='{SearchText}']";
                        }
                    }
                    //else
                    //{
                    //    if (SelectedSearchTerm == SHInspectConstants.ControlType)
                    //    {
                    //        xpath = $"//{SearchText}";
                    //    }
                    //    else if (SelectedSearchTerm == "XPath")
                    //    {
                    //        xpath = SearchText;
                    //    }
                    //    else
                    //    {
                    //        xpath = $"//*[@{SelectedSearchTerm}='{SearchText}']";
                    //    }
                    //}

                    SearchResults = SelectedItemInTree.AutomationElement.FindAllByXPath(xpath);
                    if (SearchResults.Any())
                    {
                        SelectedItem = new ElementBO(SearchResults[0] as SHAutomationElement);
                    }
                }
                catch (XPathException)
                {
                    IsSearching = false;
                }
            });

            IsSearching = false;
        }
 private void CreateAddWindowPopUp(ISHAutomationElement window)
 {
     System.Windows.Application.Current.Dispatcher.Invoke(delegate
     {
         AddWindowPopup pop = new AddWindowPopup(new WindowBO(window, false));
         pop.Left           = Mouse.Position.X;
         pop.Top            = Mouse.Position.Y;
         pop.Show();
         pop.BringIntoView();
         pop.Topmost = true;
     });
 }
        public ISHAutomationElement FindFirstByXPath(string xPath, TimeSpan timeout)
        {
            ISHAutomationElement element = null;

            bool getElement(string xpath)
            {
                element = FindFirstByXPath(xpath);
                return(element != null);
            }

            SHSpinWait.SpinUntil(() => getElement(xPath), timeout);
            return(element);
        }
Пример #10
0
 private static object GetPropertyValueHandled(ISHAutomationElement automationElement, PropertyId propertyId)
 {
     try
     {
         object output = null;
         automationElement.FrameworkAutomationElement.TryGetPropertyValue(propertyId, out output);
         return(output);
     }
     catch
     {
         return(null);
     }
 }
Пример #11
0
        public static List <PatternBO> GetElementDetailViewModelPatterns(ISHAutomationElement element)
        {
            try
            {
                List <PatternBO> details                    = new List <PatternBO>();
                var            library                      = element.Automation.PatternLibrary;
                PropertyInfo[] allPatterns                  = GetPublicProperties(library.GetType());
                List <string>  patternNames                 = allPatterns.Select(x => x.Name.Split("Pattern")[0]).ToList();
                var            supportedPatterns            = element.GetSupportedPatterns().Where(x => x.Id != 0);
                var            automationPatterns           = element.Patterns;
                var            automationPatternsProperties = GetPublicProperties(typeof(IFrameworkPatterns));


                foreach (var item in patternNames)
                {
                    if (supportedPatterns.Any(x => x.Name == item))
                    {
                        var    pattern = supportedPatterns.First(x => x.Name == item);
                        object output;
                        element.FrameworkAutomationElement.TryGetNativePattern(pattern, out output);

                        if (output != null)
                        {
                            details.Add(new PatternBO(item, string.Empty, true, true));
                            var patternObject     = automationPatternsProperties.First(x => x.Name.Split("Pattern")[0] == pattern.Name);
                            var patternType       = patternObject.GetMethod.Invoke(automationPatterns, null);
                            var patternGet        = patternType.GetType().GetMethods().First(x => x.Name == "get_Pattern").Invoke(patternType, null);
                            var patternProperties = GetPublicProperties(patternGet.GetType());
                            foreach (var prop in patternProperties)
                            {
                                var returntype = prop.GetMethod.ReturnType;
                                if (returntype.Name.Contains("AutomationProperty"))
                                {
                                    details.Add(new PatternBO(prop.Name, prop.GetMethod.Invoke(patternGet, null).ToString(), true, false));
                                }
                            }
                        }
                    }
                    else
                    {
                        details.Add(new PatternBO(item, string.Empty, false, true));
                    }
                }
                return(details.OrderByDescending(x => x.IsSupported).ToList());
            }
            catch (Exception ex) when(ex is OverflowException || ex is InvalidOperationException || ex is ArgumentException)
            {
                return(null);
            }
        }
Пример #12
0
 public ISHAutomationElement GetRootFromElement(ElementBO element)
 {
     try
     {
         ISHAutomationElement win = Automation.GetParent(element.AutomationElement, x => x.ByControlType(ControlType.Window).And(x.ByClassName(SHInspectConstants.Popup).Not()));
         if (win?.Parent != null && !win.Parent.Equals(DesktopItem))
         {
             win = win.Parent;
         }
         return(win);
     }
     catch (COMException)
     {
         return(null);
     }
 }
Пример #13
0
        public bool Exists(Func <ConditionFactory, ConditionBase> conditionFunc, TimeSpan timeout, TimeSpan offscreenTimeout, bool checkOnScreen = true, ISHAutomationElement parent = null, bool xpathOnly = false)
        {
            bool exists                  = true;
            bool regenerateXPath         = false;
            ISHAutomationElement control = null;

            if (parent == null)
            {
                control = GetXPathElementFromCondition(conditionFunc, timeout);
                if (control == null && !xpathOnly)
                {
                    regenerateXPath = true;
                    control         = FindFirstDescendant(conditionFunc, timeout: timeout);
                }
            }
            else
            {
                if (xpathOnly)
                {
                    throw new InvalidOperationException("Cannot search with xpath using parent");
                }
                control = parent.FindFirstDescendant(conditionFunc, timeout: timeout);
            }

            if (control == null)
            {
                exists = false;
            }
            else if (checkOnScreen)
            {
                control.WaitUntilPropertyEquals(PropertyId.Register(AutomationType.UIA3, 30022, "IsOffscreen"), false, offscreenTimeout);

                exists = control.IsOnscreen;
            }

            if (parent == null)
            {
                SaveXPathFromControl((SHAutomationElement)control, conditionFunc, regenerateXPath);
            }

            return(exists);
        }
        public ISHAutomationElement FindFirstDescendant(TimeSpan timeout, bool waitUntilExists = true)
        {
            ISHAutomationElement element = null;

            bool getElement(bool shouldExist)
            {
                if (element == null)
                {
                    element = FindFirstDescendantBase();
                }
                return(shouldExist ? element?.FrameworkAutomationElement != null : element == null);
            }

            getElement(waitUntilExists);
            if (element == null && waitUntilExists && timeout.TotalMilliseconds > 0)
            {
                SHSpinWait.SpinUntil(() => getElement(true), timeout);
            }
            else if (element != null && !waitUntilExists && timeout.TotalMilliseconds > 0)
            {
                SHSpinWait.SpinUntil(() => getElement(false), timeout);
            }
            return(element?.FrameworkAutomationElement != null ? element : null);
        }
Пример #15
0
 public ISHAutomationElement Find(string automationID, TimeSpan timeout, TimeSpan offscreenTimeout, ISHAutomationElement parent = null)
 {
     return(Find(x => x.ByAutomationId(automationID), timeout, offscreenTimeout, parent));
 }
Пример #16
0
 public ISHAutomationElement Find(string automationID, TimeSpan timeout, ISHAutomationElement parent = null)
 {
     return(Find(x => x.ByAutomationId(automationID), timeout, TimeSpan.FromSeconds(10), parent));
 }
Пример #17
0
 /// <summary>
 /// Converts the element to a <see cref="TreeItem"/>.
 /// </summary>
 public static TreeItem AsTreeItem(this ISHAutomationElement self)
 {
     return(self == null ? null : new TreeItem(self.FrameworkAutomationElement));
 }
Пример #18
0
 /// <summary>
 /// Generic method to convert the element to the given type.
 /// </summary>
 public static T AsType <T>(this ISHAutomationElement self) where T : SHAutomationElement
 {
     return((T)Activator.CreateInstance(typeof(T), self.FrameworkAutomationElement));
 }
Пример #19
0
 public bool Exists(Func <ConditionFactory, ConditionBase> conditionFunc, bool checkOnScreen = true, ISHAutomationElement parent = null, bool xpathOnly = false)
 {
     return(Exists(conditionFunc, TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500), checkOnScreen, parent, xpathOnly));
 }
Пример #20
0
 /// <summary>
 /// Converts the element to a <see cref="HorizontalScrollBar"/>.
 /// </summary>
 public static HorizontalScrollBar AsHorizontalScrollBar(this ISHAutomationElement self)
 {
     return(self == null ? null : new HorizontalScrollBar(self.FrameworkAutomationElement));
 }
Пример #21
0
 /// <summary>
 /// Converts the element to a <see cref="GridHeaderItem"/>.
 /// </summary>
 public static GridHeaderItem AsGridHeaderItem(this ISHAutomationElement self)
 {
     return(self == null ? null : new GridHeaderItem(self.FrameworkAutomationElement));
 }
Пример #22
0
 /// <summary>
 /// Converts the element to a <see cref="GridCell"/>.
 /// </summary>
 public static GridCell AsGridCell(this ISHAutomationElement self)
 {
     return(self == null ? null : new GridCell(self.FrameworkAutomationElement));
 }
Пример #23
0
 /// <summary>
 /// Converts the element to a <see cref="DataGridView"/>.
 /// </summary>
 public static DataGridView AsDataGridView(this ISHAutomationElement self)
 {
     return(self == null ? null : new DataGridView(self.FrameworkAutomationElement));
 }
Пример #24
0
 /// <summary>
 /// Converts the element to a <see cref="ComboBox"/>.
 /// </summary>
 public static ComboBox AsComboBox(this ISHAutomationElement self)
 {
     return(self == null ? null : new ComboBox(self.FrameworkAutomationElement));
 }
Пример #25
0
 /// <summary>
 /// Converts the element to a <see cref="Window"/>.
 /// </summary>
 public static Window AsWindow(this ISHAutomationElement self, string pathToConfigFile = null)
 {
     return(AsWindow(self, null, pathToConfigFile));
 }
Пример #26
0
 public ISHAutomationElement Find(Func <ConditionFactory, ConditionBase> conditionFunc, ISHAutomationElement parent = null)
 {
     return(Find(conditionFunc, TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(10), parent));
 }
Пример #27
0
 /// <summary>
 /// Converts the element to a <see cref="ListBox"/>.
 /// </summary>
 public static ListBox AsListBox(this ISHAutomationElement self)
 {
     return(self == null ? null : new ListBox(self.FrameworkAutomationElement));
 }
Пример #28
0
 /// <summary>
 /// Converts the element to a <see cref="VerticalScrollBar"/>.
 /// </summary>
 public static VerticalScrollBar AsVerticalScrollBar(this ISHAutomationElement self)
 {
     return(self == null ? null : new VerticalScrollBar(self.FrameworkAutomationElement));
 }
Пример #29
0
 /// <summary>
 /// Converts the element to a <see cref="Label"/>.
 /// </summary>
 public static Label AsLabel(this ISHAutomationElement self)
 {
     return(self == null ? null : new Label(self.FrameworkAutomationElement));
 }
Пример #30
0
 /// <summary>
 /// Converts the element to a <see cref="Window"/>.
 /// </summary>
 public static Window AsWindow(this ISHAutomationElement self, ILoggingService loggingService, string pathToConfigFile = null)
 {
     return(self == null ? null : new Window(self.FrameworkAutomationElement, loggingService, pathToConfigFile));
 }