-1

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.

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • I have used document.readystate but it is coming 'complete' but controls are not loaded on page because of ajax request is not completed. – Lokesh Gupta Dec 22 '16 at 14:38
  • Use `Google` to get more info about what is `ExplicitWait` and how `ExpectedConditions` + `WebdriverWait` works... Also provide more info, like `HTML`, programming language you use (add appropriate tag), piece of your code – Andersson Dec 22 '16 at 14:41
  • Where is your code, what have you tried? – Neo Dec 22 '16 at 14:42

1 Answers1

-1
  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
Happy Bird
  • 1,012
  • 2
  • 11
  • 29
  • This code only waits until all static resources are loaded from the server (initiated by the url/html, but not JS), and not until all JS finishes background requests. – skandigraun Dec 24 '16 at 21:56
  • @skandigraun You're wrong and you should consider to remove the downvotes. – Happy Bird Dec 25 '16 at 22:38
  • The [specification](https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState) talks only about resources, not extra requests after firing the `load` event.[This](http://stackoverflow.com/questions/6190762/is-there-a-definitive-javascript-not-jquery-method-for-checking-whether-or-not) question might be also useful. The suggested solution actually is just a "busy sleep", selenium does not return nor allows interaction with the page until the page is loaded.Jquery has an easy feature to count/check ajax requests, or overriding XMLHttpRequest.readyState events could be also a solution. – skandigraun Dec 26 '16 at 06:50