Skip to content

Commit

Permalink
docs(guides): add cucumber integration (wix#3858)
Browse files Browse the repository at this point in the history
  • Loading branch information
pacozaa authored Feb 20, 2023
1 parent 52ef952 commit 0314550
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
93 changes: 93 additions & 0 deletions docs/guide/cucumber-js-integration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Cucumber.js Integration

:::note Community resource

This is an article generously contributed [by the community][initial PR].
Feel free to [contribute to it][Contributing] by submitting a pull request
if you find any errors or have suggestions for improvements.
If you have any questions, please get in touch with the author directly.

:::

[Cucumber] is a tool for running automated tests written in plain language.
It can be integrated with Detox to run end-to-end tests for mobile applications
if you manage [Detox Internals API] from [Cucumber.js hooks].

In your `init.js` or `init.ts` file, please import `detox/internals` on top of the file:

```js
import detox from 'detox/internals'
```

Also import the hooks we'll be using just a bit later:

```js
import { Before, BeforeAll, AfterAll, After, ITestCaseHookParameter } from '@cucumber/cucumber'
```

Define the earliest [Cucumber.js] hook, `BeforeAll`, where you’ll [initialize][`detox.init()`] Detox and launch the app:

```js
BeforeAll({ timeout: 120 * 1000 }, async () => {
await detox.init()
await device.launchApp()
})
```

You must also tell Detox explicitly when [Cucumber.js] starts a test via calling [`detox.onTestStart()`] inside `Before` hook.
Otherwise, Detox won't be able to _start recording_ logs, screenshots, videos, and other artifacts:

```js
Before(async (message: ITestCaseHookParameter) => {
const { pickle } = message
await detox.onTestStart({
title: pickle.uri,
fullName: pickle.name,
status: 'running',
})
})
```

The symmetrical step is to inform Detox that the current test is over via calling [`detox.onTestDone()`] inside `After` hook.
Otherwise, Detox won't be able to _save_ logs, screenshots, videos, and other artifacts:

```js
After(async (message: ITestCaseHookParameter) => {
const { pickle, result } = message
await detox.onTestDone({
title: pickle.uri,
fullName: pickle.name,
status: result ? 'passed' : 'failed',
})
})
```

After the tests are over, you should call [`detox.cleanup()`] inside `AfterAll` hook so that Detox can clean up all the resources it has allocated:

```js
AfterAll(async () => {
await detox.cleanup();
})
```

For more information on [Cucumber.js] please check out this [documentation][Cucumber.js].

[Cucumber]: https://cucumber.io/

[Cucumber.js]: https://github.com/cucumber/cucumber-js#documentation

[Cucumber.js hooks]: https://github.com/cucumber/cucumber-js/blob/main/docs/support_files/hooks.md

[Initial PR]: https://github.com/wix/Detox/pull/3858

[Detox Internals API]: ../api/internals.mdx

[`detox.init()`]: ../api/internals.mdx#initoptions-promise

[`detox.cleanup()`]: ../api/internals.mdx#cleanup-promise

[`detox.onTestStart()`]: ../api/internals.mdx#onteststartevent-promise

[`detox.onTestDone()`]: ../api/internals.mdx#ontestdoneevent-promise

[Contributing]: ../contributing.md
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const sidebars = {
'guide/developing-while-writing-tests',
'guide/android-dev-env',
'guide/proguard-configuration',
'guide/cucumber-js-integration',
'guide/uninstalling',
]
},
Expand Down

0 comments on commit 0314550

Please sign in to comment.