Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Nightwatchjs Nightwatch CreateClient And LaunchBrowser

From Leeroopedia
Knowledge Sources
Domains Testing, API, Session_Management
Last Updated 2026-02-12 00:00 GMT

Overview

Concrete API for creating a Nightwatch client and launching a browser session via createClient() and launchBrowser().

Description

Nightwatch.createClient(options) returns a client wrapper object with launchBrowser(), updateCapabilities(), settings, cleanup(), runGlobalBeforeHook(), and runGlobalAfterHook() methods. Calling launchBrowser({ loadNightwatchApis: true }) initializes the internal client, creates a WebDriver session, and returns a Promise resolving to the browser (NightwatchAPI) instance.

Usage

Call createClient() with options, optionally update capabilities, then call launchBrowser() to get the browser instance. Always call cleanup() when done.

Code Reference

Source Location

  • Repository: nightwatch
  • File: lib/index.js (lines 34-204)

Signature

// Step 1: Create client
const client = Nightwatch.createClient(options);

// ClientWrapper interface:
client.launchBrowser(opts?: { loadNightwatchApis?: boolean }) -> Promise<NightwatchAPI>
client.updateCapabilities(value: Object) -> void
client.settings -> Object  // Read-only settings
client.cleanup() -> Promise<void>
client.runGlobalBeforeHook() -> Promise<void>
client.runGlobalAfterHook() -> Promise<void>

// Step 2: Launch browser
const browser = await client.launchBrowser();
// browser is now a NightwatchAPI instance

Import

const Nightwatch = require('nightwatch');

I/O Contract

Inputs

Name Type Required Description
options Object No Configuration options (see CreateClient_Options)
loadNightwatchApis boolean No Load Nightwatch API on browser object (default: true)

Outputs

Name Type Description
client ClientWrapper Object with launchBrowser, updateCapabilities, settings, cleanup
browser NightwatchAPI Full browser API with navigateTo, click, assert, expect, etc.

Usage Examples

Full Programmatic Lifecycle

const Nightwatch = require('nightwatch');

(async function() {
  // Step 1: Create client
  const client = Nightwatch.createClient({
    browserName: 'chrome',
    headless: true
  });

  // Optional: Update capabilities
  client.updateCapabilities({
    'goog:chromeOptions': {
      args: ['--window-size=1920,1080']
    }
  });

  // Step 2: Launch browser
  const browser = await client.launchBrowser();

  // Step 3: Use browser
  await browser.navigateTo('https://example.com');
  await browser.assert.titleContains('Example');

  // Step 4: Cleanup
  await browser.end();
  await client.cleanup();
})();

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment