关于 detox
面向 React Native 应用的 Detox 端到端测试技能,涵盖模拟器配置、测试编写、CI 运行及同步问题排查。
主要功能
- 01配置 iOS 和 Android 的 Detox
- 02使用匹配器和断言编写测试
- 03构建并运行端到端测试
- 04收集失败产物以便调试
- 05排查同步与时序问题
适用场景
- 01为 React Native 应用配置 Detox
- 02在模拟器或仿真器上测试登录流程
- 03在 CI 中运行移动端端到端测试
- 04排查不稳定测试和时序问题
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ partme-ai/ full-stack-skills --skill detox安装后在 Claude Code 中输入/detox确认已加载。
附带文件LICENSE.txt
详细介绍
适用场景
在用户希望执行以下操作时使用此技能:
- 使用 Detox 为 React Native 应用编写端到端测试
- 为 iOS 模拟器和 Android 模拟器配置 Detox
- 在 Detox 测试中使用元素匹配器、操作和断言
- 将 Detox 测试集成到 CI/CD 流水线
- 调试移动端测试中的同步和时序问题
使用方法
工作流
- 设置环境:安装 Detox CLI,配置
.detoxrc.js,确保 Xcode/Android SDK 已就绪 - 编写测试用例:使用
element(by.id(...))、tap()、typeText()和expect() - 构建并测试:先运行
detox build,再运行detox test - 在 CI 中运行:失败时保存构建产物(日志、屏幕截图)
1. 配置(.detoxrc.js)
module.exports = {
testRunner: { args: { config: 'e2e/jest.config.js' } },
apps: {
'ios.debug': {
type: 'ios.app',
binaryPath: 'ios/build/MyApp.app',
build: 'xcodebuild -workspace ios/MyApp.xcworkspace -scheme MyApp -configuration Debug -sdk iphonesimulator',
},
'android.debug': {
type: 'android.apk',
binaryPath: 'android/app/build/outputs/apk/debug/app-debug.apk',
build: 'cd android && ./gradlew assembleDebug',
},
},
devices: {
simulator: { type: 'ios.simulator', device: { type: 'iPhone 15' } },
emulator: { type: 'android.emulator', device: { avdName: 'Pixel_6' } },
},
configurations: {
'ios.sim.debug': { device: 'simulator', app: 'ios.debug' },
'android.emu.debug': { device: 'emulator', app: 'android.debug' },
},
};
2. 测试示例
describe('Login Flow', () => {
beforeAll(async () => {
await device.launchApp();
});
it('should login successfully', async () => {
await element(by.id('username-input')).typeText('testuser');
await element(by.id('password-input')).typeText('secret');
await element(by.id('login-button')).tap();
await expect(element(by.id('welcome-message'))).toBeVisible();
});
});
3. 运行测试
# Build the app
detox build --configuration ios.sim.debug
# Run tests
detox test --configuration ios.sim.debug
# With artifacts on failure
detox test --configuration ios.sim.debug --artifacts-location ./artifacts
最佳实践
- 使用
testID属性稳定地识别元素;避免根据文本或动画进行匹配 - 确保本地开发和 CI 中的构建及设备配置保持一致
- 失败时保存构建产物(日志、屏幕截图、视频),以便调试
- 对敏感操作使用环境变量;配置重试和并行化策略
- 在测试构建中禁用动画,以避免同步问题
资源
关键词
detox、React Native、E2E、端到端测试、移动端测试、iOS 模拟器、Android 模拟器、testID、同步、Wix Detox
评论
支持 Markdown