Unable to wait in page when partial page is getting loaded due to Ajax

322 views Asked by At

I have some controls in html's section tag and those controls load when ajax request completed. Now there is a scenario where ajax is loaded but controls are not appearing on the page and I want to stop code execution till all controls are loaded and visible on the page.

How to identify that all controls are loaded on the page.

I don't want to use the following solutions:

1) Explicit wait in code (because I don't know when controls are loaded).

2) I don't want to use wait.Until(ExpectedConditions.ElementIsVisible(By.Id(id))); (because if the particular control is not present on the page then it will wait indefinitely)

Any help would be appreciated.

1

There are 1 answers

3
Happy Bird On BEST ANSWER
  1. Create a class called ExtensionMethods
  2. Add the following method to this class

    public static void WaitForPageToLoad(this IWebDriver driver) { TimeSpan timeout = new TimeSpan(0, 0, 30); WebDriverWait wait = new WebDriverWait(driver, timeout);

    IJavaScriptExecutor javascript = driver as IJavaScriptExecutor;
    if (javascript == null)
        throw new ArgumentException("driver", "Driver must support javascript execution");
    
    wait.Until((d) =>
    {
        try
        {
            string readyState = javascript.ExecuteScript(
            "if (document.readyState) return document.readyState;").ToString();
            return readyState.ToLower() == "complete";
        }
        catch (InvalidOperationException e)
        {
            //Window is no longer available
            return e.Message.ToLower().Contains("unable to get browser");
        }
        catch (WebDriverException e)
        {
            //Browser is no longer available
            return e.Message.ToLower().Contains("unable to connect");
        }
        catch (Exception)
        {
            return false;
        }
    });
    

    }

This code will wait until all JavaScript is loaded.

  1. Use this method before you interact with the elements on the page