Utility package providing comprehensive TypeScript type definitions for the WebdriverIO ecosystem
—
Network request and cookie types for WebDriver Bidi protocol, enabling monitoring and manipulation of network traffic during browser automation.
Core network request interface for WebDriver Bidi protocol.
/**
* Network request during a WebDriver Bidi session
*/
interface Request {
/** Unique request identifier */
id?: string;
/** Request URL */
url: string;
/** Request timestamp */
timestamp: number;
/** Navigation identifier */
navigation?: string;
/** Chain of redirect URLs */
redirectChain?: string[];
/** Request headers */
headers: Record<string, string>;
/** Request cookies */
cookies?: NetworkCookie[];
/** Error message if request failed */
error?: string;
/** Response information */
response?: {
/** Whether response came from cache */
fromCache: boolean;
/** Response headers */
headers: Record<string, string>;
/** Response MIME type */
mimeType: string;
/** HTTP status code */
status: number;
};
/**
* List of child requests made due to the main request.
* Note: the list may be incomplete and does not contain requests
* made after the command has finished.
* The property will be undefined if the request is not a document
* request initiated by the browser.
*/
children?: Request[];
}Cookie information for network requests and responses.
/**
* Network cookie interface
*/
interface NetworkCookie extends Extensible {
/** Cookie name */
name: string;
/** Cookie value */
value: string;
/** Cookie domain */
domain: string;
/** Cookie path */
path: string;
/** Cookie size in bytes */
size: number;
/** Whether cookie is HTTP-only */
httpOnly: boolean;
/** Whether cookie requires secure connection */
secure: boolean;
/** SameSite attribute */
sameSite: NetworkSameSite;
/** Cookie expiry timestamp (optional) */
expiry?: number;
}
/**
* Cookie SameSite attribute values
*/
type NetworkSameSite = 'strict' | 'lax' | 'none';
/**
* Extensible interface for additional properties
*/
type Extensible = Record<string, unknown>;Usage Examples:
import type { Network } from "@wdio/types";
// Network request handler
class NetworkMonitor {
private requests: Network.Request[] = [];
onRequest(request: Network.Request) {
console.log(`Request: ${request.url}`);
this.requests.push(request);
// Log headers
Object.entries(request.headers).forEach(([name, value]) => {
console.log(` ${name}: ${value}`);
});
// Log cookies
if (request.cookies) {
request.cookies.forEach(cookie => {
console.log(` Cookie: ${cookie.name}=${cookie.value}`);
});
}
}
onResponse(request: Network.Request) {
if (request.response) {
const { status, mimeType, fromCache } = request.response;
console.log(`Response: ${status} ${mimeType} ${fromCache ? '(cached)' : ''}`);
if (request.error) {
console.error(`Request failed: ${request.error}`);
}
}
}
getRequestsByDomain(domain: string): Network.Request[] {
return this.requests.filter(req => {
try {
return new URL(req.url).hostname.includes(domain);
} catch {
return false;
}
});
}
getFailedRequests(): Network.Request[] {
return this.requests.filter(req => req.error ||
(req.response && req.response.status >= 400));
}
}
// Cookie utilities
class CookieManager {
static createCookie(
name: string,
value: string,
options: Partial<Network.NetworkCookie> = {}
): Network.NetworkCookie {
return {
name,
value,
domain: options.domain || '',
path: options.path || '/',
size: (name + value).length,
httpOnly: options.httpOnly || false,
secure: options.secure || false,
sameSite: options.sameSite || 'lax',
expiry: options.expiry,
...options
};
}
static isExpired(cookie: Network.NetworkCookie): boolean {
if (!cookie.expiry) return false;
return Date.now() > cookie.expiry * 1000;
}
static matchesDomain(cookie: Network.NetworkCookie, domain: string): boolean {
if (cookie.domain.startsWith('.')) {
return domain.endsWith(cookie.domain.slice(1));
}
return domain === cookie.domain;
}
}
// Request filtering and analysis
function analyzeNetworkTraffic(requests: Network.Request[]) {
const stats = {
total: requests.length,
successful: 0,
failed: 0,
cached: 0,
byDomain: new Map<string, number>(),
byStatus: new Map<number, number>()
};
requests.forEach(request => {
// Count by domain
try {
const domain = new URL(request.url).hostname;
stats.byDomain.set(domain, (stats.byDomain.get(domain) || 0) + 1);
} catch {}
// Count by status
if (request.response) {
const status = request.response.status;
stats.byStatus.set(status, (stats.byStatus.get(status) || 0) + 1);
if (status >= 200 && status < 400) {
stats.successful++;
} else {
stats.failed++;
}
if (request.response.fromCache) {
stats.cached++;
}
} else if (request.error) {
stats.failed++;
}
});
return stats;
}Install with Tessl CLI
npx tessl i tessl/npm-wdio--types