如何解决在CodeceptJS中自动登录的自定义步骤中管理错误
我正在autoLogin项目中使用CodeceptJS插件,并将Puppeteer用作测试库。
这是我第一个使用CodeceptJS的项目,autoLogin工作流工作正常,但是我没有使用应用程序页面(UI)登录或检查当前令牌,而是直接使用REST API调用来进行所有处理快一点。
autoLogin: {
enabled: true,savetoFile: true,inject: 'login',users: {
admin: {
login: async (I) => {
I.amOnPage('/login');
await I.loginWithCredentials(adminUser.username,adminUser.password);
},check: (I) => {
const token = codeceptjs.store['admin_session'];
I.validatetoken(token);
},fetch: async (I) => {
const cookie = await I.grabCookie('token');
return cookie.value;
},restore: (I,sessionToken) => {
I.amOnPage('/login');
I.savetokenData(sessionToken);
}
},}
}
步骤savetokenData()
,validatetoken()
和loginWithCredentials()
是用actor()
定义的自定义步骤,例如:
module.exports = function() {
return actor({
async validatetoken(token) {
let response = await this.sendGetRequest(`/api/session/validate?token=${token}`);
if (response.status === 200) {
if (response.data === true) {
return true;
}
}
throw new Error('Invalid token !!');
}
});
第throw new Error('Invalid token !!');
行,它在工作流中生成“意外”错误,并在日志中显示此行:
(node:5986) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
我尝试了几种方法,例如使用recorder.add()
(将错误抛出记录器),我也尝试使用链接的promise(没有async
关键字)和assert
lib与assert.fail()
在一起,但总是相同的问题。
如何在没有奇数条日志消息的情况下将“自定义”步骤标记为失败?我只想在autoLogin中实现check
步骤,而无需退出API来验证用户界面({{1} },I.see()
,I.seeElement()
...)
在使用更好的解决方案之前,我一直在使用一个丑陋的解决方法,自定义步骤返回一个布尔值,而不会引发任何错误,并且在I.dontSee()
代码中,我已经做到了:
check
我在CodeceptJS GitHub项目#2600中打开了一个问题
解决方法
如何使用assert.fail方法?它将使整个方案失效,您可以自定义消息!
const assert = require("assert");
module.exports = function() {
return actor({
async validateToken(token) {
let response = await this.sendGetRequest("/api/session/validate?token=${token}");
if (response.status !== 200 || response.data !== true) {
assert.fail("Invalid token!");
}
}
});
检查方法:
check: async (I) => {
console.log('admin.check');
const token = codeceptjs.store['admin_session'];
await I.validateToken(token);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。