Selenium - follow fast loading indicator

2019-07-31 08:23发布

问题:

I need to test a data table, which on every change has a loading indicator. Sometimes this indicator is very fast. Is there any best practice for selenium in order to follow these very fast UI changes? (Such as loading indicator).

回答1:

I use the following c# to query the dom for id's and classes with known loading values and wait for them to not be present. Using driver.FindElement(By*) attempts to declare the element is present at a given identifier rather than searching for it, which will cause errors and isn't the behavior we're looking for.

public IWebDriver WaitForPage()
    {// Query document for loading elements until none found
        var wait = new WebDriverWait(driver,MyDefaultTimeout)
        .Until( 
            e => ((IJavaScriptExecutor)e) 
                .ExecuteScript(@"
                    var MyDefaultTimeout = 500; 
                    var loaded = false;
                    do {
                        var elementClasses = document.getElementsByClassName('//*[contains(@class=\'is-loading\') or contains(@class=\'fa-gear\') or contains(@class=\'loading\') and not(@class=\'displayed\')]');
                        var elementIds = document.getElementById('//*[contains(@id=\'hover-gear\') or contains(@id=\loading-gear\')]');
                        if((!elementClasses === null) || (!elementIds === null)){
                            setTimeout(function() { loaded = false }, MyDefaultTimeout);
                            }
                        else{ 
                            if(!document.readyState === 'complete'){
                                setTimeout(function() { loaded = false }, MyDefaultTimeout);
                                }
                            else{ 
                                loaded = true;
                                return document.readyState;
                                }
                            }
                        }
                    while(loaded === false);", null))
                .Equals("complete");
        return driver;
    }