WebDriver/Selenium 2 Node.js client for browser automation and testing
—
HTTP configuration, timeout settings, method extension, and library customization for adapting WD to specific testing needs.
Configure HTTP settings for WebDriver communication with servers.
/**
* Configure HTTP options globally
* @param opts - HTTP configuration options
* @param cb - Callback receiving (err)
*/
configureHttp(opts: HttpConfig, cb?: callback): void;
/**
* Get current HTTP configuration
* @returns Current HTTP config object
*/
getHttpConfig(): HttpConfig;
interface HttpConfig {
timeout?: number; // Request timeout in milliseconds
retries?: number; // Number of retry attempts
retryDelay?: number; // Delay between retries in milliseconds
baseUrl?: string; // Base URL for relative requests
proxy?: string; // Proxy server URL
rejectUnauthorized?: boolean; // Reject unauthorized SSL certificates
headers?: object; // Additional HTTP headers
}Usage Examples:
// Configure HTTP settings
wd.configureHttp({
timeout: 60000, // 60 second timeout
retries: 3, // Retry failed requests 3 times
retryDelay: 15000, // Wait 15 seconds between retries
baseUrl: 'http://selenium-grid.example.com:4444'
});
// Configure for cloud testing
wd.configureHttp({
timeout: 120000, // Longer timeout for cloud
retries: 5,
proxy: 'http://corporate-proxy:8080'
});
// Get current configuration
const currentConfig = wd.getHttpConfig();
console.log('HTTP timeout:', currentConfig.timeout);
console.log('Retry count:', currentConfig.retries);
// Per-browser configuration
browser.configureHttp({
timeout: 30000,
headers: {
'X-Custom-Header': 'test-automation'
}
});Set various timeout values for different WebDriver operations.
/**
* Set HTTP request timeout
* @param ms - Timeout in milliseconds
* @param cb - Callback receiving (err)
*/
setHttpTimeout(ms: number, cb?: callback): void;
setHTTPInactivityTimeout(ms: number, cb?: callback): void; // Alias
/**
* Set implicit wait timeout for element finding
* @param ms - Timeout in milliseconds
* @param cb - Callback receiving (err)
*/
setImplicitWaitTimeout(ms: number, cb?: callback): void;
setWaitTimeout(ms: number, cb?: callback): void; // Alias
/**
* Set timeout for async script execution
* @param ms - Timeout in milliseconds
* @param cb - Callback receiving (err)
*/
setAsyncScriptTimeout(ms: number, cb?: callback): void;
/**
* Set page load timeout
* @param ms - Timeout in milliseconds
* @param cb - Callback receiving (err)
*/
setPageLoadTimeout(ms: number, cb?: callback): void;
/**
* Set command timeout (Appium)
* @param ms - Timeout in milliseconds
* @param cb - Callback receiving (err)
*/
setCommandTimeout(ms: number, cb?: callback): void;Usage Examples:
// Configure timeouts for slow environments
browser.setHttpTimeout(90000); // 90 second HTTP timeout
browser.setImplicitWaitTimeout(10000); // 10 second implicit wait
browser.setAsyncScriptTimeout(30000); // 30 second async script timeout
browser.setPageLoadTimeout(60000); // 60 second page load timeout
// Mobile testing timeouts
browser.setCommandTimeout(120000); // 2 minute command timeout for mobile
// Promise chain timeout configuration
browser
.init(capabilities)
.setImplicitWaitTimeout(5000)
.setPageLoadTimeout(30000)
.get('http://slow-website.com');
// Different timeouts for different test phases
function configureForLogin() {
browser.setImplicitWaitTimeout(15000); // Longer wait for login elements
}
function configureForNavigation() {
browser.setImplicitWaitTimeout(5000); // Shorter wait for navigation
browser.setPageLoadTimeout(45000); // Longer page load for heavy pages
}Add custom methods to WebDriver and Element classes.
/**
* Add async method to all WebDriver types
* @param name - Method name
* @param method - Method function
*/
addAsyncMethod(name: string, method: function): void;
/**
* Add async method to all Element types
* @param name - Method name
* @param method - Method function
*/
addElementAsyncMethod(name: string, method: function): void;
/**
* Add promise method to promise WebDriver types
* @param name - Method name
* @param method - Method function
*/
addPromiseMethod(name: string, method: function): void;
/**
* Add promise method to promise Element types
* @param name - Method name
* @param method - Method function
*/
addElementPromiseMethod(name: string, method: function): void;
/**
* Add promise chain method to promise chain WebDriver
* @param name - Method name
* @param method - Method function
*/
addPromiseChainMethod(name: string, method: function): void;
/**
* Add promise chain method to promise chain Element
* @param name - Method name
* @param method - Method function
*/
addElementPromiseChainMethod(name: string, method: function): void;
/**
* Remove method from all WebDriver types
* @param name - Method name
*/
removeMethod(name: string): void;Usage Examples:
// Add custom WebDriver method
wd.addAsyncMethod('waitForPageTitle', function(expectedTitle, timeout, cb) {
const browser = this;
const startTime = Date.now();
function checkTitle() {
browser.title(function(err, title) {
if (err) return cb(err);
if (title === expectedTitle) {
return cb(null, title);
}
if (Date.now() - startTime > timeout) {
return cb(new Error('Title timeout'));
}
setTimeout(checkTitle, 500);
});
}
checkTitle();
});
// Add custom Element method
wd.addElementAsyncMethod('waitForText', function(expectedText, timeout, cb) {
const element = this;
const startTime = Date.now();
function checkText() {
element.text(function(err, text) {
if (err) return cb(err);
if (text.includes(expectedText)) {
return cb(null, text);
}
if (Date.now() - startTime > timeout) {
return cb(new Error('Text timeout'));
}
setTimeout(checkText, 500);
});
}
checkText();
});
// Use custom methods
browser.waitForPageTitle('Login Complete', 10000, function(err, title) {
console.log('Page title changed to:', title);
});
browser.elementById('status-message', function(err, element) {
element.waitForText('Success', 5000, function(err, text) {
console.log('Status updated:', text);
});
});
// Add utility methods
wd.addAsyncMethod('fillForm', function(formData, cb) {
const browser = this;
const fields = Object.keys(formData);
let completed = 0;
fields.forEach(fieldId => {
browser.elementById(fieldId, function(err, element) {
if (err) return cb(err);
element.clear(function(err) {
if (err) return cb(err);
element.type(formData[fieldId], function(err) {
if (err) return cb(err);
completed++;
if (completed === fields.length) {
cb(null);
}
});
});
});
});
});
// Use utility method
browser.fillForm({
'username': 'testuser',
'password': 'testpass',
'email': 'test@example.com'
}, function(err) {
console.log('Form filled successfully');
});Control deprecation warning visibility.
/**
* Show or hide deprecation warnings
* @param show - Whether to show deprecation warnings
*/
showHideDeprecation(show: boolean): void;Usage Examples:
// Hide deprecation warnings in production
wd.showHideDeprecation(false);
// Show deprecation warnings during development
wd.showHideDeprecation(true);
// Conditional deprecation warnings
if (process.env.NODE_ENV === 'development') {
wd.showHideDeprecation(true);
} else {
wd.showHideDeprecation(false);
}Configure and switch between different WebDriver patterns.
Usage Examples:
// Factory functions with explicit driver types
const asyncBrowser = wd.remote('async');
const promiseBrowser = wd.remote('promise');
const chainBrowser = wd.remote('promiseChain');
// Configure different browsers for different purposes
const browsers = {
smoke: wd.promiseChainRemote(), // Chain for quick smoke tests
regression: wd.promiseRemote(), // Promise for complex regression
performance: wd.asyncRemote() // Async for performance testing
};
// Initialize with different configurations
browsers.smoke.init({browserName: 'chrome', chromeOptions: {args: ['--headless']}});
browsers.regression.init({browserName: 'firefox'});
browsers.performance.init({browserName: 'chrome', chromeOptions: {args: ['--disable-dev-shm-usage']}});Complex configuration scenarios for sophisticated test setups.
Usage Examples:
// Environment-specific configuration
function configureForEnvironment(env) {
const configs = {
local: {
timeout: 30000,
retries: 1,
baseUrl: 'http://localhost:4444'
},
staging: {
timeout: 60000,
retries: 3,
baseUrl: 'http://selenium-staging.company.com:4444',
proxy: 'http://proxy.company.com:8080'
},
production: {
timeout: 120000,
retries: 5,
baseUrl: 'https://selenium-prod.company.com:4444',
headers: {
'Authorization': 'Bearer ' + process.env.SELENIUM_TOKEN
}
}
};
wd.configureHttp(configs[env] || configs.local);
}
// Browser pool configuration
class BrowserPool {
constructor(size = 3) {
this.browsers = [];
this.available = [];
for (let i = 0; i < size; i++) {
const browser = wd.promiseChainRemote();
browser.poolId = i;
this.browsers.push(browser);
this.available.push(browser);
}
}
async getBrowser() {
if (this.available.length === 0) {
throw new Error('No browsers available in pool');
}
const browser = this.available.pop();
await browser.init({browserName: 'chrome'});
return browser;
}
releaseBrowser(browser) {
browser.quit().then(() => {
this.available.push(browser);
});
}
}
// Custom configuration helper
function createConfiguredBrowser(options = {}) {
const browser = wd.promiseChainRemote(options.serverUrl);
// Apply timeouts
if (options.timeouts) {
Object.keys(options.timeouts).forEach(timeoutType => {
const methodName = `set${timeoutType}Timeout`;
if (browser[methodName]) {
browser[methodName](options.timeouts[timeoutType]);
}
});
}
// Add custom methods
if (options.customMethods) {
Object.keys(options.customMethods).forEach(methodName => {
wd.addPromiseChainMethod(methodName, options.customMethods[methodName]);
});
}
// Configure HTTP
if (options.http) {
browser.configureHttp(options.http);
}
return browser;
}
// Usage of configuration helper
const browser = createConfiguredBrowser({
serverUrl: 'http://selenium-hub:4444/wd/hub',
timeouts: {
implicit: 10000,
pageLoad: 60000,
asyncScript: 30000
},
http: {
timeout: 90000,
retries: 3
},
customMethods: {
loginAs: function(username, password) {
return this
.elementById('username')
.clear()
.type(username)
.elementById('password')
.clear()
.type(password)
.elementById('login-button')
.click();
}
}
});Install with Tessl CLI
npx tessl i tessl/npm-wd