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

Important: This document includes references to a third-party product, Detox. The user interface and usage of third-party products are subject to change without notice. For the latest published information about Detox, see https://wix.github.io/Detox/docs/introduction/getting-started.

Limitations

The Perfecto-Detox integration does not currently support:

Step-by-step instructions

run-tests-android.sh

Following is a sample run-tests-android.sh script with explanations of the steps that the script runs through.

Copy

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:

  1. Finds all the connected devices with the adb devices command and stores the first device ID value to variable UDID.
  2. Stores screenshots in the project's root/screenshots folder. This folder is deleted and created at every run.
  3. Installs all packages required to run your Detox tests (npm install). 

    You should have a local package.json file that lists all npm packages needed to use with your Detox test. The command in this step uses this file to select those packages.

  4. If you modified the test, rebuilds the project with the following command:

    Copy
    "${PWD}/node_modules/.bin/detox" build --configuration android.device.release
  5. Starts the Detox server:

    Copy
    "${PWD}/node_modules/.bin/detox" run-server > detox-server.log 2>&1 &
  6. Connects a device to the server with the following command:

    Copy
    adb -s $UDID reverse tcp:8099 tcp:8099
    Important: The same port number should be declared in the package.json file as follows:
    Copy
    "server": "ws://localhost:8099",
  7. 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).

  8. Uninstalls both app and test app after the execution completes. If you use the --reuse flag, comment out the uninstall commands.
  9. Removes all reversed socket connections from the device.
  10. 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:

  1. Go to your project directory and check if the following folder exists: android/app/src/main/assets
  2. Do the following:
    • If the folder exists, delete the following files: viz index.android.bundle and index.android.bundle.meta
    • If the folder does not exist, create the missing assets folder.
  3. From your root project directory, run the following command:

    Copy
    cd android
./gradlew clean
  4. Navigate back to the root directory and do the following:

    1. If only one file (index.js) is included in the directory, run the following command:

      Copy
      react-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
    2. If two files (index.android.js and index.ios.js) are included in the directory, run the following command:

      Copy
      react-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:

    Copy
    react-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:

  1. Run the following command to kill all node processes, start the npm server, and run the application:

    Copy
    run command killall -9 node

    On Windows, run: 

    Copy
    taskkill /im node.exe
  2. If the process still persists, run:

    Copy
    taskkill /f /im node.exe
  3. Run the following command:

    Copy
    npm start --reset-cache
  4. Run one of the following commands:

    Copy

    iOS

    react-native run-ios 
    Copy

    Android

    react-native run-android