Detox
Extends Helper
This is a wrapper on top of Detox library, aimied to unify testing experience for CodeceptJS framework. Detox provides a grey box testing for mobile applications, playing especially good for React Native apps.
Detox plays quite differently from Appium. To establish detox testing you need to build a mobile application in a special way to inject Detox code. This why Detox is grey box testing solution, so you need access to application source code, and a way to build and execute it on emulator.
Comparing to Appium, Detox runs faster and more stable but requires an additional setup for build.
- Install and configure Detox
- Build an application using
detox buildcommand. - Install CodeceptJS and detox-helper:
npm i @codeceptjs/detox-helper --saveDetox configuration is required in package.json under detox section.
If you completed step 1 and step 2 you should have a configuration similar this:
"detox": { "configurations": { "ios.sim.debug": { "device": "simulator", "app": "ios.debug" } }, "apps": { "ios.debug": { "type": "ios.app", "binaryPath": "../test/ios/build/Build/Products/Debug-iphonesimulator/MyTestApp.app", "build": "xcodebuild -workspace ../test/ios/MyTestApp.xcworkspace -scheme MyTestApp -configuration Debug -sdk iphonesimulator -derivedDataPath ../test/ios/build" } }, "devices": { "simulator": { "type": "ios.simulator", "device": { "type": "iPhone 15" } } } }Configuration
Section titled “Configuration”Besides Detox configuration, CodeceptJS should also be configured to use Detox.
In codecept.conf.js enable Detox helper:
helpers: { Detox: { require: '@codeceptjs/detox-helper', configuration: '<detox-configuration-name>', }}It’s important to specify a package name under require section and current detox configuration taken from package.json.
Options:
configuration- a detox configuration name. Required.reloadReactNative- should be enabled for React Native applications.reuse- reuse application for tests. By default, Detox reinstalls and relaunches app.registerGlobals- (default: true) Register Detox helper functionsby,element,expect,waitForglobally.url- URL to open via deep-link each time the app is launched (android) or immediately afterwards (iOS). Useful for opening a bundle URL at the beginning of tests when working with Expo.
Parameters
Section titled “Parameters”config
appendField
Section titled “appendField”Appends text into the field. A field can be located by text, accessibility id, id.
I.appendField('name', 'davert');Parameters
Section titled “Parameters”checkIfElementExists
Section titled “checkIfElementExists”Checks if an element exists.
I.checkIfElementExists('~edit'); // located by accessibility idI.checkIfElementExists('~edit', '#menu'); // element inside #menuParameters
Section titled “Parameters”locator(string | object) element to locatecontext(string | object | null) context element (optional, defaultnull)
clearField
Section titled “clearField”Clears a text field. A field can be located by text, accessibility id, id.
I.clearField('~name');Parameters
Section titled “Parameters”Clicks on an element. Element can be located by its text or id or accessibility id
The second parameter is a context (id | type | accessibility id) to narrow the search.
Same as tap
I.click('Login'); // locate by textI.click('~nav-1'); // locate by accessibility labelI.click('#user'); // locate by idI.click('Login', '#nav'); // locate by text inside #navI.click({ ios: 'Save', android: 'SAVE' }, '#main'); // different texts on iOS and AndroidParameters
Section titled “Parameters”clickAtPoint
Section titled “clickAtPoint”Performs click on element with horizontal and vertical offset. An element is located by text, id, accessibility id.
I.clickAtPoint('Save', 10, 10);I.clickAtPoint('~save', 10, 10); // locate by accessibility idParameters
Section titled “Parameters”locator(string | object)xnumber horizontal offset (optional, default0)ynumber vertical offset (optional, default0)
dontSee
Section titled “dontSee”Checks text not to be visible. Use second parameter to narrow down the search.
I.dontSee('Record created');I.dontSee('Record updated', '#message');I.dontSee('Record deleted', '~message');Parameters
Section titled “Parameters”textstring to check invisibilitycontext(string | object | null) element in which to search for text (optional, defaultnull)
dontSeeElement
Section titled “dontSeeElement”Checks that element is not visible. Use second parameter to narrow down the search.
I.dontSeeElement('~edit'); // located by accessibility idI.dontSeeElement('~edit', '#menu'); // element inside #menuParameters
Section titled “Parameters”locator(string | object) element to locatecontext(string | object | null) context element (optional, defaultnull)
dontSeeElementExists
Section titled “dontSeeElementExists”Checks that element not exists. Use second parameter to narrow down the search.
I.dontSeeElementExist('~edit'); // located by accessibility idI.dontSeeElementExist('~edit', '#menu'); // element inside #menuParameters
Section titled “Parameters”locator(string | object) element to locatecontext(string | object) context element (optional, defaultnull)
fillField
Section titled “fillField”Fills in text field in an app. A field can be located by text, accessibility id, id.
I.fillField('Username', 'davert');I.fillField('~name', 'davert');I.fillField({ android: 'NAME', ios: 'name' }, 'davert');Parameters
Section titled “Parameters”goBack
Section titled “goBack”Goes back on Android
I.goBack(); // on Android onlygrabPlatform
Section titled “grabPlatform”Grab the device platform
const platform = await I.grabPlatform();installApp
Section titled “installApp”Installs a configured application. Application is installed by default.
I.installApp();launchApp
Section titled “launchApp”Launches an application. If application instance already exists, use relaunchApp.
I.launchApp();longPress
Section titled “longPress”Taps an element and holds for a requested time.
I.longPress('Login', 2); // locate by text, hold for 2 secondsI.longPress('~nav', 1); // locate by accessibility label, hold for secondI.longPress('Update', 2, '#menu'); // locate by text inside #menu, hold for 2 secondsParameters
Section titled “Parameters”locator(string | object) element to locatesecnumber number of seconds to hold tapcontext(string | object | null) context element (optional, defaultnull)
multiTap
Section titled “multiTap”Multi taps on an element. Element can be located by its text or id or accessibility id.
Set the number of taps in second argument. Optionally define the context element by third argument.
I.multiTap('Login', 2); // locate by textI.multiTap('~nav', 2); // locate by accessibility labelI.multiTap('#user', 2); // locate by idI.multiTap('Update', 2, '#menu'); // locate by idParameters
Section titled “Parameters”locator(string | object) element to locatenumnumber number of tapscontext(string | object | null) context element (optional, defaultnull)
relaunchApp
Section titled “relaunchApp”Relaunches an application.
I.relaunchApp();runOnAndroid
Section titled “runOnAndroid”Execute code only on Android
I.runOnAndroid(() => { I.click('Button'); I.see('Hi, Android');});Parameters
Section titled “Parameters”fnFunction a function which will be executed on android
runOnIOS
Section titled “runOnIOS”Execute code only on iOS
I.runOnIOS(() => { I.click('Button'); I.see('Hi, IOS');});Parameters
Section titled “Parameters”fnFunction a function which will be executed on iOS
saveScreenshot
Section titled “saveScreenshot”Saves a screenshot to the output dir
I.saveScreenshot('main-window.png');Parameters
Section titled “Parameters”namestring
scrollDown
Section titled “scrollDown”Scrolls to the bottom of an element.
I.scrollDown('#container');Parameters
Section titled “Parameters”scrollLeft
Section titled “scrollLeft”Scrolls to the left of an element.
I.scrollLeft('#container');Parameters
Section titled “Parameters”scrollRight
Section titled “scrollRight”Scrolls to the right of an element.
I.scrollRight('#container');Parameters
Section titled “Parameters”scrollToElement
Section titled “scrollToElement”Scrolls within a scrollable container to an element.
Parameters
Section titled “Parameters”targetLocator(string | object) Locator of the element to scroll tocontainerLocator(string | object) Locator of the scrollable containerdirectionstring ‘up’ or ‘down’ (optional, default'down')offsetnumber Offset for scroll, can be adjusted based on need (optional, default100)
scrollUp
Section titled “scrollUp”Scrolls to the top of an element.
I.scrollUp('#container');Parameters
Section titled “Parameters”Checks text to be visible. Use second parameter to narrow down the search.
I.see('Record created');I.see('Record updated', '#message');I.see('Record deleted', '~message');Parameters
Section titled “Parameters”textstring to check visibilitycontext(string | object | null) element inside which to search for text (optional, defaultnull)
seeElement
Section titled “seeElement”Checks for visibility of an element. Use second parameter to narrow down the search.
I.seeElement('~edit'); // located by accessibility idI.seeElement('~edit', '#menu'); // element inside #menuParameters
Section titled “Parameters”locator(string | object) element to locatecontext(string | object | null) context element (optional, defaultnull)
seeElementExists
Section titled “seeElementExists”Checks for existence of an element. An element can be visible or not. Use second parameter to narrow down the search.
I.seeElementExists('~edit'); // located by accessibility idI.seeElementExists('~edit', '#menu'); // element inside #menuParameters
Section titled “Parameters”locator(string | object) element to locatecontext(string | object) context element (optional, defaultnull)
setLandscapeOrientation
Section titled “setLandscapeOrientation”Switches device to landscape orientation
I.setLandscapeOrientation();setPortraitOrientation
Section titled “setPortraitOrientation”Switches device to portrait orientation
I.setPortraitOrientation();shakeDevice
Section titled “shakeDevice”Shakes the device.
I.shakeDevice();swipeDown
Section titled “swipeDown”Performs a swipe up inside an element.
Can be slow or fast swipe.
I.swipeUp('#container');Parameters
Section titled “Parameters”locator(string | object) an element on which to perform swipespeedstring a speed to perform:sloworfast. (optional, default'slow')
swipeLeft
Section titled “swipeLeft”Performs a swipe up inside an element.
Can be slow or fast swipe.
I.swipeUp('#container');Parameters
Section titled “Parameters”locator(string | object) an element on which to perform swipespeedstring a speed to perform:sloworfast. (optional, default'slow')
swipeRight
Section titled “swipeRight”Performs a swipe up inside an element.
Can be slow or fast swipe.
I.swipeUp('#container');Parameters
Section titled “Parameters”locator(string | object) an element on which to perform swipespeedstring a speed to perform:sloworfast. (optional, default'slow')
swipeUp
Section titled “swipeUp”Performs a swipe up inside an element.
Can be slow or fast swipe.
I.swipeUp('#container');Parameters
Section titled “Parameters”locator(string | object) an element on which to perform swipespeedstring a speed to perform:sloworfast. (optional, default'slow')
Taps on an element. Element can be located by its text or id or accessibility id.
The second parameter is a context element to narrow the search.
Same as click
I.tap('Login'); // locate by textI.tap('~nav-1'); // locate by accessibility labelI.tap('#user'); // locate by idI.tap('Login', '#nav'); // locate by text inside #navI.tap({ ios: 'Save', android: 'SAVE' }, '#main'); // different texts on iOS and AndroidParameters
Section titled “Parameters”tapByLabel
Section titled “tapByLabel”Clicks on an element. Element can be located by its label
The second parameter is a context (id | type | accessibility id) to narrow the search.
I.tapByLabel('Login'); // locate by textI.tapByLabel('Login', '#nav'); // locate by text inside #navParameters
Section titled “Parameters”tapReturnKey
Section titled “tapReturnKey”Taps return key. A field can be located by text, accessibility id, id.
I.tapReturnKey('Username');I.tapReturnKey('~name');I.tapReturnKey({ android: 'NAME', ios: 'name' });Parameters
Section titled “Parameters”Waits for number of seconds
I.wait(2); // waits for 2 secondsParameters
Section titled “Parameters”secnumber number of seconds to wait
waitForElement
Section titled “waitForElement”Waits for an element to exist on page.
I.waitForElement('#message', 1); // wait for 1 secondParameters
Section titled “Parameters”locator(string | object) an element to wait forsecnumber number of seconds to wait, 5 by default (optional, default5)
waitForElementVisible
Section titled “waitForElementVisible”Waits for an element to be visible on page.
I.waitForElementVisible('#message', 1); // wait for 1 secondParameters
Section titled “Parameters”locator(string | object) an element to wait forsecnumber number of seconds to wait (optional, default5)
waitToHide
Section titled “waitToHide”Waits an elmenet to become not visible.
I.waitToHide('#message', 2); // wait for 2 seconds