Implementation:Nightwatchjs Nightwatch Assertion Type Definitions
| Knowledge Sources | |
|---|---|
| Domains | Testing, Type_System, Assertions |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
TypeScript type definitions for Nightwatch's built-in assertion API, including element assertions, page assertions, and Node.js assert wrappers.
Description
The assertions.d.ts file defines the complete TypeScript interface for Nightwatch's `browser.assert` and `browser.verify` namespaces. It includes:
- NightwatchCommonAssertions — Element-level assertions like `attributeContains`, `textEquals`, `visible`, `elementPresent`, `cssProperty`, `urlEquals`, `titleEquals`, and more.
- NightwatchNodeAssertions — Node.js `assert` module wrappers (`equal`, `deepEqual`, `strictEqual`, `throws`, `rejects`, etc.) integrated into the Nightwatch chain.
- NightwatchAssertions — Combined interface with `.not` negation support.
- Assert — The top-level type combining both common and Node assertions.
All assertion methods return `Awaitable` types, enabling both callback and async/await usage patterns.
⚠️ Deprecation Warning: This file contains 7 deprecated assertion methods (`elementNotPresent`, `cssClassNotPresent`, `cssClassPresent`, `containsText`, `title`, `value`, `hidden`) that have been superseded by modern equivalents. See Heuristic:Nightwatchjs_Nightwatch_Warning_Deprecated_API_Members for migration guidance.
Usage
These type definitions are consumed automatically when writing Nightwatch tests in TypeScript. They provide autocompletion and type checking for `browser.assert.*` and `browser.verify.*` calls.
Code Reference
Source Location
- Repository: Nightwatchjs_Nightwatch
- File: types/assertions.d.ts
- Lines: 1-672
Signature
export interface NightwatchCommonAssertions<ReturnType> {
attributeContains(selector: Definition, attribute: string, expected: string, message?: string): Awaitable<IfUnknown<ReturnType, this>, NightwatchAssertionsResult<string>>;
attributeEquals(selector: Definition, attribute: string, expected: string, message?: string): Awaitable<IfUnknown<ReturnType, this>, NightwatchAssertionsResult<string>>;
textContains(selector: Definition, expectedText: string, msg?: string): Awaitable<IfUnknown<ReturnType, this>, NightwatchAssertionsResult<string>>;
textEquals(selector: Definition, expectedText: string, msg?: string): Awaitable<IfUnknown<ReturnType, this>, NightwatchAssertionsResult<string>>;
visible(selector: Definition, message?: string): Awaitable<IfUnknown<ReturnType, this>, NightwatchAssertionsResult<boolean>>;
elementPresent(selector: ScopedSelector, msg?: string): Awaitable<IfUnknown<ReturnType, this>, NightwatchAssertionsResult<ElementResult[]>>;
urlEquals(expected: string, message?: string): Awaitable<IfUnknown<ReturnType, this>, NightwatchAssertionsResult<string>>;
titleEquals(expected: string, message?: string): Awaitable<IfUnknown<ReturnType, this>, NightwatchAssertionsResult<string>>;
// ... 30+ additional assertion methods
}
export interface NightwatchAssertions<ReturnType> extends NightwatchCommonAssertions<ReturnType>, NightwatchCustomAssertions<ReturnType> {
not: Omit<NightwatchAssertions<ReturnType>, 'not'>;
}
export interface Assert<ReturnType = unknown> extends NightwatchAssertions<ReturnType>, NightwatchNodeAssertions<ReturnType> {}
Import
import { Assert, NightwatchAssertions, NightwatchCommonAssertions } from 'nightwatch';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| selector | Definition / ScopedSelector | Yes (for element assertions) | CSS selector, XPath, or element reference |
| expected | string / number / RegExp | Yes | Expected value to assert against |
| message | string | No | Custom assertion failure message |
Outputs
| Name | Type | Description |
|---|---|---|
| NightwatchAssertionsResult | Object | Contains `value`, `status: 0`, `returned: 1`, `passed: true` |
| NightwatchNodeAssertionsResult | Object | Contains `value: null`, `returned: 1` |
Usage Examples
Element Assertions
// Using browser.assert in a Nightwatch test
describe('Assertion examples', function() {
it('demonstrates assertion types', async function(browser) {
await browser.assert.visible('#main');
await browser.assert.textContains('#heading', 'Welcome');
await browser.assert.attributeEquals('body', 'class', 'loaded');
await browser.assert.elementPresent('.content');
await browser.assert.urlContains('/dashboard');
await browser.assert.titleEquals('My App');
// Negated assertions
await browser.assert.not.visible('.hidden-element');
await browser.assert.not.elementPresent('.removed');
});
});
Node.js Assert Wrappers
describe('Node assert wrappers', function() {
it('uses Node.js assert methods', async function(browser) {
await browser.assert.ok(true, 'Value is truthy');
await browser.assert.equal(1 + 1, 2, 'Math works');
await browser.assert.strictEqual('hello', 'hello');
await browser.assert.deepEqual({ a: 1 }, { a: 1 });
});
});