简体   繁体   English

Selenium WebdriverJS承诺循环

[英]Selenium WebdriverJS Promise Loop

I'm trying to look for the "More" link in a container to keep clicking on it until the link is no longer there. 我试图在容器中查找“更多”链接,以保持单击状态,直到该链接不再存在。 I'm creating a deferred and returning it with the fulfill call happening once there is no longer a "More" link available. 我正在创建一个延迟的,并在不再有“更多”链接可用的情况下,通过执行调用返回它。

.then(function (previousResults) {
    var deferred = webdriver.promise.defer();

    // look for the more link, keep clicking it till it's no longer available
    browser.wait(function() {
        // see if we have "more" to click on
        browser.findElements(byMoreLinkXpath)
            .then(function (moreLinks) {
                if (moreLinks[0]) {
                    console.log('has more');
                    moreLinks[0].click()
                        .then(function() {
                            // check for spinner to go away
                            browser.wait(pageDoneLoading, configSetting.settings.testTimeoutMillis);
                        });
                } else {
                    console.log('no more');
                    deferred.fulfill(true);
                }
            });
    }, 5000);

    return deferred.promise;
})

Unfortunately, the promise is never fulfilled it simply times out. 不幸的是,这个承诺永远都无法兑现。 I tried doing a return deferred.promise; 我试图做一个return deferred.promise;return deferred.promise; in the else block and while it works for reject , it still doesn't work for fulfill . else块中,尽管它可以reject ,但是仍然不能fulfill

the syntax of webdriver.wait : webdriver.wait的语法:

wait(condition, opt_timeout, opt_message) 等待(条件,opt_timeout,opt_message)

but in your code, the first argument is neither a condition nor a promise but a function , so I would change it to: 但是在您的代码中,第一个参数既不是condition也不是promise而是function ,因此我将其更改为:

also, I think what you are doing here is promise anti-pattern( also I am not seeing the loop of checking again for more links, sorry but I think you do not completely understand driver.wait ), I would simply reduce the above function as: 另外,我认为您在此处所做的工作是promise anti-pattern(同样,我也没有看到再次检查更多链接的循环,对不起,但我认为您不完全了解driver.wait ),我只会简化上述功能如:

function exhaustMoreLinks(){
    return driver.wait( until.elementLocated(byMoreLinkXpath), 5000)
        .then(function(){
            return driver.findElement(byMoreLinkXpath);
        }).then(function(moreLink){
            console.log('more links');
            return moreLink.click();
        }).then(function(){
                return browser.wait(pageDoneLoading(), configSetting.settings.testTimeoutMillis).then(exhaustMoreLinks);
            }, function(err){
            if(err.name === 'NoSuchElementError' || (err.message.search(/timed out/i)> -1 && err.message.search(/waiting element/i) > -1) ){    // checking if error because time-out or element not found, if true, redirect to fulfil
                console.log('no more links');
                return;
            }else{
                throw err;
            }
        });
}

and usage would be like: 和用法如下:

...
.then(exhaustMoreLinks)
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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