Detox helps you check if your app works well from the user's point of view. It runs tests that tap buttons, type text, and check screens automatically.
Detox for E2E testing in React Native
describe('Example test', () => { beforeAll(async () => { await device.launchApp(); }); it('should show welcome screen', async () => { await expect(element(by.id('welcome'))).toBeVisible(); }); });
describe groups tests.
beforeAll runs setup before tests.
it defines a test case.
device.launchApp() starts the app.
element(by.id('id')) finds UI elements by their test ID.
expect(...).toBeVisible() checks if element is visible.
await device.launchApp(); await element(by.id('loginButton')).tap();
await expect(element(by.text('Hello'))).toBeVisible();
await element(by.id('usernameInput')).typeText('user123');
This test opens the app, checks if the welcome message is visible, then taps the start button and checks if the home screen appears.
describe('Welcome screen test', () => { beforeAll(async () => { await device.launchApp(); }); it('shows welcome message', async () => { await expect(element(by.id('welcomeMessage'))).toBeVisible(); }); it('allows tapping start button', async () => { await element(by.id('startButton')).tap(); await expect(element(by.id('homeScreen'))).toBeVisible(); }); });
Make sure your app's UI elements have testID props for Detox to find them.
Run Detox tests on simulators or real devices for best results.
Detox tests run asynchronously, so use await to wait for actions to finish.
Detox automates user actions to test your app end-to-end.
Use describe, it, and expect to write tests.
Test IDs in your app help Detox find UI elements easily.
Practice
Solution
Step 1: Understand Detox's role
Detox is a testing tool that automates user interactions to check if the app works correctly from start to finish.Step 2: Compare options
Options A, B, and C relate to backend, styling, and state management, which are not Detox's purpose.Final Answer:
To automate user actions and test the app end-to-end -> Option CQuick Check:
Detox purpose = automate user actions [OK]
- Confusing Detox with styling or state tools
- Thinking Detox builds backend APIs
- Assuming Detox only tests small parts
Solution
Step 1: Identify grouping function
Thedescribe()function groups related tests into a suite for better organization.Step 2: Understand other functions
it()defines individual tests,expect()makes assertions, andbeforeEach()runs setup code before tests.Final Answer:
describe() -> Option AQuick Check:
Group tests = describe() [OK]
- Using it() to group tests instead of describe()
- Confusing expect() with grouping
- Thinking beforeEach() groups tests
await element(by.id('loginButton')).tap();
await expect(element(by.id('welcomeMessage'))).toBeVisible();Solution
Step 1: Analyze the tap action
The code taps the element with testID 'loginButton', simulating a user pressing it.Step 2: Analyze the expectation
It then checks if the element with testID 'welcomeMessage' is visible on screen.Final Answer:
The test taps the login button and checks if the welcome message is visible -> Option BQuick Check:
tap loginButton + expect welcomeMessage visible = B [OK]
- Mixing up which element is tapped
- Assuming tap() fails without checking element existence
- Ignoring the order of actions
await element(by.id('submitBtn')).tap();Solution
Step 1: Understand the error message
The error says Detox cannot find an element with testID 'submitBtn', meaning it does not exist or is not rendered.Step 2: Check other options
tap() spelling is correct, Detox supports by.id, and import errors cause different failures.Final Answer:
The app does not have a UI element with testID 'submitBtn' -> Option DQuick Check:
Element not found = missing testID in app [OK]
- Assuming tap() is wrong
- Thinking Detox lacks by.id support
- Ignoring testID spelling or missing assignment
Solution
Step 1: Wait for spinner to disappear
UsewaitFor(...).toBeNotVisible()with a timeout to pause until spinner hides.Step 2: Tap the continue button after spinner disappears
After waiting, tap the button safely knowing the UI is ready.Final Answer:
await waitFor(element(by.id('spinner'))).toBeNotVisible().withTimeout(5000); await element(by.id('continueBtn')).tap(); -> Option AQuick Check:
Wait for spinner gone, then tap button = C [OK]
- Tapping spinner instead of waiting
- Tapping button before spinner disappears
- Checking spinner visible instead of not visible
Ready to prove it?
Pass the test and this pattern moves unknown
