简体   繁体   中英

Wait until condition is met Selenium-Webdriver

I am trying to create additional conditions on top of the selenium-webdriver conditions. I am still trying to grasp the complete concept of promises and callbacks. I have drastically improved thanks to the help of stack overflow. I am unsure of how to repeat a condition every so many seconds in javascript. My inheritance may be wrong as well.

var WebElementCondition = require('./selenium-webdriver/lib/until');

var ExpectedConditions = function()
{
    this.waitForWindowWithTitle = function (title)
    {

        return driver.getAllWindowHandles().then(function (title, handles) {

            console.log(handles.length + ' .then function');

            for (var window in handles) {
                if (window.title === title) 
                {
                    return true;
                }
                else 
                {
                    return false;
                }
            }
        });

    }
}

module.exports = ExpectedConditions;
require('util').inherits(module.exports, WebElementCondition);

In waitForWindowWithTitle function would I do something like

return driver.wait(10000).then(function() {
     driver.getAllWindowHandles().then(function (title, handles) {

        console.log(handles.length + ' .then function');

        for (var window in handles) {
            if (window.title === title)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    });
});

You can use wait which accepts any JS function to create custom conditions. Here is an example that checks when angular is ready.

// Wait for Angular to Finish
function angularReady(): any  {
  return $browser.executeScript("return (window.angular !== undefined) && (angular.element(document).injector() !== undefined) && (angular.element(document).injector().get('$http').pendingRequests.length === 0)")
     .then(function(angularIsReady) {                        
                    return angularIsReady === true;
                  });
}

$browser.wait(angularReady, 5000).then(...);

Wait Type Definition

wait<T>(condition: promise.Promise<T> | until.Condition<T> | ((driver: WebDriver) => T) | Function, timeout?: number, opt_message?: string): promise.Promise<T>;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM