module.exports.pollForEvents = function()

in lib/appium/helpers/wdHelper.js [202:263]


module.exports.pollForEvents = function (driver, platform, skipBuster, windowOffset, retries) {
    const isAndroid = platform === utilities.ANDROID;
    const isBrowser = platform === utilities.BROWSER;
    const isIOS = platform === utilities.IOS;

    if (retries === undefined || retries === null) retries = 2;
    if (!windowOffset) windowOffset = 0;

    // polling for new events
    return driver
        .sleep(0)
        .then(function () {
            if (skipBuster) return driver;
            return driver.bustAlert(platform);
        })
        .then(function () {
            if (isIOS || isBrowser) return driver;

            // for some reason inappbrowser tests tend to leave an active window on android
            // so for the polling to work correctly we need to
            // switch back to the window where the cache is located
            return driver
                .windowHandles()
                .then(function (windowHandles) {
                    if (windowOffset >= windowHandles.length) {
                        throw new Error('Cannot find a window with the event cache.');
                    }
                    return driver.window(windowHandles[windowOffset]);
                });
        })
        .execute(function () {
            // wrong window
            if (typeof window._jasmineParamedicProxyCache === 'undefined') return null;

            // get the results and clean up the cache
            const result = window._jasmineParamedicProxyCache;
            window._jasmineParamedicProxyCache = [];
            return JSON.stringify(result);
        }, [])
        .then(function (result) {
            if (result) {
                result = JSON.parse(result);
            }
            // found
            if (Object.prototype.toString.call(result) === '[object Array]') return result;

            // not found
            if (isBrowser && retries > 0) {
            // the odds are that we're hitting "bad gateway" error on Sauce, refreshing the page should fix it
                return driver
                    .get('http://localhost:8000/cdvtests/index.html')
                    .then(function () {
                        return module.exports.pollForEvents(driver, platform, skipBuster, windowOffset, retries - 1);
                    });
            }
            if (!isAndroid) {
                throw new Error('Cannot get the event cache: it doesn\'t exist in the app. Got this instead: ' + result);
            }
            // no luck finding the event cache in this window, let's try next
            return module.exports.pollForEvents(driver, platform, skipBuster, windowOffset + 1);
        });
};