Appium-CodeceptJS-Gherkin测试脚本执行异常及配置问题求助
CodeceptJS + Appium 测试问题解决方案
问题1:执行npx codeceptjs run未执行step_definitions目录下的文件
原因
- 配置文件中
gherkin.steps仅指定了home-screen-steps.js,未包含add-task-steps.js,导致该文件的步骤未被加载 add-task-steps.js存在代码错误:- 未引入
AddTaskScreen模块 - 代码顺序错误(
inject()调用、模块引用位置不符合规范)
- 未引入
解决方案
- 修改配置文件中的
gherkin.steps,包含所有步骤文件:gherkin: { features: './features/*.feature', steps: ['./step_definitions/home-screen-steps.js', './step_definitions/add-task-steps.js'] // 也可使用glob批量匹配:'./step_definitions/**/*.steps.js' }, - 修复
add-task-steps.js的代码错误:const { I } = inject(); const AddTaskScreen = require("../screens/add-task-screen.js"); // 补充引入对应屏幕模块 const HomeScreen = require("../screens/home-screen.js"); When("I input all information", () => { AddTaskScreen.inputTaskName('sdsada'); });
问题2:使用/*steps.js配置步骤定义无效,必须指定精确路径
原因
Windows系统下,CodeceptJS的glob路径匹配需要使用相对路径+递归匹配模式,/*steps.js是根目录匹配规则,无法正确定位到step_definitions目录下的文件。
解决方案
使用正确的glob模式匹配所有步骤文件:
gherkin: { features: './features/*.feature', steps: './step_definitions/**/*.steps.js' },
**表示递归匹配所有子目录(若有多层目录结构)*.steps.js匹配所有以.steps.js结尾的文件
若你的步骤文件命名规则不同,可调整glob模式适配实际文件名。
问题3:应用崩溃但未抛出异常,脚本仍持续执行
原因
Appium默认不会主动检测应用崩溃状态,若CodeceptJS步骤未包含断言或状态检查逻辑,会继续执行后续操作。
解决方案
- 在Appium helper中启用崩溃自动重启,并配置状态检查:
helpers: { Appium: { app: 'Appium/ToDoList.apk', platform: 'Android', device: 'emulator-5556', autoRestart: true, // 应用崩溃后自动重启 disableDeviceTimeout: false } }, - 在关键步骤后添加元素存在断言,判断应用是否正常运行:
Given("I click Add Task button", () => { HomeScreen.tapAddTaskButton(); // 断言目标元素存在,若不存在则抛出错误终止脚本 I.waitForElement(AddTaskScreen.taskNameInput, env.TIMEOUT); }); - 启用
pauseOnFail插件,在步骤失败时暂停脚本,方便排查状态:plugins: { pauseOnFail: { enabled: true, pause: 5000 // 失败后暂停5秒 } },
内容的提问来源于stack exchange,提问作者Hoang Nguyen
相关产品推荐
相关产品推荐

