具有延迟增压的量角器

protractor with deferred boostrapping

本文关键字:量角器 延迟      更新时间:2023-09-26

我正在尝试为我们的角度应用程序设置端到端测试,但遇到了一些障碍。

一是引导。我们正在使用这个库:角度延迟引导来引导我们的应用程序。该库允许我们进行http调用,然后将其结果注入到我们的应用程序中(作为角度value() )。然后,它调用 bootstrap 函数以实际使用 angular 引导应用。我希望测试在此提升完成后运行。

这就是我到目前为止所做的

describe('navigation should', function () {
    beforeEach(function () {
        // load homepage
        browser.get('/');
    }, 10000);
    it('show side navigation', function () {
        browser.wait(function () {
            var deferred = protractor.promise.defer();
            element(by.css('body.deferred-bootstrap-loading')).isPresent()
                .then(function (isPresent) {
                    deferred.fulfill(!isPresent);
                });
            return deferred.promise;
        });
    });
});

图书馆方便地将deferred-bootstrap-loading类放在body上。我正在等待,直到它被删除。

问题是有时我收到错误Error while waiting for Protractor to sync with the page: "[ng:test] no injector found for element argument to getTestability'nhttp://errors.angularjs.org/1.4.8/ng/test".

它似乎比(测试)通过更频繁地产生此错误。

我不明白这里有什么问题?量角器是否在角有机会运行之前运行?

browser.wait返回承诺时,我是否必须在回调中运行所有测试?

此外,我希望此代码为每个测试运行(等待引导完成)。组织它的最佳方法是什么?

我要做的是关闭同步,直到满足预期条件

beforeEach(function () {
    var EC = protractor.ExpectedConditions;
    browser.ignoreSynchronization = true;
    browser.get('/');
    // wait for deferred bootstrap body to not be present
    var body = $("body.deferred-bootstrap-loading");
    browser.wait(EC.stalenessOf(body), 10000).then(function () {
        browser.ignoreSynchronization = false;
    });
    browser.waitForAngular();  // might not be needed
});

此外,我希望此代码为每个测试运行(等待引导完成)。组织它的最佳方法是什么?

请看:

  • 如何在Jasmine JS中重用beforeEach/afterEach?
  • 使用 Jasmine 重用测试代码的好方法是什么?

当您使用手动/半手动引导机制时,我不会使用browser.get(),因为这会等待加载 Angular。请改用browser.driver.get(),并在之后对deferred-bootstrap-loading类添加等待。请参阅从 Angular 设置受测系统。

因此,如下所示的内容应该可以解决问题:

describe('navigation should', function () {
    beforeEach(function (done) {
        // load homepage
        browser.driver.get('/');
        browser.driver.wait(function () {
            return browser.driver.isElementPresent(by.class('body.deferred-bootstrap-loading'))
                .then(function (el) {
                    return el === false;
                });
        }).then(done);
    }, 10000);
    it('show side navigation', function () {
        // normal test here
    });
});

我没有测试过上面的内容,但你应该得到图片。通过使 beforeEach 异步并等待类从 body 标记中消失,您可以期望在运行 it 块时一切都在运行。