Exemplo n.º 1
0
        public virtual IList <T> FindElements <T>(By locator, string name = null, ElementSupplier <T> supplier = null, ElementsCount expectedCount = ElementsCount.Any, ElementState state = ElementState.Displayed) where T : IElement
        {
            var elementSupplier = ResolveSupplier(supplier);

            switch (expectedCount)
            {
            case ElementsCount.Zero:
                ConditionalWait.WaitForTrue(() => !ElementFinder.FindElements(locator, state, TimeSpan.Zero, name).Any(),
                                            message: LocalizationManager.GetLocalizedMessage("loc.elements.with.name.found.but.should.not", name, locator.ToString(), state.ToString()));
                break;

            case ElementsCount.MoreThenZero:
                ConditionalWait.WaitForTrue(() => ElementFinder.FindElements(locator, state, TimeSpan.Zero, name).Any(),
                                            message: LocalizationManager.GetLocalizedMessage("loc.no.elements.with.name.found.by.locator", name, locator.ToString()));
                break;

            case ElementsCount.Any:
                ConditionalWait.WaitFor(() => ElementFinder.FindElements(locator, state, TimeSpan.Zero, name) != null);
                break;

            default:
                throw new ArgumentOutOfRangeException($"No such expected value: {expectedCount}");
            }

            var             webElements = ElementFinder.FindElements(locator, state, TimeSpan.Zero, name);
            IEnumerable <T> elements    = webElements.Select((webElement, index) =>
            {
                var elementIndex = index + 1;
                var elementName  = $"{name ?? "element"} {elementIndex}";
                return(elementSupplier(GenerateXpathLocator(locator, webElement, elementIndex), elementName, state));
            });

            return(elements.ToList());
        }
Exemplo n.º 2
0
        public void OverrideDefaultScheduler()
        {
            // Arrange
            var    binder           = Create.Binder(_viewModel, Scheduler.Default);
            Thread observableThread = null;
            var    task             = new Task <int>(() =>
            {
                observableThread = Thread.CurrentThread;
                return(5);
            });

            Thread actionThread = null;

            binder.Observe(task.ToObservable())
            .ObserveOn(Scheduler.CurrentThread)
            .Subscribe(x => actionThread = Thread.CurrentThread);

            // Act
            task.Start();

            ConditionalWait.WaitFor(() => actionThread != null);

            // Assert
            Assert.That(actionThread.ManagedThreadId, Is.EqualTo(observableThread.ManagedThreadId),
                        "Expected subscription action to run on same thread as observable.");
        }
        public override IWebElement FindElement(By locator, Func <IWebElement, bool> elementStateCondition, string stateName, TimeSpan?timeout = null)
        {
            IWebElement element = null;

            if (!ConditionalWait.WaitFor(() =>
            {
                try
                {
                    element = SearchContextSupplier().FindElement(locator);
                    return(elementStateCondition(element));
                }
                catch (WebDriverException)
                {
                    return(false);
                }
            }, timeout))
            {
                if (element != null)
                {
                    Logger.Debug("loc.elements.were.found.but.not.in.state", null, locator.ToString(), stateName);
                }
                else
                {
                    Logger.Debug("loc.no.elements.found.by.locator", null, locator.ToString());
                }
                throw new NoSuchElementException($"No elements with locator '{locator.ToString()}' were found in {stateName} state");
            }

            return(element);
        }
Exemplo n.º 4
0
        public void ObserveSourceAndTakeAction(bool binderActiveDuringEvent, bool expectUpdated)
        {
            // Arrange
            var task = new Task <int>(() => 5);

            _viewModel.MyObservable = task.ToObservable();

            var result   = 0;
            var complete = false;

            _binder.Observe(_viewModel.MyObservable)
            .Subscribe(ctx => ctx.OnNext(arg => result = arg).OnComplete(() => complete = true));
            if (!binderActiveDuringEvent)
            {
                _binder.Dispose();
            }

            // Act
            task.Start();
            task.Wait();

            var expectedComplete = expectUpdated;
            var expectedResult   = expectUpdated ? 5 : 0;

            ConditionalWait.WaitFor(() => complete == expectedComplete && result == expectedResult);

            // Assert
            Assert.That(result, Is.EqualTo(expectedResult));
            Assert.That(complete, Is.EqualTo(expectedComplete));
        }
Exemplo n.º 5
0
        public void ComplexAsyncSubscription()
        {
            // Arrange
            var task = new Task <int>(() => 5);

            _viewModel.MyObservable = task.ToObservable();

            var result   = 0;
            var complete = false;

            _binder.Observe(_viewModel.MyObservable)
            .Subscribe(ctx => ctx.OnNextAsync(SetResultAsync).OnComplete(() => complete = true));

            // Act
            task.Start();
            task.Wait();

            var expectedResult = 5;

            ConditionalWait.WaitFor(() => complete && result == expectedResult);

            // Assert
            Assert.That(result, Is.EqualTo(expectedResult));
            Assert.That(complete, Is.True);

            async Task SetResultAsync(int value)
            {
                await Task.Run(() => { result = value; });
            }
        }
Exemplo n.º 6
0
        public void SimpleAsyncSubscription()
        {
            // Arrange
            var task = new Task <int>(() => 5);

            _viewModel.MyObservable = task.ToObservable();

            var result = 0;

            _binder.Observe(_viewModel.MyObservable).SubscribeAsync(SetResultAsync);

            // Act
            task.Start();
            task.Wait();

            var expectedResult = 5;

            ConditionalWait.WaitFor(() => result == expectedResult);

            // Assert
            Assert.That(result, Is.EqualTo(expectedResult));

            async Task SetResultAsync(int value)
            {
                await Task.Run(() => { result = value; });
            }
        }
Exemplo n.º 7
0
        public void OverrideDefaultScheduler()
        {
            // Arrange
            var binder             = Create.Binder(_viewModel, Scheduler.Default);
            var observableThreadId = 0;
            var observable         = Observable.Create <int>(o =>
            {
                observableThreadId = Thread.CurrentThread.ManagedThreadId;
                o.OnNext(5);
                return(Disposable.Empty);
            }).SubscribeOn(NewThreadScheduler.Default);

            var subscriptionThreadId = 0;

            binder.Observe(observable)
            .ObserveOn(Scheduler.CurrentThread)     // CurrentThread means observable's thread
            .Subscribe(x => subscriptionThreadId = Thread.CurrentThread.ManagedThreadId);

            // Act
            ConditionalWait.WaitFor(() => subscriptionThreadId > 0);

            // Assert
            Assert.That(subscriptionThreadId, Is.EqualTo(observableThreadId),
                        "Expected subscribed action to run on same thread as the observable.");
        }
Exemplo n.º 8
0
 /// <summary>
 /// Generates xpath locator for target element
 /// </summary>
 /// <param name="baseLocator">locator of parent element</param>
 /// <param name="webElement">target element</param>
 /// <param name="elementIndex">index of target element</param>
 /// <returns>target element's locator</returns>
 protected override By GenerateXpathLocator(By baseLocator, IWebElement webElement, int elementIndex)
 {
     return(IsLocatorSupportedForXPathExtraction(baseLocator)
         ? base.GenerateXpathLocator(baseLocator, webElement, elementIndex)
         : By.XPath(ConditionalWait.WaitFor(driver => driver.ExecuteJavaScript <string>(
                                                JavaScript.GetElementXPath.GetScript(), webElement), message: "XPath generation failed")));
 }
Exemplo n.º 9
0
        public void EnableChangesOccurOnSameThreadWhereBindingOccurred()
        {
            // Arrange
            _command.CanExecuteCondition = vm => vm.IntValue > 0;
            _binder.Control(_control).OnClick(_command);

            // Act
            var task = Task.Factory.StartNew(() => _viewModel.IntValue = 5);

            task.Wait();
            ConditionalWait.WaitFor(() => _control.Enabled);
            Assert.That(_control.Enabled, Is.True);
            Assert.That(_invoker.Invoked, Is.True);
        }
        public void WaitForSliding()
        {
            var style = string.Empty;

            ConditionalWait.WaitForTrue(() =>
            {
                if (style == Style)
                {
                    return(true);
                }
                style = Style;
                return(false);
            }, message: "Sliding should stop after a while");
        }
Exemplo n.º 11
0
        public void ObserveSourceWithException()
        {
            // Arrange
            var task = new Task <int>(() => { throw new InvalidOperationException(); });

            _viewModel.MyObservable = task.ToObservable();

            Exception thrown = null;

            _binder.Observe(_viewModel.MyObservable).Subscribe(ctx => ctx.OnNext(arg => { }).OnError(ex => thrown = ex));

            // Act
            task.Start();
            ConditionalWait.WaitFor(() => task.IsFaulted && thrown != null);

            // Assert
            Assert.That(thrown, Is.InstanceOf <InvalidOperationException>());
        }
Exemplo n.º 12
0
        public void EnableIsUpdatedBasedOnCanExecute()
        {
            // Arrange
            _command.CanExecuteCondition = vm => vm.IntValue > 0;

            // Act & Assert
            _binder.Control(_control).OnClick(_command);
            Assert.That(_control.Enabled, Is.False, "Enabled should be set based off initial condition");
            _viewModel.IntValue = 5;
            ConditionalWait.WaitFor(() => _control.Enabled);
            Assert.That(_control.Enabled, Is.True);
            _viewModel.IntValue = -1;
            ConditionalWait.WaitFor(() => !_control.Enabled);
            Assert.That(_control.Enabled, Is.False);
            _viewModel.IntValue = 5;
            ConditionalWait.WaitFor(() => _control.Enabled);
            Assert.That(_control.Enabled, Is.True);
        }
        public static void OpenAutomationPracticeSite(string customUrl = null)
        {
            var resourceLimitLabel = Get <IElementFactory>()
                                     .GetLabel(By.XPath("//h1[.='Resource Limit Is Reached']"), "Resource Limit Is Reached");

            Browser.GoTo(customUrl ?? Constants.UrlAutomationPractice);
            Browser.WaitForPageToLoad();
            ConditionalWait.WaitForTrue(() =>
            {
                if (resourceLimitLabel.State.IsDisplayed)
                {
                    Browser.Refresh();
                    Browser.WaitForPageToLoad();
                    return(false);
                }
                return(true);
            }, timeout: TimeSpan.FromMinutes(3), pollingInterval: TimeSpan.FromSeconds(15),
                                        message: $"Failed to load [{customUrl ?? Constants.UrlAutomationPractice}] website. {resourceLimitLabel.Name} message is displayed.");
        }
        public override ReadOnlyCollection <IWebElement> FindElements(By locator, DesiredState desiredState, TimeSpan?timeout = null, string name = null)
        {
            var foundElements  = new List <IWebElement>();
            var resultElements = new List <IWebElement>();

            try
            {
                ConditionalWait.WaitForTrue(() =>
                {
                    foundElements  = SearchContextSupplier().FindElements(locator).ToList();
                    resultElements = foundElements.Where(desiredState.ElementStateCondition).ToList();
                    return(resultElements.Any());
                }, timeout);
            }
            catch (TimeoutException ex)
            {
                HandleTimeoutException(new WebDriverTimeoutException(ex.Message, ex), desiredState, locator, foundElements, name);
            }
            return(resultElements.AsReadOnly());
        }
Exemplo n.º 15
0
        public void SpecifyDefaultScheduler()
        {
            // Arrange
            _binder = Create.Binder(_viewModel);
            var task = new Task <int>(() => 5);

            _viewModel.MyObservable = task.ToObservable();

            var    bindingThread = Thread.CurrentThread;
            Thread actionThread  = null;

            _binder.Observe(_viewModel.MyObservable).Subscribe(x => actionThread = Thread.CurrentThread);

            // Act
            task.Start();
            task.Wait();

            ConditionalWait.WaitFor(() => actionThread != null);

            // Assert
            Assert.That(actionThread, Is.Not.SameAs(bindingThread));
        }
 public bool WaitForNotDisplayed(TimeSpan?timeout = null)
 {
     return(DoAndLogWaitForState(() => ConditionalWait.WaitFor(() => !IsDisplayed, timeout), "not.displayed", timeout));
 }
 public bool WaitForNotExist(TimeSpan?timeout = null)
 {
     return(DoAndLogWaitForState(() => ConditionalWait.WaitFor(() => !IsExist, timeout), "not.exist", timeout));
 }