I'm doing an end-to-end testing using protractor on an angular 2 project and i'm required to test a login operation. The problem here is that when login button is clicked the expect statement still gets the login page's URL as supposed to home page. If I wait, then the test succeeds. I have read somewhere in the documentation that protractor waits for processes to complete automatically, so,
- What is causing this error?
- How can I perform the operation correctly without explicitly using
wait()
method? - Does protractor have something similar to
fakeAsync
,async
or atleastwhenStable
like in unit test?
Here's my test case
it("User successfully login",()=>{
browser.get(browser.baseUrl + '/account/login').then(() => {
return expect(browser.getCurrentUrl()).toBe(browser.baseUrl + "/account/login")
}).then(()=>{
element(by.tagName('input[type=text]')).sendKeys('username');
element(by.tagName('input[type=password]')).sendKeys('password');
element(by.css('button[type=submit]')).click().then(()=>{
return expect(browser.getCurrentUrl()).toBe(browser.baseUrl + "/home");
});
});
});
Note:
- I have instructions to avoid using
browser.wait()
method. - Login button sends an http request to the backend via API.
Workaround solution [Based on Ernst's solution]:
I sort of solved this problem. I don't know if it works for other scenarios or if it's the correct approach. I added
browser.wait(EC.urlContains(browser.baseUrl + "/account/login"), 10000);
browser.waitForAngularEnabled(false);
browser.wait(EC.urlContains(browser.baseUrl + "/home");
just after click() event and it seems to work for now.