Detox
Detox is a JavaScript mobile testing framework that is built into the application so that the test execution starts with the app launch. This makes test execution fast and robust because no external additional tools are needed to orchestrate and synchronize during test execution.
This section walks you through setting up the integration between Perfecto and Detox. It also includes information about limitations and troubleshooting of the Detox integration.
A sample project is available in GitHub: https://github.com/PerfectoMobileSA/PerfectoDetoxSample
On this page:
Limitations
The Perfecto-Detox integration does not currently support:
- Physical iOS devices. At this point, Detox only supports running tests on iOS simulators. Physical iOS devices are not supported. For details, see:
- Perfecto Smart Reporting. Because device connection happens via DevTunnel, Perfecto does not create any reports.
Step-by-step instructions
-
Go to your Perfecto cloud at:
https://<your-cloud>.
perfectomobile.com/nexperience/
where
<your-cloud>
is the name of your Perfecto cloud. -
Open an Android device and connect to DevTunnel.
-
Run the
run-tests-android.sh
file. The following section provides details on this script.
run-tests-android.sh
Following is a sample run-tests-android.sh
script with explanations of the steps that the script runs through.
Sample run-tests-android.sh script
#!/bin/bash
##### Perfecto Cloud execution starts
export PATH=$PATH:$PWD
export SCREENSHOTSFOLDER=$PWD/screenshots
#################################
cd "$(dirname "$0")" || exit
echo "Before install $(date)"adb devices
UDID="$(adb devices | grep device | tr "\n" " " | awk '{print $5}')"export UDID
echo "UDID: $UDID"rm -rf "$SCREENSHOTSFOLDER"rm ./*.xml
mkdir -p "$SCREENSHOTSFOLDER"echo $SCREENSHOTSFOLDER
node --version
npm -version
watchman --version
npm install
"${PWD}/node_modules/.bin/detox" build --configuration android.device.release
echo "Launching Detox server""${PWD}/node_modules/.bin/detox" run-server > detox-server.log 2>&1 &adb -s $UDID reverse tcp:8099 tcp:8099
echo "Running tests $(date)""${PWD}/node_modules/.bin/detox" test --configuration android.device.release -n $UDID --reuse
scriptExitStatus=$?
adb -s $UDID uninstall com.sampleproject
adb -s $UDID uninstall com.sampleproject.test
adb -s 8C7X1KBE1 reverse --remove-all
echo "Test has been run $(date), exit status: '${scriptExitStatus}'"mv ./*.xml TEST-all.xml
exit $scriptExitStatus
The script performs the following steps:
- Finds all the connected devices with the
adb devices
command and stores the first device ID value to variableUDID
. - Stores screenshots in the project's
root/screenshots
folder. This folder is deleted and created at every run. -
Installs all packages required to run your Detox tests (
npm install
).You should have a local
package.json
file that lists allnpm
packages needed to use with your Detox test. The command in this step uses this file to select those packages. -
If you modified the test, rebuilds the project with the following command:
Copy"${PWD}/node_modules/.bin/detox" build --configuration android.device.release
-
Starts the Detox server:
Copy"${PWD}/node_modules/.bin/detox" run-server > detox-server.log 2>&1 &
-
Connects a device to the server with the following command:
Copyadb -s $UDID reverse tcp:8099 tcp:8099
Important: The same port number should be declared in thepackage.json
file as follows:Copy"server": "ws://localhost:8099",
-
Starts the app and test execution:
Copy"${PWD}/node_modules/.bin/detox" test –configuration android.device.release -n $UDID
Tip:For a faster run, you can use the
--reuse
flag to reuse the existing installed app (do not delete and reinstall). - Uninstalls both app and test app after the execution completes. If you use the
--reuse
flag, comment out theuninstall
commands. - Removes all reversed socket connections from the device.
- Moves the result to the
Test-all.xml
file.
Troubleshooting
To troubleshoot Detox issues, find your error message and perform the steps listed as a workaround.
Error message: "Unable to load script from assets."
If you encounter the error message "Unable to load script from assets," perform the following steps to work around the issue:
- Go to your project directory and check if the following folder exists:
android/app/src/main/assets
- Do the following:
- If the folder exists, delete the following files:
viz index.android.bundle
andindex.android.bundle.meta
- If the folder does not exist, create the missing
assets
folder.
- If the folder exists, delete the following files:
-
From your root project directory, run the following command:
Copycd android
./gradlew clean
-
Navigate back to the root directory and do the following:
-
If only one file (
index.js
) is included in the directory, run the following command:Copyreact-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
-
If two files (
index.android.js
andindex.ios.js
) are included in the directory, run the following command:Copyreact-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
Run the following command:
Copyreact-native run-android
-
Error message: "Module AppRegistry is not a registered callable module."
If you encounter the error message "Module AppRegistry is not a registered callable module," perform the following steps to work around the issue:
-
Run the following command to kill all node processes, start the npm server, and run the application:
Copyrun command killall -9 node
On Windows, run:
Copytaskkill /im node.exe
-
If the process still persists, run:
Copytaskkill /f /im node.exe
-
Run the following command:
Copynpm start --reset-cache
-
Run one of the following commands: