angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

$authWithPassword requires browser.sleep with e2e tests

Open maneesht18 opened this issue 10 years ago • 9 comments

My application is using $authWithPassword, and inside the ".then" callback, I am redirecting to a different state (using ui-router).

authObj.$authWithPassword({
   email: that.email,
   password: that.password
}).then(function(data) {
   $state.go("nextState");
})

Here is part of the test:

//login clicks a button that authenticates using the code above
login().then(() => {
  browser.sleep(1000);
  expect(browser.getCurrentUrl()).toContain("next");
});

There aren't many examples regarding this online. It seems like mainly an issue with how angularfire works, however. The problem is, if I do not put in the sleep, protractor won't wait for angularfire's promise returned by the auth.

maneesht18 avatar Mar 26 '16 04:03 maneesht18

Please include a minimal repro. Can't reproduce with what you have here.

katowulf avatar Mar 28 '16 17:03 katowulf

Without the browser.sleep The test would fail.

maneesht avatar Mar 28 '16 17:03 maneesht

No repro included.

katowulf avatar Mar 28 '16 18:03 katowulf

@katowulf

this.loginWithPass = function() {
   authObj.$authWithPassword({
   email: that.email,
   password: that.password
    }).then(function(data) {
       $state.go("nextState");
    })
}

E2E

//login clicks a button that authenticates using the code above
function login() {
    //click executes login.Pass
    loginButton.click();
}
login().then(() => {
    expect(browser.getCurrentUrl()).toContain("next");
});

expect doesn't wait for the promise to finish

maneesht avatar Mar 28 '16 18:03 maneesht

This still isn't a repro. See creating an mcve. Where does loginWithPass() get called from? What versions are we working with? I can't run this.

At first glance, there's no promise involved in this process, other than the one returned by the button click. If you want it to wait for the promise, you have to involve that in some way. The promise is returned by authObj.$authWithPassword(), and since you aren't returning that (just going to a new screen) there's really no way to make your e2e test wait on it.

I suppose you could improvise by monitoring $state's events and seeing when $go() is called?

katowulf avatar Mar 28 '16 18:03 katowulf

<div ng-controller="TestController as tc">
    <button ng-click="ac.login()"></button>
</div>

JS

angular.module("test", [])
    .controller("TestController", function() {
        this.login() {
           return authObj.$authWithPassword({
           email: that.email,
           password: that.password
        }).then(function(data) {
           $state.go("nextState");
           return data;
    }
})

E2E

//login clicks a button that authenticates using the code above
function login() {
    //click executes login.Pass
    loginButton.click();
}
login().then(() => {
    expect(browser.getCurrentUrl()).toContain("next");
});

Protractor version 3.1.1 angularfire 1.1.2

maneesht avatar Mar 28 '16 19:03 maneesht

@maneesht that's not a repro. login() doesn't return anything and this won't compile. There are missing brackets. See creating an mcve. Attach a minimal repro that runs or a failing test unit as a PR for demonstration.

katowulf avatar Apr 19 '16 01:04 katowulf

[redacted; unrelated to this bug]

liveakshay avatar Apr 19 '16 01:04 liveakshay

@liveakshay your error is with failNetx() and MockFirebase and looks unrelated to this one. Additionally, this issue is closed. I've moved your question to a new issue.

katowulf avatar Apr 19 '16 01:04 katowulf