刚开始使用 e2e,我想在 Angular 应用程序中测试登录成功案例。所以这是我的测试用例
it("when login is successful — user object should be set in storage", () => {
loginPage.fillCredentials();
// wait until redirected to new lage
const EC = protractor.ExpectedConditions;
const elm = element(by.css(".logo-text"));
browser.wait(EC.presenceOf(elm), 40000);
expect(loginPage.getLocalStorageObject("currentUser")).toBeDefined();
const currentUser = JSON.parse(loginPage.getLocalStorageObject("currentUser"));
expect(currentUser.CarrierId).toBeDefined();
});
和
fillCredentials(credentials: any = this.credentials): any {
this.getFormField("email").sendKeys(credentials.userName);
this.getFormField("password").sendKeys(credentials.password);
element(by.css("button[aria-label='Login']")).click();
}
getLocalStorageObject(key): any {
browser.executeScript(`return window.localStorage.getItem('${key}');`);
}
我提供了正确的用户凭据,然后点击登录按钮。
在应用程序中,当使用正确的凭据完成登录时,它会设置currectUser
对象localStorage
并重定向到从登录页面返回的页面。
但是我的测试用例失败了
when login is successful — user object should be set in storage
- Expected undefined to be defined.
我正在使用这样的等待功能it
const EC = protractor.ExpectedConditions;
const elm = element(by.css(".logo-text"));
browser.wait(EC.presenceOf(elm), 40000);
其中.logo-text
class 是重定向页面上存在的元素。
这是正确的方法还是需要做其他事情?