Implementation:Nightwatchjs Nightwatch CreateClient And LaunchBrowser
| 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
- Environment:Nightwatchjs_Nightwatch_Node_18_Runtime
- Environment:Nightwatchjs_Nightwatch_Selenium_WebDriver_4