Powered By Blogger

Search Here!

Sunday, June 13, 2021

Webdriver Wait for window loads !

Wait for window loads and then select the window.

/// Wait for window till it gets load on the page.
///
/// The name of the window.
/// This is time to wait.
/// if the window not loads in the specified interval of time.
/// If the window not exists on the page.
        protected void WaitUntilWindowLoads(string windowName, int timeOut = -1)
        {
            // Intialization of bool 'isWindowPresent' for checking window present
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            if (timeOut == -1)
            {
                timeOut = this.waitTimeOut;
            }
            while (stopWatch.Elapsed.TotalSeconds < timeOut)
            {
                try
                {
                    //Wait for window
                    if (WaitUntilWindow(windowName) == true) break;
                }
                catch (Exception)
                {
                    // For Any exceptions catch value is assinged false
                }
            }
        }
  
///
/// Is Window Opened In the Specified Interval of Time or not.
///
/// This is the name of the window.
/// This is the time to wait for window get open.
/// True if the window is opened otherwise false.
/// If the window not able to find in the specified time.
/// If the window not exists on the page.
        protected bool WaitUntilWindow(string windowName, int timeOut = -1)
        {
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            if (timeOut == -1)
            {
                timeOut = this.waitTimeOut;
            }
            while (stopWatch.Elapsed.TotalSeconds < timeOut)
            {
                if (WebDriver.WindowHandles.Any(item =>                       WebDriver.SwitchTo().Window(item).Title == windowName))
                {
                    return true;
                }
            }
            stopWatch.Stop();
            return false;
        }

No comments:

Post a Comment