如何在 TestCafe 中全局处理错误

2023-12-28

我已经用打字稿为我的 TestCafe 项目构建了一个页面对象模型。我发现每种方法中 try/catch 的重复性都太过分了。我总是希望用温斯顿记录错误。

有没有建议的方法来处理全局错误?这是一个示例页面:

import { Selector, t } from "testcafe";
import logger from 'logger';

export default class DocumentQueryPage {
    path: string;
    queryDocumentsButton: Selector;

    constructor() {
        this.path = "#/records/query";

        this.queryDocumentsButton = Selector('button.btn-request-records');
    }

    async go(): Promise<void> {
        try {
            await t.navigateTo(this.path)
        }
        catch (err) {
            logger.error(err)
        }
    }

    async selectFirstQuery(): Promise<void> {
        try {
            await t.navigateTo(this.path)
        }
        catch (err) {
            logger.error(err)
        }
    }
}

我建议不要将 try/catch 块与 testcafe 内置操作一起使用。您可以实现自己的报告器或修改现有的报告器。请参考以下文章:http://devexpress.github.io/testcafe/documentation/extending-testcafe/reporter-plugin/ http://devexpress.github.io/testcafe/documentation/extending-testcafe/reporter-plugin/了解现有记者的工作方式也很有用。请查看默认的 testcafe-reporter 如何显示错误:https://github.com/DevExpress/testcafe-reporter-spec/blob/fac1fa6d2bfae5e51cd076f990abb6d889ee9747/src/index.js#L87 https://github.com/DevExpress/testcafe-reporter-spec/blob/fac1fa6d2bfae5e51cd076f990abb6d889ee9747/src/index.js#L87您可以通过添加自己的错误处理逻辑来修改现有的报告器reportTestDone method.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 TestCafe 中全局处理错误 的相关文章

随机推荐