等待页面重定向来评估`expect()`语句

时间:2014-04-01 20:04:12

标签: angularjs protractor

所以,我正在尝试通过量角器编写自动登录表单,但在我尝试在页面重定向后验证cookie时会遇到一些问题。

这是我的示例代码:

describe('login', function () {
    var app;

    var LoginPage = require('./login.page.e2e.js');

    // Before each test, refresh page
    beforeEach(function () {
        LoginPage.get();

        app = element(by.css('body'));
    });


    // Check route, make sure it hasn't been redirected somewhere strange
    it('should be at path: /login', function () {
        expect(browser.getCurrentUrl()).toContain('/login');
    });


    /**
     * Login as a provider (phoenix.e2e.login.test@leadingreach.com)
     */
    it('should be able to login', function () {

        // Fill out fields
        LoginPage.populate_provider_form();

        // Login as provider
        // Clicking this button fires off an AJAX request that logs in the user, and populates a few browser cookies
        element(by.css('#provider-login-form-container #login_btn')).click();

        // These two statements work fine. They seem to wait for the redirect and end up passing.
        expect(browser.getCurrentUrl()).toContain('/dashboard');
        expect(app.evaluate('currentUser.username')).toEqual('phoenixe2elogin');

        // The following statements are executed before the page redirects, and therefore fail
        expect(!!browser.manage().getCookies().lrrt).toBe(true);
        expect(!!browser.manage().getCookies().lrco).toBe(true);
        expect(browser.manage().getCookies().lrrm).toBe('false');

    });
});

自从我创建了第一个Protractor测试以来已经有3到4个月了,所以我正在重新学习所有新语法等等。我目前的印象是使用waitsFor和类似的方法不再鼓励(或支持),所以我想知道有人可以如何编写这样的脚本。

1 个答案:

答案 0 :(得分:3)

为了让量角器等待元素出现,你必须使用以下语法:

ptor.findElement(by.id('my-elt')).then(function (elt) {
    expect(elt.evaluate('my.binding')).toBe('someValue');
});

我花了一些时间来弄清楚这一点,希望它最终帮助某人:D