Blog

Automatic mobile app testing using Detox

Add a file android/app/src/androidTest/java/com/[your.package]/DetoxTest.java and fill as in the detox example app for NR. Don’t forget to change the package name to your project’s (create folder androidTest if you need).

  1. Add Detox configuration to package.json

“detox” : {

“configurations”: {

“android.emu.debug”: {

“binaryPath”: “android/app/build/outputs/apk/debug/app-debug.apk”,

“build”:

“cd android && ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug && cd ..”,

“type”: “android.emulator”,

“device”: {

“avdName”: <your_emulator_abd_name>

}

},

“android.emu.release”: {

“binaryPath”: “android/app/build/outputs/apk/release/app-release.apk”,

“build”: “cd android && ./gradlew assembleRelease assembleAndroidTest -DtestBuildType=release && cd ..”,

“type”: “android.emulator”,

“device”: {

“avdName”: <your_emulator_abd_name>

}

}

}

}

note: You can get your list of available emulators with the following commands:

cd $ANDROID_SDK_ROOT/emulator

./emulator -list-avds

Step 3: Writing a basic test case.

Let’s assume we want to start a test interacting with a TouchableOpacity. First thing we need is to define a testID so we can reference the ponent via a Detox test:


<View>

<TouchableOpacity testID=’MyUniqueId123′>

<Text>Some button</Text>

</TouchableOpacity>

</View>

Note: since Detox forces us to define IDs for the componentes, it is considered a “gray-box” testing framework due to the fact that the code will reflect the fact that it is tested with Detox through this property.

Create a .spec.js file inside e2e folder (or use the existing file firstTest.spec.js) and paste the following code:

//Define a test block

describe(‘App Tests’, () => {

//Reset the App before each test

beforeEach(async () => {

await device.reloadReactNative();

});

//Define a test case

it(‘test case description’, async () => {

//Your Code here, for example await element(by.id(‘id’)).tap()

});

})

Match the element and perform an action:

await element(by.id(‘MyUniqueId123’)).tap();

Set an expectation on the result:

await expect(element(by.id(‘AnotherUniqueId456’))).toBeVisible();

Step 4: Running the test.

Build the app:

detox build

Then, the tests can be run with the following commands

  1. Android:

detox test -c android.emu.debug

  1. IOS:

detox test -c ios.sim.debug

The command above will run the test (files with extension .spec.js) inside e2e folder.

That’s it. Your first failing Detox test is running!

Make more tests:

You can create more complex tests using the Detox API:

Compartir el post

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email