我有一个非常简单的量角器测试,它可以进入我的网站,登录并获取菜单按钮的文本,该菜单按钮的文本应该以用户名作为按钮的内文。不幸的是,当我调用getText()时,即使我尝试通过.then函数手动解决承诺,承诺也无法解决(我将默认超时设置为5分钟)。测试代码如下:
describe('workspace-project App', () => {
let page: AppPage;
const secretsManagerService: SecretsManagerService = new SecretsManagerService();
let username: string;
let password: string;
let getSecretCallback = async (err: AWSError, data: GetSecretValueResponse) => {
if(err) {
throw err;
}
data.SecretString;
}
beforeEach((done) => {
page = new AppPage();
let request: AWS.Request<SecretsManager.Types.GetSecretValueResponse, AWSError> = secretsManagerService.getSecret("/dev/ui/test/username");
request.send();
request.promise().then((data) => {
username = data.SecretString;
request = secretsManagerService.getSecret("/dev/ui/test/password");
request.send();
request.promise().then((data) => {
password = data.SecretString;
done();
})
})
});
it('should login', async () => {
await page.navigateTo();
await page.navigateToLogin();
await page.typeUsername(username);
await page.typePassword(password);
await page.submit()
// page.getUsername().then((res) => {
// console.log("THIS IS A TEST")
// })
let actualUsername = await page.getUsername();
expect(actualUsername).toEqual(username);
});
afterEach(async () => {
// Assert that there are no errors emitted from the browser
const logs = await browser.manage().logs().get(logging.Type.BROWSER);
expect(logs).not.toContain(jasmine.objectContaining({
level: logging.Level.SEVERE,
} as logging.Entry));
});
});
正如您在注释代码中看到的那样,我试图解决Promise并记录一条调试语句,但从未记录过任何语句。 该应用程序的页面代码为:
export class AppPage {
navigateTo() {
return browser.get(browser.baseUrl) as Promise<any>;
}
getTitleText() {
return element(by.css('title')).getWebElement().getText() as Promise<string>;
}
navigateToLogin() {
return element(by.id("loginButton")).getWebElement().click()
}
typeUsername(username: string) {
return element(by.id("username")).sendKeys(username);
}
typePassword(password: string) {
return element(by.id("password")).sendKeys(password);
}
submit() {
return element(by.id("loginSubmit")).getWebElement().click();
}
//the promise returned here never resolves
getUsername() {
return element(by.id('userMenu')).getText();
}
}
##编辑1切换为异步/等待