Skip to content
pawelstryczek edited this page Sep 15, 2017 · 10 revisions

Following functionalities were implemented for AngularJS support.

Wait for Angular method.

public static void WaitForAngular(this IWebDriver webDriver, double timeout)
        {
            try
            {
                new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeout)).Until(
                    driver =>
                    {
                        var javaScriptExecutor = driver as IJavaScriptExecutor;
                        return javaScriptExecutor != null
                               &&
                               (bool)javaScriptExecutor.ExecuteScript(
                                   "return window.angular != undefined && window.angular.element(document.body).injector().get('$http').pendingRequests.length == 0");
                    });
            }
            catch (InvalidOperationException)
            {
                Logger.Info("Wait for angular invalid operation exception.");
            }
        }

Synchronize with Angular.

Synchronization is added in GetElement function. If feature is enabled framework will wait for Angular process to be finished.

public static IWebElement GetElement(this ISearchContext element, ElementLocator locator, double timeout, Func<IWebElement, bool> condition, [Optional] string customMessage)
        {
            var driver = element.ToDriver();
            if (DriversCustomSettings.IsDriverSynchronizationWithAngular(driver))
            {
                driver.WaitForAngular();
            }

            var by = locator.ToBy();

            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)) { Message = customMessage };
            wait.IgnoreExceptionTypes(typeof(StaleElementReferenceException));

            wait.Until(
                    drv =>
                    {
                        var ele = element.FindElement(@by);
                        return condition(ele);
                    });

            return element.FindElement(@by);
        }

Enabling AngularJS synchronization from the code

To enable feature this.Driver.SynchronizeWithAngular(true); need to be added. To disable this.Driver.SynchronizeWithAngular(false);

Remarks

Please notice that sychronization will not work when default FindElement is using instead of GetElement from the framework.

Clone this wiki locally