detox

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Detox

Detox

Detox is designed for React Native. Unlike Appium (Black-box), Detox works "Gray-box" by running inside your app, monitoring the main thread. It knows when the app is busy/idle, eliminating flaky waits.
Detox是专为React Native设计的测试工具。与Appium(黑盒测试工具)不同,Detox采用“灰盒”模式运行——它在应用内部运行,监控主线程。它能知晓应用何时处于忙碌/空闲状态,从而消除不稳定的等待操作。

When to Use

适用场景

  • React Native Apps: The gold standard for RN E2E.
  • Speed & Stability: Much less flaky than Appium for RN because of synchronization.
  • CI/CD: Designed to be fast enough for CI.
  • React Native应用: 是React Native端到端测试的黄金标准。
  • 速度与稳定性: 由于具备同步机制,相比Appium,Detox在React Native测试中的稳定性大幅提升,极少出现不稳定情况。
  • CI/CD: 其设计足够高效,可适配持续集成/持续部署流程。

Quick Start

快速开始

javascript
describe("Example", () => {
  beforeAll(async () => {
    await device.launchApp();
  });

  it("should show hello screen after tap", async () => {
    await element(by.id("hello_button")).tap();
    await expect(element(by.text("Hello!!!"))).toBeVisible();
  });
});
javascript
describe("Example", () => {
  beforeAll(async () => {
    await device.launchApp();
  });

  it("should show hello screen after tap", async () => {
    await element(by.id("hello_button")).tap();
    await expect(element(by.text("Hello!!!"))).toBeVisible();
  });
});

Core Concepts

核心概念

Synchronization

同步机制

Detox monitors network requests, animations, and timers. It waits for the app to go "Idle" before executing the next command. No
sleep()
needed.
Detox会监控网络请求、动画和计时器。它会等待应用进入“空闲”状态后再执行下一条命令,无需使用
sleep()
函数。

Matchers

匹配器

by.id()
,
by.text()
,
by.label()
. Needs
testID
props on React Native components.
by.id()
,
by.text()
,
by.label()
。需要在React Native组件上添加
testID
属性。

Best Practices (2025)

2025年最佳实践

Do:
  • Add
    testID
    props
    : Add them to all interactive elements in your React Native code.
  • Use
    device.reloadReactNative()
    : Faster than relaunching the whole app for every test.
  • Mock Metro: Mock JS bundles to avoid network flakes in CI.
Don't:
  • Don't run animations: Disable them in the test build for speed and stability.
  • Don't combine with Appium: Choose one for your project.
建议
  • 添加
    testID
    属性
    : 为React Native代码中所有可交互元素添加
    testID
    属性。
  • 使用
    device.reloadReactNative()
    : 相比每次测试都重新启动整个应用,该方法速度更快。
  • Mock Metro: 在CI环境中Mock JS包,避免网络问题导致的测试不稳定。
不建议
  • 不要运行动画: 在测试构建中禁用动画,以提升速度和稳定性。
  • 不要与Appium混用: 为项目选择其中一种工具即可。

References

参考资料