Graybox E2E Tests and Automation Library for Mobile
- About
- Getting Started
- See it in Action
- The Detox API
- Dealing with flakiness
- Contributing to detox
- Some implementation details
- License
High velocity native mobile development requires us to adopt continuous integration workflows, which means our reliance on manual QA has to drop significantly. The most difficult part of automated testing on mobile is the tip of the testing pyramid - E2E. The core problem with E2E tests is flakiness - tests are usually not deterministic. We believe the only way to tackle flakiness head on is by moving from blackbox testing to graybox testing and that's where detox comes into play.
Please note that this library is still pre version 1.0.0 and under active development. The NPM version is higher because the name "detox" was transferred to us from a previous inactive package.
This is a step-by-step guide to help you add detox to your project.
Step 0: Remove Previous Detox Integration (for [email protected]
users)
If you have integrated with Detox in the past, you will need to clean your project before integrating with current Detox version.
- Use the provided
cleanup_4.0.rb
to remove unneeded changes made with Detox 4.0.x. - Make sure to add changes performed by running this script to version control.
- Install the latest version of
brew
. - If you haven't already, install Node.js
brew update && brew install node
- You'd also need
fbsimctl
installed:
brew tap facebook/fb && brew install fbsimctl
-
Detox CLI
detox-cli
package should be installed globally, enabling usage of detox command line tools outside of your npm scripts.npm install -g detox-cli
-
If you do not have a
package.json
file in the root folder of your project, create one by runningnpm init
Follow the on screen instructions.
By default, Xcode uses a randomized hidden path for outputting project build artifacts, called DerivedData. For ease of use (and better support in CI environments), it is recommended to change the project build path to a more convenient path.
- With your project opened in Xcode, select menu
File
â–şProject Settings...
. Click onAdvanced...
, selectCustom
and from the drop-down menu, selectProject-relative Location
. - Build artifacts will now be created in a
DerivedData
folder next to yourxcodeproj
project.
- Add
detox
to thedevDependencies
section ofpackage.json
:
npm install detox --save-dev
- Detox needs to run inside a test runner (there are many out there: karma, mocha, ava etc.). Currently, we recommend mocha.
npm install mocha --save-dev
-
Add to the
scripts
section ofpackage.json
: -
Add a
detox
section topackage.json
:configurations
: holds all the device configurations, if there is only one configuration inconfigurations
detox build
anddetox test
will default to it, to choose a specific configuration use--configuration
param**per configuration: **
Configuration Params Details binaryPath
relative path to the ipa/app due to be tested (make sure you build the app in a project relative path) type
device type, currently on simulator
(iOS) is supportedname
device name, aligns to the device list avaliable through fbsimctl list
for example, this is one line of the output offbsimctl list
: `A3C93900-6D17-4830-8FBE-E102E4BBCBB9build
[optional] build command (either xcodebuild
,react-native run-ios
, etc...), will be later available through detox CLI tool."detox": { "configurations": { "ios.sim.release": { "binaryPath": "ios/build/Build/Products/Release-iphonesimulator/example.app", "build": "xcodebuild -project ios/example.xcodeproj -scheme example -configuration Release -sdk iphonesimulator -derivedDataPath ios/build", "type": "simulator", "name": "iPhone 7" } } }
Detox can either initialize a server using a generated configuration, or can be overriden with a manual configuration:
"detox": { ... "session": { "server": "ws://localhost:8099", "sessionId": "YourProjectSessionId" } }
Applies when using
detox-cli
by runningdetox test
command, default ise2e
."detox": { ... "specs": "path/to/tests" }
- Create an
e2e
folder in your project root - Create
mocha.opts
file with this content. - Create
init.js
file with this content. - Create your first test!
myFirstTest.spec.js
with content similar to this.
By using detox
command line tool, you can build and test your project easily.
In your detox config (in package.json) paste your build command into the configuration's build
field.
The build command will be triggered when running
If there's only one configuration, you can simply use:
detox build
For multiple configurations, choose your configuration by passing --configuration
param:
detox build --configuration yourConfiguration
- We have prepared a build command in
detox-cli
that can help you control your tests easily
You can now choose to build your project in any of these ways...
-
Through
detox
:detox build --configuration yourConfiguration
-
Building with xcodebuild:
xcodebuild -project ios/YourProject.xcodeproj -scheme YourProject -sdk iphonesimulator -derivedDataPath ios/build
-
Building using React Native, this is the least suggested way of running your build, since it also starts a random simulator and installs the app on it.
react-native run-ios
-
If you have build problems, see troubleshooting.
Note: remember to update the
app
path in yourpackage.json
.
If there's only one configuration, you can simply use:
detox test
For multiple configurations, choose your configuration by passing --configuration
param:
detox test --configuration yourConfiguration
Open the React Native demo project and follow the instructions
Not using React Native? you now have a pure native demo project too
Check the API Reference or see detox's own E2E test suite to learn the test API by example.
See the Flakiness handbook
If you're interested in working on detox core and contributing to detox itself, take a look here.
- We let you write your e2e tests in JS (they can even be cross-platform)
- We use websockets to communicate (so it should be super fast and bi-directional)
- Both the app and the tester are clients, so we need the server to proxy between them
- We are relying on EarlGrey as our gray-box native library for iOS (espresso for Android later on)
- The JS tester controls EarlGrey by remote using a strange JSON protocol
- Instead of wrapping the zillion API calls EarlGrey supports, we implemented a reflection mechanism
- So the JS tester in low level actually invokes the native methods.. freaky
- We've abstracted this away in favor of a protractor-like api, see
demo-react-native/e2e/example.spec.js
- See everything EarlGrey supports here and in this cheatsheet
- We use fbsimctl to control the simulator from the test, restart the app, etc
- detox by itself and all original source code in this repo is MIT
- detox relies on some important dependencies, their respective licenses are: