量角器中的ajax请求的一般解决方案

By simon at 2018-02-07 • 0人收藏 • 40人看过

我想等到ajax通话完成。下面我写了一般的方法。但它 似乎是不行的。 当我跑呼叫功能e非常afterajax的要求,是永远是真实的。 在量角器中,有没有解决方法?或者我犯了什么错误? 谢谢你或

    module.waitUntilJqueryLoad = async function (timeout) {
    var isDone = false;

    timeout = timeout || 60;

    await browser.sleep(1000);
    //Wait for jQuery to load
    var isLoaded = await browser.driver.executeScript("return jQuery.active == 0;");

    if(isLoaded) {
        console.log("JQuery is Ready!");

        return await browser;
    }

    //This loop will rotate for 60 times to check If page Is ready after every 1 second.
    //You can replace your value with 60 If you wants to Increase or decrease wait time.
    for (var i = 0; i < timeout; i++) {
        try {
            await browser.sleep(1000);
        } catch (err) {}

        //To check page ready state.
        var isLoaded = await browser.driver.executeScript("return jQuery.active == 0;");
        console.log(isLoaded);

        if(isLoaded) {
            console.log("JQuery is Ready");
            isDone = true;
        } else {
            console.log("JQuery is NOT Ready !!");
        }

        if(isDone)
            break;
    }

    return browser;
};

1 个回复 | 最后更新于 2018-02-07
2018-02-07   #1

我有一个工作。如果您的加载弹出式添加了显示样式, 将工作。 这不是一个通用的解决方案,但其他张贴解决方案甚至没有工作 下面的代码。

await browser.wait(until.invisibilityOf(element(by.id('loadingPanel'))), 60000);
用法示例:
element(by.id('selectPerson')).waitForInvisibilityOf(10000); // wait 10 seconds
这是我的解决办法;
protractor.ElementFinder.prototype.waitForInvisibilityOf = async function (timeout) {
var _debug = true;
var isDone = false;
timeout = timeout || 60000;
var seconds = timeout / 1000;

if(await !this.isPresent())
    return this;

//This loop will rotate for 60 times to check If page Is ready after every 1 second.
//You can replace your value with 60 If you wants to Increase or decrease wait time.
for (var i = 1; i <= seconds; i++) {
    await browser.sleep(1000);

    var style = await this.getAttribute('style');
    var insibilityOf = await style.includes('display: none;');
    var visibilityOf = await style.includes('display: block;');

    if(insibilityOf) {
        if(_debug)
            console.log(i + " second: Element invisible!");
        isDone = true;
    }
    else {
        if(_debug)
            console.log(i + " second: Element NOT invisible!");
    }

    if(seconds === i)
        throw "Element invisibility timed out after "+ timeout +" milliseconds";

    if(!insibilityOf && !visibilityOf && i > 10) // break for paging is loaded
        break;

    if(isDone) // If element is invisible
        break;
}

return await this; };

登录后方可回帖

Loading...