TestCafe测试步骤乱序执行且断言失效问题咨询
TestCafe测试步骤乱序、断言失效原因分析
核心问题
你封装的consentPageSteps里所有验证方法都是异步函数,但调用时没有加await关键字,这是导致问题的根本原因。
具体原因拆解
- 硬等待掩盖了异步逻辑漏洞:添加
await t.wait(1000)时,测试线程被强制暂停,给那些未加await的异步方法留出了足够时间完成执行,看起来步骤是按顺序运行的,但这只是硬等待带来的假象,并非正确的异步处理方式。 - 无等待时异步任务并行触发:移除硬等待后,所有
consentPageSteps.xxx()会被立即触发,但这些异步方法的实际执行顺序由浏览器事件循环和操作耗时决定,自然会出现乱序。更关键的是,这些未被await的异步方法里的断言失败,会被包裹在未处理的Promise中,TestCafe无法捕获到这些错误,所以就算断言返回false,测试也会误以为全部通过。 - 代码逻辑验证:看你的示例代码,所有
consentPageSteps的方法调用都没加await,而TestCafe的内置操作、断言API本身都是异步的,你封装的页面步骤方法如果依赖这些API,必然也是异步的,必须用await等待执行完成。
修复方法
给每个consentPageSteps的方法调用前加上await,修改后的代码示例:
test .page(page)('Submit Button Behaviors', async () => { await t.addRequestHooks(mocks.consent(url)); await t.addRequestHooks(mocks.sc(url)); await consentPageSteps.verify_the_header_is_displayed(); await consentPageSteps.verify_the_previous_button_is_displayed(); await consentPageSteps.verify_user_can_select_all_consent_checkboxes(); await consentPageSteps.verify_the_consent_submit_button_is_active(); await consentPageSteps.verify_user_can_deselect_all_consent_checkboxes(); await consentPageSteps.verify_the_consent_submit_button_is_disabled(); });
内容的提问来源于stack exchange,提问作者L M Salopek
相关产品推荐
相关产品推荐

