@codeceptjs/detox-helper

1.0.7 • Public • Published

Publish npm Package e2e-android e2e-ios

Detox Helper for CodeceptJS

Testing Mobile Apps on iOS and Android can look like this:

I.setLandscapeOrientation();
I.fillField('#text', 'a new text');
I.see('a new text', '#textValue');
I.dontSeeElement('#createdAndVisibleText');
I.click({ ios: '#GoButton', android: 'Button' });
I.waitForElement('#createdAndVisibleText', 20);
I.seeElement('#createdAndVisibleText');
I.runOnAndroid(() => {
  I.click('Save');
  I.see('Text Saved', '#message');
});
I.runOnIOS(() => {
  I.click('SAVE');
  I.see('SAVED!');
});

Example output:

CodeceptJS v3.5.12 #StandWithUkraine
Using test root "/Users/t/Desktop/projects/detox-helper/test"
Helpers: Detox
Plugins: screenshotOnFail

Starter --
    [1]  Starting recording promises
    Timeouts: 
12:08:18.693 detox[70751] i org.reactjs.native.example.MyTestApp launched. To watch simulator logs, run:
        /usr/bin/xcrun simctl spawn 2A7CB01D-1AAF-4AB6-8236-163E1701D21A log stream --level debug --style compact --predicate 'process == "MyTestApp"'
12:08:20.542 detox[70751] i   App started
12:08:20.908 detox[70751] i     I see "Welcome to
React Native"
12:08:20.919 detox[70751] i   ✔ OK in 17ms
12:08:20.919 detox[70751] i 
12:08:20.922 detox[70751] i   Get plaform
12:08:21.274 detox[70751] i     I grab platform 
12:08:21.274 detox[70751] i ios
12:08:21.274 detox[70751] i   ✔ OK in 4ms
12:08:21.275 detox[70751] i 
12:08:21.310 detox[70751] i 
12:08:21.312 detox[70751] i   OK  | 2 passed   // 14s

CodeceptJS provides next features over standard Detox:

  • Unified API. The same test can be executed in Appium or Detox. Unified API helps different teams to use the same syntax and easy port tests from one engine to another.
  • Interactive pause. When starting/stopping an application takes a long time, debugging and writing tests can be hard. CodeceptJS solves this by pausing an execution and letting you try different commands and locators. With this feature a test can be writtern during one running session.
  • One Test For Android and IOS. When application differs on Android and IOS you can provide corresponding system-related locators for both systems. When needed a different code can be executed for Android and IOS keeping it inside the same test.

API

Table of Contents

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.

Setup

  1. Install and configure Detox

  2. Build an application using detox build command.

  3. Install CodeceptJS and detox-helper:

    npm i @codeceptjs/detox-helper --save

Detox 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

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 functions by, element, expect, waitFor globally.

Parameters

  • config

saveScreenshot

Saves a screenshot to the output dir

I.saveScreenshot('main-window.png');
Parameters

relaunchApp

Relaunches an application.

I.relaunchApp();

launchApp

Launches an application. If application instance already exists, use relaunchApp.

I.launchApp();

installApp

Installs a configured application. Application is installed by default.

I.installApp();

shakeDevice

Shakes the device.

I.shakeDevice();

goBack

Goes back on Android

I.goBack(); // on Android only

setLandscapeOrientation

Switches device to landscape orientation

I.setLandscapeOrientation();

setPortraitOrientation

Switches device to portrait orientation

I.setPortraitOrientation();

grabPlatform

Grab the device platform

const platform = await I.grabPlatform();

runOnIOS

Execute code only on iOS

I.runOnIOS(() => {
   I.click('Button');
   I.see('Hi, IOS');
});
Parameters
  • fn Function a function which will be executed on iOS

runOnAndroid

Execute code only on Android

I.runOnAndroid(() => {
   I.click('Button');
   I.see('Hi, Android');
});
Parameters
  • fn Function a function which will be executed on android

tap

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 text
I.tap('~nav-1'); // locate by accessibility label
I.tap('#user'); // locate by id
I.tap('Login', '#nav'); // locate by text inside #nav
I.tap({ ios: 'Save', android: 'SAVE' }, '#main'); // different texts on iOS and Android
Parameters
  • locator CodeceptJS.LocatorOrString
  • context (CodeceptJS.LocatorOrString | null) (optional, default null)

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 text
I.multiTap('~nav', 2); // locate by accessibility label
I.multiTap('#user', 2); // locate by id
I.multiTap('Update', 2, '#menu'); // locate by id
Parameters
  • locator CodeceptJS.LocatorOrString element to locate
  • num number number of taps
  • context (CodeceptJS.LocatorOrString | null) context element (optional, default null)

longPress

Taps an element and holds for a requested time.

I.longPress('Login', 2); // locate by text, hold for 2 seconds
I.longPress('~nav', 1); // locate by accessibility label, hold for second
I.longPress('Update', 2, '#menu'); // locate by text inside #menu, hold for 2 seconds
Parameters
  • locator CodeceptJS.LocatorOrString element to locate
  • sec number number of seconds to hold tap
  • context (CodeceptJS.LocatorOrString | null) context element (optional, default null)

click

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 text
I.click('~nav-1'); // locate by accessibility label
I.click('#user'); // locate by id
I.click('Login', '#nav'); // locate by text inside #nav
I.click({ ios: 'Save', android: 'SAVE' }, '#main'); // different texts on iOS and Android
Parameters
  • locator CodeceptJS.LocatorOrString
  • context (CodeceptJS.LocatorOrString | null) (optional, default null)

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 text
I.tapByLabel('Login', '#nav'); // locate by text inside #nav
Parameters
  • locator CodeceptJS.LocatorOrString
  • context (CodeceptJS.LocatorOrString | null) (optional, default null)

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 id
Parameters
  • locator CodeceptJS.LocatorOrString
  • x number horizontal offset (optional, default 0)
  • y number vertical offset (optional, default 0)

see

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
  • text string to check visibility
  • context (CodeceptJS.LocatorOrString | null) element inside which to search for text (optional, default null)

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
  • text string to check invisibility
  • context (CodeceptJS.LocatorOrString | null) element in which to search for text (optional, default null)

seeElement

Checks for visibility of an element. Use second parameter to narrow down the search.

I.seeElement('~edit'); // located by accessibility id
I.seeElement('~edit', '#menu'); // element inside #menu
Parameters
  • locator CodeceptJS.LocatorOrString element to locate
  • context (CodeceptJS.LocatorOrString | null) context element (optional, default null)

checkIfElementExists

Checks if an element exists.

I.checkIfElementExists('~edit'); // located by accessibility id
I.checkIfElementExists('~edit', '#menu'); // element inside #menu
Parameters
  • locator CodeceptJS.LocatorOrString element to locate
  • context (CodeceptJS.LocatorOrString | null) context element (optional, default null)

dontSeeElement

Checks that element is not visible. Use second parameter to narrow down the search.

I.dontSeeElement('~edit'); // located by accessibility id
I.dontSeeElement('~edit', '#menu'); // element inside #menu
Parameters
  • locator CodeceptJS.LocatorOrString element to locate
  • context (CodeceptJS.LocatorOrString | null) context element (optional, default null)

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 id
I.seeElementExists('~edit', '#menu'); // element inside #menu
Parameters
  • locator CodeceptJS.LocatorOrString element to locate
  • context CodeceptJS.LocatorOrString context element (optional, default null)

dontSeeElementExists

Checks that element not exists. Use second parameter to narrow down the search.

I.dontSeeElementExist('~edit'); // located by accessibility id
I.dontSeeElementExist('~edit', '#menu'); // element inside #menu
Parameters
  • locator CodeceptJS.LocatorOrString element to locate
  • context CodeceptJS.LocatorOrString context element (optional, default null)

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
  • field CodeceptJS.LocatorOrString an input element to fill in
  • value string value to fill

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
  • field CodeceptJS.LocatorOrString an input element to fill in

clearField

Clears a text field. A field can be located by text, accessibility id, id.

I.clearField('~name');
Parameters
  • field CodeceptJS.LocatorOrString an input element to clear

appendField

Appends text into the field. A field can be located by text, accessibility id, id.

I.appendField('name', 'davert');
Parameters
  • field CodeceptJS.LocatorOrString
  • value string

scrollUp

Scrolls to the top of an element.

I.scrollUp('#container');
Parameters
  • locator CodeceptJS.LocatorOrString

scrollDown

Scrolls to the bottom of an element.

I.scrollDown('#container');
Parameters
  • locator CodeceptJS.LocatorOrString

scrollLeft

Scrolls to the left of an element.

I.scrollLeft('#container');
Parameters
  • locator CodeceptJS.LocatorOrString

scrollRight

Scrolls to the right of an element.

I.scrollRight('#container');
Parameters
  • locator CodeceptJS.LocatorOrString

swipeUp

Performs a swipe up inside an element. Can be slow or fast swipe.

I.swipeUp('#container');
Parameters
  • locator CodeceptJS.LocatorOrString an element on which to perform swipe
  • speed string a speed to perform: slow or fast. (optional, default 'slow')

swipeDown

Performs a swipe up inside an element. Can be slow or fast swipe.

I.swipeUp('#container');
Parameters
  • locator CodeceptJS.LocatorOrString an element on which to perform swipe
  • speed string a speed to perform: slow or fast. (optional, default 'slow')

swipeLeft

Performs a swipe up inside an element. Can be slow or fast swipe.

I.swipeUp('#container');
Parameters
  • locator CodeceptJS.LocatorOrString an element on which to perform swipe
  • speed string a speed to perform: slow or fast. (optional, default 'slow')

swipeRight

Performs a swipe up inside an element. Can be slow or fast swipe.

I.swipeUp('#container');
Parameters
  • locator CodeceptJS.LocatorOrString an element on which to perform swipe
  • speed string a speed to perform: slow or fast. (optional, default 'slow')

wait

Waits for number of seconds

I.wait(2); // waits for 2 seconds
Parameters
  • sec number number of seconds to wait

waitForElement

Waits for an element to exist on page.

I.waitForElement('#message', 1); // wait for 1 second
Parameters
  • locator CodeceptJS.LocatorOrString an element to wait for
  • sec number number of seconds to wait, 5 by default (optional, default 5)

waitForElementVisible

Waits for an element to be visible on page.

I.waitForElementVisible('#message', 1); // wait for 1 second
Parameters
  • locator CodeceptJS.LocatorOrString an element to wait for
  • sec number number of seconds to wait (optional, default 5)

waitToHide

Waits an elmenet to become not visible.

I.waitToHide('#message', 2); // wait for 2 seconds
Parameters
  • locator CodeceptJS.LocatorOrString an element to wait for
  • sec number number of seconds to wait (optional, default 5)

scrollToElement

Scrolls within a scrollable container to an element.

Parameters
  • targetLocator CodeceptJS.LocatorOrString Locator of the element to scroll to
  • containerLocator CodeceptJS.LocatorOrString Locator of the scrollable container
  • direction string 'up' or 'down' (optional, default 'down')
  • offset number Offset for scroll, can be adjusted based on need (optional, default 100)

Readme

Keywords

none

Package Sidebar

Install

npm i @codeceptjs/detox-helper

Weekly Downloads

29,380

Version

1.0.7

License

ISC

Unpacked Size

49.8 kB

Total Files

4

Last publish

Collaborators

  • egor_bodnar
  • davert