我有一个标准的用户名/密码/提交按钮表单,当用户单击该按钮时,表单会使用 ng-submit="login.submit()"
提交,该表单会进行登录并打开成功使用 ui.router 重定向到主页 ($state.go("main")
)。
以下测试失败:
describe("login", function() {
beforeEach(function() {
var email = element(by.model("login.email"));
email.clear().sendKeys("mail");
var password = element(by.model("login.password"));
password.clear().sendKeys("pass");
var submit = element(by.id("submit"));
submit.click();
});
it("should be able to login", function() {
expect(element(by.css(".loginPage")).isPresent()).toBe(false);
expect(element(by.css(".mainPage")).isPresent()).toBe(true);
});
});
如果我尝试增加等待时间,我可以看到浏览器始终停留在登录页面上(单击按钮后) - 然后我就会超时。
成功登录后,浏览器会收到一个带有 token 的 cookie,用于验证每个后续请求。
编辑:经过一些修补,我发现了它失败的地方..
function login(email, pass) {
alert("it gets here");
return _auth.post({ username: email, password: pass }).then(function(data) {
alert("does not get here");
console.log("loginok, token:" +$browser.cookies().apiToken); //this should be the received token
return data;
});
}
EDIT2:身份验证服务
var _auth = Restangular.withConfig(function(Configurer) {
Configurer.setBaseUrl("/");
}).service("auth/simple");
return {
login: login,
};
function login(email, pass) {
return _auth.post({ username: email, password: pass });
}
手动一切都会按预期进行。
最佳答案
@JoMendez 的答案非常接近,但不适用于我的情况。用过@DaveGray的here .
必须将 isPresent()
调用包装在函数中。
browser.wait(function() {
return element(by.css('.mainPage')).isPresent();
});
关于authentication - 在继续 AngularJS 测试之前, Protractor 不等待登录重定向,有什么建议吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26931937/