如何等待页面测试加载到非角度站点?

时间:2015-10-23 11:42:40

标签: protractor wait

我试过这个:

 browser.wait(function () {
        return browser.executeScript('return document.readyState==="complete" &&' +
            ' jQuery !== undefined && jQuery.active==0;').then(function (text) {
                return text === true;
            });
    }, 30000);

如果jQuery.active==0则页面已完全加载。这适用于具有JQuery和非角度页面的站点。

然而,我有很多不稳定的问题来测试非角度位置。

如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

默认情况下,量角器会一直等到页面完全加载。如果您遇到任何错误,那么这是因为量角器正在等待您完成的默认时间,您在conf.js文件中指定等待页面加载。如果您认为应用程序运行缓慢,请将值更改为等待更长时间 -

// How long to wait for a page to load.
getPageTimeout: 10000, //Increase this time to whatever you think is better

您还可以增加defaultTimeoutInterval以使量角器在测试失败前等待一段时间 -

jasmineNodeOpts: {
  // Default time to wait in ms before a test fails.
  defaultTimeoutInterval: 30000
},

如果您想等待任何特定元素,则可以使用wait()函数执行此操作。可能等待最后一个元素加载是测试它的最佳方法。这是如何 -

var EC = protractor.ExpectedConditions;
var lastElement = element(LOCATOR_OF_LAST_ELEMENT);
browser.wait(EC.visibilityOf(lastElement), 10000).then(function(){ //Alternatively change the visibilityOf to presenceOf to check for the element's presence only
    //Perform operation on the last element
});

希望它有所帮助。

答案 1 :(得分:2)

我使用ExpectedConditions等待,并验证页面加载。我walk through it a bit on my siteexample code on GitHub。这就是要点......

基页:(由所有页面对象扩展)

// wait for & verify correct page is loaded
this.at = function() {
    var that = this;
    return browser.wait(function() {
        // call the page's pageLoaded method
        return that.pageLoaded();
    }, 5000);
};

// navigate to a page 
this.to = function() {
    browser.get(this.url, 5000);
    // wait and verify we're on the expected page
    return this.at();
};
...

页面对象:

var QsHomePage = function() {
    this.url = 'http://qualityshepherd.com';

    // pageLoaded uses Expected Conditions `and()`, that allows us to use
    // any number of functions to wait for, and test we're on a given page
    this.pageLoaded = this.and(
        this.hasText($('h1.site-title'), 'Quality Shepherd')
    ...

};
QsHomePage.prototype = basePage; // extend basePage
module.exports = new QsHomePage();

页面对象可能包含一个url(如果可以直接访问),以及一个pageLoaded属性,它返回我们用来证明页面已加载的ExepectedCondition函数(以及右页)。

<强>用法:

describe('Quality Shepherd blog', function() {
    beforeEach(function() {
       // go to page
       qsHomePage.to();
    });

    it('home link should navigate home', function() {
        qsHomePage.homeLink.click();

        // wait and verify we're on expected page
        expect(qsHomePage.at()).toBe(true);
    });
});

调用at()调用ExpectedCondidion(可以是and()or()等等)。

希望这会有所帮助......

相关问题