Expect condition inside the .then won't execute

13 views
Skip to first unread message

Haseeb Iqbal

unread,
Apr 25, 2018, 8:44:11 AM4/25/18
to Angular and AngularJS discussion

I'm using PageObjects in Protractor tests. Structure looks like this,

  • e2e
    • specs
      • base.po.ts // Base PageObject class
      • Login
        • login.e2e-spec.ts // contains describeit blocks etc. and Expect conditions.
        • login.po.ts // Interacts with page elements

I'm returning Promises from methods inside PageObject file. And then inside spec file in it blocks i've Expect conditions.

A sample code is like,

// login.e2e-spec.ts

    it('should logout', () => {
          console.log('---- step 1 ----');
          page.logout().then(function () {
              console.log('---- step 4 ----');
              expect(page.inDom(page.getLoginButton())).toBe(true);
          });
          console.log('---- step 5 ----');
    });
// login.po.ts

public logout() {
        const that = this;
        return new Promise(function (fulfill = null, reject = null) {
            that.clickIfElementIsAvailable(that.welcomeModelCancelButtonElement);
            that.waitForVisibility(that.sideBarOpenerElement).then(function () {
                console.log('---- step 2 ----');
                that.sideBarOpenerElement.click();

                that.waitForVisibility(that.logoutButtonElement).then(function () {
                    console.log('---- step 3 ----');
                    that.logoutButtonElement.click();
                    fulfill();

                }, reject);

            }, reject);
        });
    }

After execution, all the tests pass and i get following output in log.

---- step 1 ----
---- step 5 ----
(node:2639) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property 'waitForElement' of undefined
(node:2639) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

3 specs, 0 failures

In practical i've multiple tests so control just moves to next tests and at the end tells that all tests passed successfully.

Now i understand that control is putting the commands in queue and moving forward. How can i handle this situation? What i'm doing wrong? Thanks.

Haseeb Iqbal

unread,
Apr 25, 2018, 8:44:11 AM4/25/18
to Angular and AngularJS discussion
Reply all
Reply to author
Forward
0 new messages