public ISHAutomationElement[] FindAllWithOptions(TreeScope treeScope, ConditionBase condition, TreeTraversalOption traversalOptions, SHAutomationElement root, TimeSpan timeout, bool waitUntilExists = true)
        {
            List <SHAutomationElement> elements = new List <SHAutomationElement>();

            bool getElements(bool shouldExist)
            {
                if (!elements.Any())
                {
                    elements = FindAllWithOptionsBase(treeScope, condition, traversalOptions, root).Where(x => x.FrameworkAutomationElement != null).ToList();
                }
                return(shouldExist ? elements.Any() : !elements.Any());
            }

            getElements(waitUntilExists);
            if (!elements.Any() && waitUntilExists && timeout.TotalMilliseconds > 0)
            {
                SHSpinWait.SpinUntil(() => getElements(true), timeout);
            }
            else if (elements.Any() && !waitUntilExists && timeout.TotalMilliseconds > 0)
            {
                SHSpinWait.SpinUntil(() => getElements(false), timeout);
            }
            if (elements != null && elements.Any())
            {
                return(elements.ToArray());
            }
            return(Array.Empty <SHAutomationElement>());
        }
        public ISHAutomationElement[] FindAllDescendants(TimeSpan timeout)
        {
            List <SHAutomationElement> elements = new List <SHAutomationElement>();

            bool getElements()
            {
                if (!elements.Any())
                {
                    elements = FindAllDescendantsBase().Where(x => x.FrameworkAutomationElement != null).ToList();
                }
                return(elements.Any());
            }

            getElements();
            if (!elements.Any() && timeout.TotalSeconds != 0)
            {
                SHSpinWait.SpinUntil(() => getElements(), timeout);
            }

            if (elements != null && elements.Any())
            {
                return(elements.ToArray());
            }

            return(Array.Empty <SHAutomationElement>());
        }
예제 #3
0
        private Window GetMainWindow(AutomationBase automation, TimeSpan?waitTimeout, string pathToConfigFile)
        {
            WaitWhileMainHandleIsMissing(waitTimeout);
            var mainWindowHandle = MainWindowHandle;

            if (mainWindowHandle == IntPtr.Zero)
            {
                return(null);
            }

            SHSpinWait.SpinUntil(() => automation.FromHandle(mainWindowHandle) != null, waitTimeout.HasValue ? waitTimeout.Value : TimeSpan.FromMinutes(5));

            var mainWindow = automation.FromHandle(mainWindowHandle)
                             .AsWindow(_loggingService, pathToConfigFile);

            if (mainWindow != null)
            {
                mainWindow.IsMainWindow = true;
            }
            else
            {
                throw new InvalidOperationException("mainWindow is null");
            }
            return(mainWindow);
        }
        public ISHAutomationElement FindFirstDescendant(ConditionBase condition, TimeSpan timeout, bool waitUntilExists = true)
        {
            SHAutomationElement element = null;

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

            getElement(true);
            if (element == null && waitUntilExists && timeout.TotalMilliseconds > 0)
            {
                SHSpinWait.SpinUntil(() => getElement(waitUntilExists), timeout);
            }
            else if (element != null && !waitUntilExists && timeout.TotalMilliseconds > 0)
            {
                SHSpinWait.SpinUntil(() => getElement(waitUntilExists), timeout);
            }

            return(element?.FrameworkAutomationElement != null ? element : null);
        }
 /// <summary>
 /// Waits until the element is enabled.
 /// </summary>
 public static T WaitUntilEnabled <T>(this T self, TimeSpan?timeout = null) where T : SHAutomationElement
 {
     if (self != null)
     {
         SHSpinWait.SpinUntil(() => self.IsEnabled, timeout.HasValue ? timeout.Value : TimeSpan.FromMilliseconds(500));
     }
     return(self);
 }
예제 #6
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);
        }
예제 #7
0
 /// <summary>
 /// Waits until the main handle is set.
 /// </summary>
 /// <param name="waitTimeout">An optional timeout. If null is passed, the timeout is infinite.</param>
 /// <returns>True a main window handle was found, false otherwise.</returns>
 public bool WaitWhileMainHandleIsMissing(TimeSpan?waitTimeout = null)
 {
     return(SHSpinWait.SpinUntil(() =>
     {
         int processId = _process.Id;
         _process.Dispose();
         _process = FindProcess(processId);
         return _process.MainWindowHandle != IntPtr.Zero;
     }, waitTimeout.HasValue ? waitTimeout.Value : TimeSpan.FromMinutes(2)));
 }
예제 #8
0
        public void TimesOutAfterTimeoutElapses_SpinWait_BeGreaterOrEqualTo()
        {
            var elapsed = PerformanceDiagnostics.Time(() =>
            {
                SHSpinWait.SpinUntil(() => true == false, TimeSpan.FromMilliseconds(50));
            });

            //Because of the way spinuntil works there is a slight margin of error, this checks it's within that margin
            elapsed.Should().BeGreaterOrEqualTo(TimeSpan.FromMilliseconds(30)).And.BeLessOrEqualTo(TimeSpan.FromMilliseconds(70));
        }
        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);
        }
 public void HoverOver(int mouseSpeed = 5)
 {
     SHSpinWait.SpinUntil(() => SupportsBoundingRectangle, TimeSpan.FromSeconds(5));
     if (SupportsBoundingRectangle)
     {
         SHSpinWait.SpinUntil(() => !BoundingRectangle.IsEmpty, TimeSpan.FromSeconds(10));
         if (!BoundingRectangle.IsEmpty)
         {
             MouseHelpers.MoveTo(Centre(), mouseSpeed);
         }
         else
         {
             throw new InvalidOperationException("HoverOver bounding rectangle failed to load or is not supported (You may need an onscreen check)");
         }
     }
     else
     {
         throw new ElementNotFoundException("HoverOver bounding rectangle is not supported");
     }
 }
        public ISHAutomationElement FindFirstChild(TimeSpan timeout, bool waitUntilExists = true)
        {
            SHAutomationElement element = null;

            bool getElement(bool shouldExist)
            {
                if (element == null)
                {
                    element = FindFirstChildBase();
                }
                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);
        }
        public ISHAutomationElement[] FindAllDescendantsNameEndsWith(string name, TimeSpan timeout, bool waitUntilExists = true)
        {
            List <ISHAutomationElement> elements = new List <ISHAutomationElement>();

            bool getElements(bool shouldExist)
            {
                if (!elements.Any())
                {
                    elements = FindAllDescendants(timeout: TimeSpan.Zero).Where(x => x.FrameworkAutomationElement != null && x.SupportsName && !string.IsNullOrEmpty(x.Name) && x.Name.EndsWith(name)).ToList();
                }
                return(shouldExist ? elements.Any() : !elements.Any());
            }

            getElements(waitUntilExists);
            if (!elements.Any() && waitUntilExists && timeout.TotalMilliseconds > 0)
            {
                SHSpinWait.SpinUntil(() => getElements(true), timeout);
            }
            else if (elements.Any() && !waitUntilExists && timeout.TotalMilliseconds > 0)
            {
                SHSpinWait.SpinUntil(() => getElements(false), timeout);
            }
            return(elements.ToArray());
        }
예제 #13
0
        public ISHAutomationElement[] FindAllByXPath(string xPath, TimeSpan timeout, bool waitUntilExists = true)
        {
            List <SHAutomationElement> elements = new List <SHAutomationElement>();

            bool getElements(bool shouldExist, bool firstRun)
            {
                if (firstRun || elements.Any() != shouldExist)
                {
                    elements = FindAllByXPathBase(xPath).Where(x => x.FrameworkAutomationElement != null).ToList();
                }
                return(shouldExist ? elements.Any() : !elements.Any());
            }

            getElements(waitUntilExists, true);
            if (!elements.Any() && waitUntilExists && timeout.TotalMilliseconds > 0)
            {
                SHSpinWait.SpinUntil(() => getElements(true, false), timeout);
            }
            else if (elements.Any() && !waitUntilExists && timeout.TotalMilliseconds > 0)
            {
                SHSpinWait.SpinUntil(() => getElements(false, false), timeout);
            }
            return(elements.ToArray());
        }
예제 #14
0
 public void ReturnsFalseWhenTimedOut_SpinWait_BeFalse()
 {
     SHSpinWait.SpinUntil(() => true == false, TimeSpan.FromMilliseconds(50)).Should().BeFalse();
 }
예제 #15
0
        public void CheckNonZeroTimeoutIsValid_SpinWait_NotThrowInvalidOperationException()
        {
            Action act = () => SHSpinWait.SpinUntil(() => true, TimeSpan.FromMilliseconds(10));

            act.Should().NotThrow <InvalidOperationException>("Should not throw invalid operation exception if timeout >0 is passed");
        }
예제 #16
0
        public void CheckZeroTimeOutIsInvalid_SpinWait_ThrowInvalidOperationException()
        {
            Action act = () => SHSpinWait.SpinUntil(() => true, TimeSpan.Zero);

            act.Should().Throw <InvalidOperationException>("Should throw invalid operation exception if 0 timeout is passed");
        }
예제 #17
0
        public void CheckNegativeTimeOutIsInvalid_SpinWait_ThrowInvalidOperationException()
        {
            Action act = () => SHSpinWait.SpinUntil(() => true, TimeSpan.FromMilliseconds(-10));

            act.Should().Throw <InvalidOperationException>("Should throw invalid operation exception if timeout < 0 is passed");
        }