robert.bo.roth
robert.bo.roth

Reputation: 1353

Wait for page redirect to evaluate `expect()` statement

So, I'm trying to script a login form for automation through protractor, but running into some problems when I'm trying to verify the cookies after the page redirect.

Here's my sample code:

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 ([email protected])
     */
    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');

    });
});

It's been about 3 or 4 months since I created my first Protractor tests, so I'm re-learning all the new syntax, etc. I'm currently under the impression that using waitsFor and methods like that aren't really encouraged (or supported) anymore, so I'm wondering how someone could go about scripting something like this.

Upvotes: 0

Views: 1806

Answers (1)

robert.bo.roth
robert.bo.roth

Reputation: 1353

In order to get protractor to wait for the element to appear, you have to use the following syntax:

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

Took me a while to figure this out, hope it ends up helping someone :D

Upvotes: 3

Related Questions