Search Here!

Saturday, February 16, 2013

Webdriver Handling Thinking Indicator Loading

When we switch to different page or Iframes, we need to wait for some time due to loader in progress. Webdriver throws exception like 'ElementNotVisibleException' or 'TimeoutException' in case if we not handled this situation properly. 

///
/// Wait until Thinking Indicator is loading on the page.
///

/// True if the thinking indicator element is currently present
/// after the specified time interval, false otherwise.

/// if the window not loads in the specified interval of time.

/// If the element not visible on the page.

        protected bool IsThinkingIndicatorLoading(int timeOut = -1)
        {
            //If element not present then webdriver throw the exception,
            //catch this exception so that outer code does not worry
            //about this exception and existing logic which relies on TRUE/FALSE can work as it is.
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            if (timeOut == -1)
            {
                timeOut = this.waitTimeOut;
            }
            bool isThinkingIndicatorProcessing = false;
            try
            {
                while (stopWatch.Elapsed.TotalSeconds < timeOut)
                {
                    isThinkingIndicatorProcessing = WebDriver.FindElement(By.XPath
                        ("//img[contains(@src,'/IMG/ThinkingIndicator/process.gif')]")).Displayed;
                }
            }
            catch (Exception)
            {
                stopWatch.Stop();
                return false;
            }
            stopWatch.Stop();
            return isThinkingIndicatorProcessing;
        }

No comments:

Post a Comment