为什么我的TestCafe测试用例会被重复执行两次?
TestCafe测试重复运行两次问题解决方案
通用排查步骤
- 检查测试启动命令:确认没有加
--retries/-r重试参数,没有开启--live热重载模式,上述配置都会触发测试自动重跑 - 检查TestCafe配置文件(.testcaferc.json):确认
testExecutionAttempts参数值为1,该参数大于1时会在测试失败后自动重试 - 检查测试页面逻辑:若页面存在初始化自动跳转、未处理的系统弹窗,会被TestCafe判定为加载异常,触发自动重试
- 检查代码语法完整性:你提供的测试代码存在两处明显问题,会触发运行器异常导致重复执行
代码问题修正
- MainApp.ts缺少
ClientFunction导入,测试运行时会抛出未定义异常,修正后代码:
import { ClientFunction, t } from 'testcafe'; const getUrl = ClientFunction(() => window.location.href); class MainApp { async login(url:string) { await t.expect(getUrl()).eql(url); } } export default new MainApp();
- Test.ts存在两个错误:test块未闭合、
disablePageCaching传参错误,修正后代码:
import { Selector, t } from "testcafe"; import MainApp from "./MainApp"; const testUrl = `https://www.google.com/`; fixture("fixture") .page(testUrl) .beforeEach(async () => { console.log("test"); }) .disablePageCaching(); // 该方法无需传入参数 test("Test", async () => { console.log("Starting test"); await MainApp.login(testUrl); }); // 补全test块闭合符号
额外验证方法
你可以在启动命令中添加--skip-js-errors参数临时屏蔽页面JS异常干扰,运行后确认是否还会重复执行,若仍然重复可以直接指定单浏览器运行,避免多浏览器实例同时执行导致的重复运行错觉。
内容的提问来源于stack exchange,提问作者Puzzle234
相关产品推荐
相关产品推荐

