TestCafe如何全局处理错误

How Do I Handle Errors Globally in TestCafe

我已经用打字稿为我的 TestCafe 项目建立了一个页面对象模型。我发现每个方法中 try/catch 的重复性质都有些矫枉过正。我总是希望用 winston 记录错误。

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

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/ 了解现有记者的工作方式也很有用。请查看默认的 testcafe-reporter 如何显示错误:https://github.com/DevExpress/testcafe-reporter-spec/blob/fac1fa6d2bfae5e51cd076f990abb6d889ee9747/src/index.js#L87 您可以通过将自己的错误处理逻辑添加到 reportTestDone 方法来修改现有报告器。