# Technical Documentation: nightwatchjs/nightwatch

> ℹ️ **Provenance:** Multi-Tier Ingestion · [CodeWiki Reference](https://codewiki.google/github.com/nightwatchjs/nightwatch)

### Overview (`README.md`)

# Nightwatch.js

[](https://www.npmjs.com/package/nightwatch)
[](https://github.com/nightwatchjs/nightwatch/actions/workflows/build-node.yaml)
[](https://codecov.io/gh/nightwatchjs/nightwatch)
[![Discord][discord-badge]][discord]



<p align="center">
  <img alt="Nightwatch.js Logo" src="https://raw.githubusercontent.com/nightwatchjs/nightwatch/main/.github/assets/nightwatch-logo.png" width=300 />
</p>

#### [Homepage](https://nightwatchjs.org) &bullet; [Developer Guide](https://nightwatchjs.org/guide) &bullet; [API Reference](https://nightwatchjs.org/api) &bullet; [About](https://nightwatchjs.org/about) &bullet; [Blog](https://nightwatchjs.org/blog)

Nightwatch is an integrated testing framework powered by Node.js and using the [W3C Webdriver API](https://www.w3.org/TR/webdriver/). It is a complete testing solution developed at [BrowserStack](https://www.browserstack.com/) and which can be used for: 

☑️ end-to-end testing of web applications and websites

☑️ component testing in isolation (React / Vue / Storybook / Angular)

☑️ Node.js unit, visual regression testing, accessibility testing & API testing

☑️ Native mobile app testing on Android & iOS

## 🚀 Nightwatch v3
[What's New](https://nightwatchjs.org/guide/overview/whats-new-in-v3.html) | [Release Notes](https://github.com/nightwatchjs/nightwatch/releases/tag/v3.0.1) | [Discussions](https://github.com/nightwatchjs/nightwatch/discussions)

Nightwatch v3 is an all new generation that has been built around these three pillars:

* **Developer Experience** : The entire experience from getting started, to writing and debugging tests, has been redesigned for speed, stability, and consistent non-flaky results.

* **Mobile first**: Test your web or native, iOS and Android, mobile applications on simulators, real mobile devices or a cloud grid like BrowserStack.

* **One test automation framework**: Run all types of tests from unit, component, and E2E to API, visual, and accessibility with a single framework.

The [Nightwatch v3](https://github.com/nightwatchjs/nightwatch/releases/tag/v3.0.1) is not just a new version, it’s the result of months of engineering effort to reimagine test automation for the future. Try it out in 60 seconds and see it in action.  

## ⚙️ Get started in 60 seconds

#### 1. Install Nightwatch from NPM

From your existing project's root dir:

```sh
npm init nightwatch@latest
```

or, if you want to initialize a new project:


```sh
npm init nightwatch@latest ./path/to/new/project
```




#### 2. Answer a few questions about your preferred setup:

- What is your Language - Test Runner setup? 
- Where do you want to run your e2e tests? 
- Where you'll be testing on? 
- Where do you plan to keep your end-to-end tests? 
- What is the base_url of your project? 

Nightwatch will do the entire setup for you based on your answers.

#### 3. Run a Demo Test:

Nightwatch comes with a few examples, which are automatically copied to your Nightwatch project during the setup and can also be used as boilerplate to write your own tests on top of them.

You can follow the instructions given at the end of the setup to run your first test with Nightwatch.

<img width="413" alt="image" src="https://user-images.githubusercontent.com/39924567/174763723-aff4d501-6320-402c-81cc-de75fbb5e8f0.png">

---

## Nightwatch mobile app testing

Nightwatch enables automation testing of native mobile applications via Appium. It combines the robustness of Appium with the enhanced developer experience provided by Nightwatch. It enables end-to-end functional testing of native mobile apps on Android and iOS devices. Try it now

## Go beyond E2E
### [Component testing](https://nightwatchjs.org/guide/component-testing/introduction.html)

With Nightwatch you can test components in isolation by mounting them in the browser. Nightwatch 2 added support for component testing for popular web frameworks such as

1. [React](https://nightwatchjs.org/guide/component-testing/testing-react-components.html)
2. [VueJS](https://nightwatchjs.org/guide/component-testing/vite-plugin.html)
3. [Angular](https://nightwatchjs.org/guide/component-testing/angular-component-testing.html)
4. [Storybook](https://nightwatchjs.org/guide/component-testing/storybook-component-testing.html)

### Nightwatch unit tests

The tests for Nightwatch are written using Mocha.

1. **Clone the project**

   ```bash
   git clone https://github.com/nightwatchjs/nightwatch.git

   # change directory
   cd nightwatch

   # install the dependencies
   npm install
   ```

2. **Run tests**

   To run the complete test suite:

   ```bash
   npm test
   ```

   To check test coverage, run the command:

   ```bash
   npm run mocha-coverage
   ```

   and then open the generated coverage/index.html file in your browser.

See [Unit testing guide](https://nightwatchjs.org/guide/writing-tests/write-nodejs-unit-integration-tests.html) for more details.

### Other types of testing
#### [Visual Regression Testing](https://nightwatchjs.org/guide/writing-tests/visual-regression-testing.html)

Nightwatch v3 introduces visual regression testing as an in-house plugin. The plugin takes care of

1. Capturing screenshots
2. Comparison with baseline to highlight visual differences
3. Report to review the differences
4. Approve the changes

VRT can be done on real desktop & mobile browsers. Also, VRT can be run on components as part of component testing as well.
#### [API Testing](https://nightwatchjs.org/guide/writing-tests/api-testing.html)

API testing is now available with Nightwatch v3. The following functionality can be achieved with API testing

1. Request assertions
2. Response assertions
3. Viewing API tests in the HTML report
4. Mock server

#### [Accessibility Testing](https://nightwatchjs.org/guide/using-nightwatch/accessibility-testing.html)
Nightwatch v3 packages the aXe-core package developed by Deque Systems as a plugin. It enables 90 different types of accessibility tests for WCAG compliance.

## 🦉 About Nightwatch
Nightwatch was initially built by [@pineviewlabs](https://github.com/pineviewlabs/) - an independent software consultancy based in Oslo, Norway, with help from [contributors](https://github.com/nightwatchjs/nightwatch/graphs/contributors). In mid 2021, Nightwatch has become a part of the [@BrowserStack](https://github.com/browserstack) family and it is being developed further at the BrowserStack Open-source Program Office. Read more on [our blog](https://nightwatchjs.org/blog/nightwatch-has-joined-the-browserstack-family.html).

## Contributing

We welcome any and all contributions from the community which can help improve Nightwatch. Please check out [CONTRIBUTING.md](CONTRIBUTING.md) for more extensive contributing guidelines.

## Licence
[MIT](https://github.com/nightwatchjs/nightwatch/blob/main/LICENSE.md)

[discord-badge]: https://img.shields.io/discord/618399631038218240.svg?color=7389D8&labelColor=6A7EC2&logo=discord&logoColor=ffffff&style=flat-square&label=discord
[discord]: https://discord.gg/SN8Da2X

---

### Overview (`api/README.md`)

# Nightwatch API Commands
Public commands api exported by Nightwatch in order to be extended upon from outside.

**Example:**

```js
const {Quit: quit} = require('nightwatch/api');

module.exports = CustomQuit extends Quit {
  async command(cb = function(r) {return r}) {
    console.log('from custom quit command');
    
    await super.command(cb);
  }
}
```

**This is a work in progress.** 

## Element commands:
  - ### Finding elements:
    - [findElement](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/element-commands/findElement.js)
    - [findElements](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/element-commands/findElements.js)
    - [element](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/protocol/element.js)
    - [elements](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/protocol/elements.js)
    - [elementIdElement](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/protocol/elementIdElement.js)
    - [elementIdElements](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/protocol/elementIdElements.js)
    - [waitForElementPresent](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/element-commands/waitForElementPresent.js)
    - [waitForElementNotPresent](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/element-commands/waitForElementNotPresent.js)
    - [waitForElementVisible](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/element-commands/waitForElementVisible.js)
    - [waitForElementNotVisible](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/element-commands/waitForElementNotVisible.js)
  - ### Element interaction:
      - [clearValue](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/element-commands/clearValue.js)
      - [click](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/element-commands/click.js)
      - [moveToElement](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/element-commands/moveToElement.js)
      - [setValue](https://github.com/nightwatchjs/nightwatch/blob/main/lib/api/element-commands/setValue.js)
  - ### Element state:
  - ### Element location:
  - ### Element location:

## Document handling:

## Session related:

## Navigation:

## Window related:

## Frames:

## User actions:

## User prompts:

## Mobile related:

## Utilities & Debugging:

---

### Overview (`examples/cucumber-js/README.md`)

# Using Cucumber.js with Nightwatch 2

Nightwatch 2 brings integrated support for using [Cucumber.js](https://cucumber.io/) directly as an alternative test runner. No other plugins are necessary, other than the [Cucumber library](https://www.npmjs.com/package/@cucumber/cucumber) itself (version 7.3 or higher). 

Simply run the following in the same project where Nightwatch is also installed:

```sh
$ npm i @cucumber/cucumber --save-dev
```


## Configuration
```js
{
  test_runner: {
    // set cucumber as the runner
    type: 'cucumber',  
      
    // define cucumber specific options  
    options: {
      //set the feature path
      feature_path: 'examples/cucumber-js/*/*.feature',
      
      // start the webdriver session automatically (enabled by default)
      auto_start_session: true,
    
      // use parallel execution in Cucumber  
      parallel: 2 // set number of workers to use (can also be defined in the cli as --parallel 2
    }
  },
  
  src_folders: ['examples/cucumber-js/features/step_definitions']
}
```

## CLI Options
| option             |  description |
|------------------------|------------------------------|
| `--dry-run`        | Do all the aggregation work of looking at your feature files, loading your support code etc but without actually executing the tests. [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/dry_run.md)
| `--name`           | Specify a scenario by its name matching a regular expression. [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/cli.md#running-specific-features)    
| `--tags`             | Use tags to run specific scenario or features. [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/cli.md#tags)
| `--require`        | Use `--require <GLOB OR DIR OR FILE>` to explicitly require support files before executing the features. [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/cli.md#requiring-support-files)
| `--format`           | Use `--format <TYPE[:PATH]>` to specify the format of the output. [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/cli.md#formats)
| `--format-options`   | Many formatters, including the built-in ones, support some configurability via options. You can provide this data as a JSON literal via the --format-options CLI option. [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/formatters.md#options)         
| `--fail-fast`        | Abort the run on first failure (default: false). [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/cli.md#--fail-fast)        
| `--retries`            | Use `--retries <NUMBER-OF-ATTEMPT>` to have Cucumber attempt it multiple times until either it passes or the maximum number of attempts is reached. [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/retry.md#retry)       
| `--retry-tag-filter` |  Use `--retry-tag-filter` to retry failed scenarios based on tags. [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/retry.md#targeting-scenarios)
| `--require-module`   | [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/cli.md#transpilation)
| `--no-strict`        | By default, cucumber runner works in strict mode, meaning it will fail if there are pending steps.         
| `--parallel`         | Use `--parallel <NUMBER-OF-WORKER-THREADS>` to run your scenarios in parallel. [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/parallel.md#parallel)               
| `--profile`          | As of now only `cucumber.js` is considered while picking up profiles. [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/profiles.md)        
| `--world-parameters` | Provide this data as a JSON literal via the `--world-parameters`. [read more](https://github.com/cucumber/cucumber-js/blob/main/docs/support_files/world.md#world-parameters)


## Running
Cucumber spec files/step definition files can be provided in `src_folders` in Nightwatch config or as a CLI argument.

With `src_folders` defined:

```sh
$ npx nightwatch 
```

Without `src_folders` defined:

```sh
$ npx nightwatch examples/cucumber-js/features/step_definitions 
```

Parallel running using 2 workers:

```sh
$ nightwatch examples/cucumber-js/features/step_definitions --parallel 2 
```

Use other [test runner options](https://nightwatchjs.org/guide/running-tests/command-line-options.html) as usual:

```sh
$ npx nightwatch examples/cucumber-js/features/step_definitions --headless
```

## Disable the automatic session start
You might need sometimes to not start the Webdriver session automatically after Nightwatch is instantiated. For this purpose, Nightwatch provides the instance available as `this.client`, which contains an `launchBrowser()` method.

### Configuration:
```js
test_runner: {
  type: 'cucumber',
  options: {
    feature_path: 'examples/cucumber-js/*/*.feature',
    auto_start_session: false
  }
}
```

You can then use an extra setup file that you can pass as an extra `--require` to Nightwatch, which will be forwarded to Cucumber. In the extra setup file, you can add other operations needed to be executed before the session is started.

#### Example _extra_setup.js:

Remember to set the `browser` on `this` so it can be closed automatically by Nightwatch. Otherwise, remember to call `.quit()` in your own Cucumber `After()` hooks. 

```js
const {Before} = require('@cucumber/cucumber');

Before(async function(testCase) {
  if (!this.client) {
    console.error('Nightwatch instance was not created.');

    return;
  }

  this.client.updateCapabilities({
    testCap: 'testing'
  });

  this.browser = await this.client.launchBrowser();
});
```

#### Nightwatch setup file for Cucumber

You might also want to inspect the built-in setup file that Nightwatch uses for initializing the Cucumber runner. It is available in our project root folder at [/cucumber-js/_setup_cucumber_runner.js](https://github.com/nightwatchjs/nightwatch/blob/v2/cucumber-js/_setup_cucumber_runner.js).

#### Run with extra setup:

```sh
$ nightwatch examples/cucumber-js/features/step_definitions --require {/full/path/to/_extra_setup.js}
```

## Reporting
When using the integrated Cucumber test runner, you need to use the Cucumber [formatters](https://github.com/cucumber/cucumber-js/blob/main/docs/formatters.md) for generating output.

Nightwatch reporters (like JUnit XML reports or the [global custom reporter](https://nightwatchjs.org/guide/extending-nightwatch/custom-reporter.html)) are not available. The main reason is that reporting is delegated to the Cucumber CLI. You can also [write your own](https://github.com/cucumber/cucumber-js/blob/main/docs/custom_formatters.md) Cucumber formatter.

Nightwatch will forward `--format` and `--format-options` CLI arguments, if present, to Cucumber.

By default, the `progress` formatter is used. Here's how the output looks like when running the example tests in Firefox:

```sh
ℹ Connected to GeckoDriver on port 4444 (1740ms).
Using: firefox (92.0.1) on MAC (20.6.0).

..  ✔ Testing if the page title equals 'Rijksmuseum Amsterdam, home of the Dutch masters' (4ms)
.  ✔ Element <#rijksmuseum-app> was visible after 46 milliseconds.
.  ✔ Testing if element <.search-results> contains text 'Operation Night Watch' (1994ms)
...  ✔ Testing if the page title equals 'Rijksmuseum Amsterdam, home of the Dutch masters' (8ms)
.  ✔ Element <#rijksmuseum-app> was visible after 49 milliseconds.
.  ✔ Testing if element <.search-results> contains text 'The Night Watch, Rembrandt van Rijn, 1642' (1427ms)
.

2 scenarios (2 passed)
10 steps (10 passed)
0m13.024s (executing steps: 0m12.998s)
```

---

### Overview (`examples/tests/README.md`)

# Using Mocha as a test runner in Nightwatch 2

Nightwatch 2 brings support for Mocha v9 as an alternative test runner which contains most of the existing functionality from the Nightwatch default runner.

We are still in the process of updating the examples. Please check back here soon.

---

### Overview (`lib/runner/test-runners/cucumber/README.md`)

# Using Cucumber.js with Nightwatch 2

Nightwatch 2 brings integrated support for using [Cucumber.js](https://cucumber.io/) directly as an alternative test runner. No other plugins are necessary, other than the [Cucumber library](https://www.npmjs.com/package/@cucumber/cucumber) itself (version 7.3 or higher). 

Simply run the following in the same project where Nightwatch is also installed:

```sh
$ npm i @cucumber/cucumber --save-dev
```

The [examples folder](https://github.com/nightwatchjs/nightwatch/tree/v2/examples/cucumber-js) contains a few example spec files and cucumber features, as well as configuration details.

---

--- METRICS ---
- Files Extracted: 5
- Estimated Token Budget: ~4561 tokens
- Canonical Reference: https://codewiki.google/github.com/nightwatchjs/nightwatch
