# Debug Nightwatch Tests

A failing Nightwatch test tells you which command or assertion broke, but not what the page looked like, which request failed or what the browser console said at that moment. [WebdriverIO DevTools](/docs/devtools.md) records all of that for every Nightwatch command, from one entry in your Nightwatch config and with no change to your test files. Use **live mode** to watch a failing test, inspect each step and rerun it on your machine, and **trace mode** to keep a replayable file of failures in CI. It works with Nightwatch's `describe`/`it` and exports-object interfaces and Cucumber, in desktop and mobile browsers and in Appium apps. A native app has no page or browser console, so there DevTools records each step's screenshot and element tree instead; see [Mobile Testing](/docs/devtools/mobile.md).

## Set up DevTools[​](#set-up-devtools "Direct link to Set up DevTools")

Install the adapter:

```
npm install @wdio/nightwatch-devtools --save-dev
```

Add it to your config as `globals`:

nightwatch.conf.cjs

```
const nightwatchDevtools = require('@wdio/nightwatch-devtools').default



module.exports = {

  src_folders: ['tests'],



  test_settings: {

    default: {

      desiredCapabilities: {

        browserName: 'chrome',

        webSocketUrl: true,

        'goog:loggingPrefs': { performance: 'ALL' }

      },

      globals: nightwatchDevtools()

    }

  }

}
```

`'goog:loggingPrefs'` lets the adapter read network requests from Chrome. `webSocketUrl: true` lets it load its page recorder before each page's own scripts run, so the replayed page is accurate right after a navigation. The adapter needs Node.js 22.23.4+, 24.22.1+ or 26.12.1+ and Nightwatch 3 or later. For Cucumber, also register its hooks as shown on the [Nightwatch DevTools](/docs/devtools/nightwatch.md#cucumber--bdd) page.

Your tests stay as they are. This is an ordinary Nightwatch test:

tests/login.js

```
describe('login', function () {

  it('logs in with valid credentials', function (browser) {

    browser

      .navigateTo('https://the-internet.herokuapp.com/login')

      .setValue('#username', 'tomsmith')

      .setValue('#password', 'SuperSecretPassword!')

      .click('button[type="submit"]')

      .assert.textContains('#flash', 'You logged into a secure area')

  })

})
```

## Debug a failing test on your machine[​](#debug-a-failing-test-on-your-machine "Direct link to Debug a failing test on your machine")

Run the test that fails:

```
npx nightwatch tests/login.js
```

The DevTools dashboard opens in a new browser window and fills in as the test runs. The run stays open after the tests finish so you can look at the results; close the dashboard window to end it. Then:

1. Select the failed test in the sidebar. The **Actions** list shows its commands in the order they ran, and the one that failed is marked.
2. Select a command. The browser pane shows the page as it was after that command, so you can see whether the element you expected was there, hidden or covered.
3. Use the bottom tabs to find the cause:

| You want to know                                               | Look at                                                               |
| -------------------------------------------------------------- | --------------------------------------------------------------------- |
| The error and the line that raised it                          | **Errors**, then **Source** to see the test code                      |
| What the selected command was called with and what it returned | **Log**                                                               |
| Whether an API call failed or returned something unexpected    | **Network**, then select a request for its headers, status and timing |
| Whether the app logged an error or threw an exception          | **Console**                                                           |

4. Fix the test or the app, then click ▶ on the test's row to rerun only that test. The rerun starts Nightwatch again with your config, so it runs your edited code, and reports into the same window.

`browser.assert` and `browser.verify` checks, including `.not` ones, are rows of their own carrying the expected and actual values.

## Debug a test that fails only in CI[​](#debug-a-test-that-fails-only-in-ci "Direct link to Debug a test that fails only in CI")

CI has no screen to open a dashboard on, so record a trace instead. Switch to trace mode only in CI:

nightwatch.conf.cjs

```
const nightwatchDevtools = require('@wdio/nightwatch-devtools').default



module.exports = {

  src_folders: ['tests'],



  test_settings: {

    default: {

      desiredCapabilities: {

        browserName: 'chrome',

        webSocketUrl: true,

        'goog:loggingPrefs': { performance: 'ALL' }

      },

      globals: nightwatchDevtools(process.env.CI ? { mode: 'trace' } : {})

    }

  }

}
```

This writes one trace per browser session to a `test-results/` folder next to your tests. For Cucumber scenarios and the exports-object interface you can write one trace per test and keep only the failed ones:

```
globals: nightwatchDevtools(process.env.CI

  ? { mode: 'trace', traceGranularity: 'test', tracePolicy: 'retain-on-failure' }

  : {})
```

With the `describe`/`it` interface, Nightwatch signals the start of a test only once per file, so a per-test trace covers the whole file and is named after its first test. Keep the default one trace per session there.

Upload the folders as a build artifact. On GitHub Actions:

.github/workflows/test.yml

```
      - name: Upload DevTools traces

        if: ${{ !cancelled() }}

        uses: actions/upload-artifact@v4

        with:

          name: devtools-traces

          path: |

            **/test-results/

            !**/node_modules/**

          if-no-files-found: ignore
```

To open the traces, download the artifact, unzip it and run:

```
npx show-trace test-results/
```

The [trace player](/docs/devtools/trace-player.md) lists every trace with its outcome, failures first. It has the same tabs as the dashboard, plus DOM time-travel, the **A11y** tab and element overlay, which copy a CSS locator you can paste into a Nightwatch command, and a scrubbable filmstrip of the run. To open a trace without installing anything, drop the folder on [trace.webdriver.io](https://trace.webdriver.io); the file is read in your browser tab and uploaded nowhere.

Traces mask password fields and the `Authorization`, `Cookie` and `Set-Cookie` headers by default. Add the [`secrets`](/docs/devtools/dashboard.md#secret-redaction) option for other values, such as API tokens, before you share a trace.

## Debug a flaky test[​](#debug-a-flaky-test "Direct link to Debug a flaky test")

A flaky test fails on one run and passes on the next. Compare the two runs rather than reading two logs side by side.

On your machine, hover the failed test's row and click the bug-and-play icon next to ▶. [Preserve & Rerun](/docs/devtools/wdio/preserve-and-rerun.md) keeps the failed run, reruns the test, and opens the **Compare** tab with both runs aligned command by command. The first command where they differ is highlighted, along with the assertion's expected and received values.

In CI, Nightwatch's `--retries` reruns a test inside the same run without telling the adapter, so a trace cannot be kept just because a test needed a retry: a test that failed and then passed counts as passed. To catch a flake in CI, keep every trace on the runs where you hunt it: leave out `tracePolicy`, so trace mode keeps everything, as in the first CI config above.

## Hand a failure to a coding agent[​](#hand-a-failure-to-a-coding-agent "Direct link to Hand a failure to a coding agent")

Every trace contains `transcript.md`, a Markdown summary of the test's commands with their selectors, values and errors. Point your agent at the failed test's trace and ask it to read `transcript.md` first, or click **Copy** on the player's **Transcript** tab to paste the transcript and the failing errors into a chat.

## Troubleshooting[​](#troubleshooting "Direct link to Troubleshooting")

**The Network tab is empty.** Add `'goog:loggingPrefs': { performance: 'ALL' }` to `desiredCapabilities` as shown in the setup. For console messages and network requests as they happen, also pass `bidi: true` to `nightwatchDevtools()` with `webSocketUrl: true` in the capabilities.

**The log says `Could not find nightwatch config` and `test rerun will be unavailable`.** The ▶ and Preserve & Rerun buttons start Nightwatch again with your config file, so the adapter has to find it. It uses the file passed with `--config` (or `-c`), or else the first `nightwatch.conf.*` in the current folder or a folder above it. Pass `--config` or run Nightwatch from a folder at or below the config file.

**The run does not end after the tests finish.** In live mode the run waits for you to close the dashboard window. Press Ctrl+C to end it and leave the window open, or use trace mode in CI.

## Next steps[​](#next-steps "Direct link to Next steps")

* [Nightwatch DevTools](/docs/devtools/nightwatch.md): every option, Cucumber setup, screencasts and BiDi capture.
* [Trace Mode](/docs/devtools/wdio/trace-mode.md): what a trace contains and how granularity and retention work.
* [Trace Player](/docs/devtools/trace-player.md): the player's tabs, keyboard shortcuts and shareable links.
* [Mobile Testing](/docs/devtools/mobile.md): debug Appium tests on Android and iOS.
