What is detox?
Detox is an end-to-end testing and automation framework for mobile applications. It is designed to test mobile apps on both iOS and Android platforms, ensuring that the app works as expected from the user's perspective. Detox is known for its ability to handle asynchronous operations and its integration with popular mobile development frameworks like React Native.
What are detox's main functionalities?
End-to-End Testing
This code demonstrates a basic end-to-end test using Detox. It launches the app, checks for the visibility of a welcome screen, taps a button, and verifies that a 'Hello' screen is displayed.
const { device, expect, element, by } = require('detox');
beforeAll(async () => {
await device.launchApp();
});
describe('Example', () => {
it('should have welcome screen', async () => {
await expect(element(by.id('welcome'))).toBeVisible();
});
it('should show hello screen after tap', async () => {
await element(by.id('hello_button')).tap();
await expect(element(by.text('Hello!!!'))).toBeVisible();
});
});
Device Interaction
This code sample shows how to interact with the device using Detox. It includes actions like rotating the device to landscape mode and shaking the device.
const { device } = require('detox');
beforeAll(async () => {
await device.launchApp();
});
describe('Device Interaction', () => {
it('should rotate the device to landscape', async () => {
await device.setOrientation('landscape');
});
it('should shake the device', async () => {
await device.shake();
});
});
Synchronization
This code demonstrates Detox's synchronization capabilities. It waits for a specific element to become visible within a given timeout period.
const { device, element, by } = require('detox');
beforeAll(async () => {
await device.launchApp();
});
describe('Synchronization', () => {
it('should wait for element to be visible', async () => {
await waitFor(element(by.id('uniqueId'))).toBeVisible().withTimeout(2000);
});
});
Other packages similar to detox
appium
Appium is an open-source tool for automating native, mobile web, and hybrid applications on iOS and Android platforms. Unlike Detox, which is specifically designed for React Native and mobile apps, Appium supports a broader range of applications and programming languages.
cypress
Cypress is a JavaScript end-to-end testing framework primarily designed for web applications. While it does not natively support mobile app testing like Detox, it is known for its ease of use and powerful features for web testing.
webdriverio
WebdriverIO is a popular testing framework that allows you to run tests based on the WebDriver protocol and Appium. It supports both web and mobile applications, making it more versatile compared to Detox, which is focused on mobile apps.
detox
Library
Install
- make sure you're in folder
detox/detox
- run
npm install
- run
npm run build
Run Tests
- make sure you're in folder
detox/detox
- run
npm run test-clean
- run
npm run test-install
- run
npm run test
npm run test --debug
(for tests only on a debug app)npm run test --release
(for tests only on a release app)
Developing detox and maintaining your sanity
First, follow the instructions above until the tests pass. Run the tests after every change you make to verify you didn't break anything. Doing test-install
after every change will drive you insane, so here are some tips to minimize wait time between changes you make:
- if you change detox library JS code (stuff under
detox/detox/src
)
- run
npm run test-update-js
- run
npm run test
- if you change detox library native code (stuff under
detox/detox/ios
)
- run
npm run test-update-native
- run
npm run test
- if you change the e2e tests (stuff under
detox/detox/test/e2e
)
- no need to do anything special
- run
npm run test
- if you change the test RN app (stuff under
detox/detox/text
)
- for debug
- no need to do anything special
- run
npm run test --debug
- for release
- run
npm run test-install
- run
npm run test --release
- if you change detox-server (stuff under
detox/detox-server
)
- run
npm run test-update-js
- run
npm run test
- if you change detox-tools (stuff under
detox/detox-tools
)
- run
cd test ; npm install detox-tools ; cd ..
- for debug
- for release
- run
npm run test-install
- run
npm run test --release