Implementation:Nightwatchjs Nightwatch Session Cleanup API
| Knowledge Sources | |
|---|---|
| Domains | Testing, API, Resource_Management |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete API for terminating browser sessions and cleaning up Nightwatch client resources via browser.end(), browser.quit(), and client.cleanup().
Description
browser.end() ends the Nightwatch session and closes the browser. browser.quit() is an alias that terminates the WebDriver session. client.cleanup() waits for the command queue to complete, empties and resets the queue, removes all listeners, and resets the logger. For complete cleanup, call browser.end() followed by client.cleanup().
Usage
Call browser.end() to close the browser, then client.cleanup() to release internal resources. Essential in programmatic usage.
Code Reference
Source Location
- Repository: nightwatch
- File: lib/index.js (lines 174-180)
- File: api/index.js (line 13)
Signature
// Browser session termination
browser.end() -> Promise<void> // End session and close browser
browser.quit() -> Promise<void> // Alias: terminate WebDriver session
// Client resource cleanup
client.cleanup() -> Promise<void>
// Internally:
// await client.queue.waitForCompletion()
// client.queue.empty()
// client.queue.reset()
// client.queue.removeAllListeners()
// Logger.reset()
Import
// Methods available on browser and client objects
// No separate import needed
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| Active browser session | NightwatchAPI | Yes | Browser instance from launchBrowser() |
| Active client | ClientWrapper | Yes | Client from createClient() |
Outputs
| Name | Type | Description |
|---|---|---|
| Session closed | void | WebDriver session terminated, browser process ended |
| Queue cleared | void | Command queue emptied, listeners removed |
| Logger reset | void | Internal logger state cleared |
Usage Examples
Complete Cleanup
const Nightwatch = require('nightwatch');
(async function() {
const client = Nightwatch.createClient({
browserName: 'chrome',
headless: true
});
const browser = await client.launchBrowser();
try {
await browser.navigateTo('https://example.com');
await browser.assert.titleContains('Example');
} finally {
// Always cleanup, even on error
await browser.end();
await client.cleanup();
}
})();
Cleanup in Test Hooks
describe('Programmatic test', function() {
let client, browser;
before(async function() {
client = Nightwatch.createClient({ browserName: 'chrome' });
browser = await client.launchBrowser();
});
after(async function() {
await browser.end();
await client.cleanup();
});
it('runs a test', async function() {
await browser.navigateTo('https://example.com');
});
});