0
# Configuration & Extension
1
2
HTTP configuration, timeout settings, method extension, and library customization for adapting WD to specific testing needs.
3
4
## Capabilities
5
6
### HTTP Configuration
7
8
Configure HTTP settings for WebDriver communication with servers.
9
10
```javascript { .api }
11
/**
12
* Configure HTTP options globally
13
* @param opts - HTTP configuration options
14
* @param cb - Callback receiving (err)
15
*/
16
configureHttp(opts: HttpConfig, cb?: callback): void;
17
18
/**
19
* Get current HTTP configuration
20
* @returns Current HTTP config object
21
*/
22
getHttpConfig(): HttpConfig;
23
24
interface HttpConfig {
25
timeout?: number; // Request timeout in milliseconds
26
retries?: number; // Number of retry attempts
27
retryDelay?: number; // Delay between retries in milliseconds
28
baseUrl?: string; // Base URL for relative requests
29
proxy?: string; // Proxy server URL
30
rejectUnauthorized?: boolean; // Reject unauthorized SSL certificates
31
headers?: object; // Additional HTTP headers
32
}
33
```
34
35
**Usage Examples:**
36
37
```javascript
38
// Configure HTTP settings
39
wd.configureHttp({
40
timeout: 60000, // 60 second timeout
41
retries: 3, // Retry failed requests 3 times
42
retryDelay: 15000, // Wait 15 seconds between retries
43
baseUrl: 'http://selenium-grid.example.com:4444'
44
});
45
46
// Configure for cloud testing
47
wd.configureHttp({
48
timeout: 120000, // Longer timeout for cloud
49
retries: 5,
50
proxy: 'http://corporate-proxy:8080'
51
});
52
53
// Get current configuration
54
const currentConfig = wd.getHttpConfig();
55
console.log('HTTP timeout:', currentConfig.timeout);
56
console.log('Retry count:', currentConfig.retries);
57
58
// Per-browser configuration
59
browser.configureHttp({
60
timeout: 30000,
61
headers: {
62
'X-Custom-Header': 'test-automation'
63
}
64
});
65
```
66
67
### Timeout Configuration
68
69
Set various timeout values for different WebDriver operations.
70
71
```javascript { .api }
72
/**
73
* Set HTTP request timeout
74
* @param ms - Timeout in milliseconds
75
* @param cb - Callback receiving (err)
76
*/
77
setHttpTimeout(ms: number, cb?: callback): void;
78
setHTTPInactivityTimeout(ms: number, cb?: callback): void; // Alias
79
80
/**
81
* Set implicit wait timeout for element finding
82
* @param ms - Timeout in milliseconds
83
* @param cb - Callback receiving (err)
84
*/
85
setImplicitWaitTimeout(ms: number, cb?: callback): void;
86
setWaitTimeout(ms: number, cb?: callback): void; // Alias
87
88
/**
89
* Set timeout for async script execution
90
* @param ms - Timeout in milliseconds
91
* @param cb - Callback receiving (err)
92
*/
93
setAsyncScriptTimeout(ms: number, cb?: callback): void;
94
95
/**
96
* Set page load timeout
97
* @param ms - Timeout in milliseconds
98
* @param cb - Callback receiving (err)
99
*/
100
setPageLoadTimeout(ms: number, cb?: callback): void;
101
102
/**
103
* Set command timeout (Appium)
104
* @param ms - Timeout in milliseconds
105
* @param cb - Callback receiving (err)
106
*/
107
setCommandTimeout(ms: number, cb?: callback): void;
108
```
109
110
**Usage Examples:**
111
112
```javascript
113
// Configure timeouts for slow environments
114
browser.setHttpTimeout(90000); // 90 second HTTP timeout
115
browser.setImplicitWaitTimeout(10000); // 10 second implicit wait
116
browser.setAsyncScriptTimeout(30000); // 30 second async script timeout
117
browser.setPageLoadTimeout(60000); // 60 second page load timeout
118
119
// Mobile testing timeouts
120
browser.setCommandTimeout(120000); // 2 minute command timeout for mobile
121
122
// Promise chain timeout configuration
123
browser
124
.init(capabilities)
125
.setImplicitWaitTimeout(5000)
126
.setPageLoadTimeout(30000)
127
.get('http://slow-website.com');
128
129
// Different timeouts for different test phases
130
function configureForLogin() {
131
browser.setImplicitWaitTimeout(15000); // Longer wait for login elements
132
}
133
134
function configureForNavigation() {
135
browser.setImplicitWaitTimeout(5000); // Shorter wait for navigation
136
browser.setPageLoadTimeout(45000); // Longer page load for heavy pages
137
}
138
```
139
140
### Method Extension
141
142
Add custom methods to WebDriver and Element classes.
143
144
```javascript { .api }
145
/**
146
* Add async method to all WebDriver types
147
* @param name - Method name
148
* @param method - Method function
149
*/
150
addAsyncMethod(name: string, method: function): void;
151
152
/**
153
* Add async method to all Element types
154
* @param name - Method name
155
* @param method - Method function
156
*/
157
addElementAsyncMethod(name: string, method: function): void;
158
159
/**
160
* Add promise method to promise WebDriver types
161
* @param name - Method name
162
* @param method - Method function
163
*/
164
addPromiseMethod(name: string, method: function): void;
165
166
/**
167
* Add promise method to promise Element types
168
* @param name - Method name
169
* @param method - Method function
170
*/
171
addElementPromiseMethod(name: string, method: function): void;
172
173
/**
174
* Add promise chain method to promise chain WebDriver
175
* @param name - Method name
176
* @param method - Method function
177
*/
178
addPromiseChainMethod(name: string, method: function): void;
179
180
/**
181
* Add promise chain method to promise chain Element
182
* @param name - Method name
183
* @param method - Method function
184
*/
185
addElementPromiseChainMethod(name: string, method: function): void;
186
187
/**
188
* Remove method from all WebDriver types
189
* @param name - Method name
190
*/
191
removeMethod(name: string): void;
192
```
193
194
**Usage Examples:**
195
196
```javascript
197
// Add custom WebDriver method
198
wd.addAsyncMethod('waitForPageTitle', function(expectedTitle, timeout, cb) {
199
const browser = this;
200
const startTime = Date.now();
201
202
function checkTitle() {
203
browser.title(function(err, title) {
204
if (err) return cb(err);
205
206
if (title === expectedTitle) {
207
return cb(null, title);
208
}
209
210
if (Date.now() - startTime > timeout) {
211
return cb(new Error('Title timeout'));
212
}
213
214
setTimeout(checkTitle, 500);
215
});
216
}
217
218
checkTitle();
219
});
220
221
// Add custom Element method
222
wd.addElementAsyncMethod('waitForText', function(expectedText, timeout, cb) {
223
const element = this;
224
const startTime = Date.now();
225
226
function checkText() {
227
element.text(function(err, text) {
228
if (err) return cb(err);
229
230
if (text.includes(expectedText)) {
231
return cb(null, text);
232
}
233
234
if (Date.now() - startTime > timeout) {
235
return cb(new Error('Text timeout'));
236
}
237
238
setTimeout(checkText, 500);
239
});
240
}
241
242
checkText();
243
});
244
245
// Use custom methods
246
browser.waitForPageTitle('Login Complete', 10000, function(err, title) {
247
console.log('Page title changed to:', title);
248
});
249
250
browser.elementById('status-message', function(err, element) {
251
element.waitForText('Success', 5000, function(err, text) {
252
console.log('Status updated:', text);
253
});
254
});
255
256
// Add utility methods
257
wd.addAsyncMethod('fillForm', function(formData, cb) {
258
const browser = this;
259
const fields = Object.keys(formData);
260
let completed = 0;
261
262
fields.forEach(fieldId => {
263
browser.elementById(fieldId, function(err, element) {
264
if (err) return cb(err);
265
266
element.clear(function(err) {
267
if (err) return cb(err);
268
269
element.type(formData[fieldId], function(err) {
270
if (err) return cb(err);
271
272
completed++;
273
if (completed === fields.length) {
274
cb(null);
275
}
276
});
277
});
278
});
279
});
280
});
281
282
// Use utility method
283
browser.fillForm({
284
'username': 'testuser',
285
'password': 'testpass',
286
'email': 'test@example.com'
287
}, function(err) {
288
console.log('Form filled successfully');
289
});
290
```
291
292
### Deprecation Management
293
294
Control deprecation warning visibility.
295
296
```javascript { .api }
297
/**
298
* Show or hide deprecation warnings
299
* @param show - Whether to show deprecation warnings
300
*/
301
showHideDeprecation(show: boolean): void;
302
```
303
304
**Usage Examples:**
305
306
```javascript
307
// Hide deprecation warnings in production
308
wd.showHideDeprecation(false);
309
310
// Show deprecation warnings during development
311
wd.showHideDeprecation(true);
312
313
// Conditional deprecation warnings
314
if (process.env.NODE_ENV === 'development') {
315
wd.showHideDeprecation(true);
316
} else {
317
wd.showHideDeprecation(false);
318
}
319
```
320
321
### Driver Type Management
322
323
Configure and switch between different WebDriver patterns.
324
325
**Usage Examples:**
326
327
```javascript
328
// Factory functions with explicit driver types
329
const asyncBrowser = wd.remote('async');
330
const promiseBrowser = wd.remote('promise');
331
const chainBrowser = wd.remote('promiseChain');
332
333
// Configure different browsers for different purposes
334
const browsers = {
335
smoke: wd.promiseChainRemote(), // Chain for quick smoke tests
336
regression: wd.promiseRemote(), // Promise for complex regression
337
performance: wd.asyncRemote() // Async for performance testing
338
};
339
340
// Initialize with different configurations
341
browsers.smoke.init({browserName: 'chrome', chromeOptions: {args: ['--headless']}});
342
browsers.regression.init({browserName: 'firefox'});
343
browsers.performance.init({browserName: 'chrome', chromeOptions: {args: ['--disable-dev-shm-usage']}});
344
```
345
346
### Advanced Configuration Patterns
347
348
Complex configuration scenarios for sophisticated test setups.
349
350
**Usage Examples:**
351
352
```javascript
353
// Environment-specific configuration
354
function configureForEnvironment(env) {
355
const configs = {
356
local: {
357
timeout: 30000,
358
retries: 1,
359
baseUrl: 'http://localhost:4444'
360
},
361
staging: {
362
timeout: 60000,
363
retries: 3,
364
baseUrl: 'http://selenium-staging.company.com:4444',
365
proxy: 'http://proxy.company.com:8080'
366
},
367
production: {
368
timeout: 120000,
369
retries: 5,
370
baseUrl: 'https://selenium-prod.company.com:4444',
371
headers: {
372
'Authorization': 'Bearer ' + process.env.SELENIUM_TOKEN
373
}
374
}
375
};
376
377
wd.configureHttp(configs[env] || configs.local);
378
}
379
380
// Browser pool configuration
381
class BrowserPool {
382
constructor(size = 3) {
383
this.browsers = [];
384
this.available = [];
385
386
for (let i = 0; i < size; i++) {
387
const browser = wd.promiseChainRemote();
388
browser.poolId = i;
389
this.browsers.push(browser);
390
this.available.push(browser);
391
}
392
}
393
394
async getBrowser() {
395
if (this.available.length === 0) {
396
throw new Error('No browsers available in pool');
397
}
398
399
const browser = this.available.pop();
400
await browser.init({browserName: 'chrome'});
401
return browser;
402
}
403
404
releaseBrowser(browser) {
405
browser.quit().then(() => {
406
this.available.push(browser);
407
});
408
}
409
}
410
411
// Custom configuration helper
412
function createConfiguredBrowser(options = {}) {
413
const browser = wd.promiseChainRemote(options.serverUrl);
414
415
// Apply timeouts
416
if (options.timeouts) {
417
Object.keys(options.timeouts).forEach(timeoutType => {
418
const methodName = `set${timeoutType}Timeout`;
419
if (browser[methodName]) {
420
browser[methodName](options.timeouts[timeoutType]);
421
}
422
});
423
}
424
425
// Add custom methods
426
if (options.customMethods) {
427
Object.keys(options.customMethods).forEach(methodName => {
428
wd.addPromiseChainMethod(methodName, options.customMethods[methodName]);
429
});
430
}
431
432
// Configure HTTP
433
if (options.http) {
434
browser.configureHttp(options.http);
435
}
436
437
return browser;
438
}
439
440
// Usage of configuration helper
441
const browser = createConfiguredBrowser({
442
serverUrl: 'http://selenium-hub:4444/wd/hub',
443
timeouts: {
444
implicit: 10000,
445
pageLoad: 60000,
446
asyncScript: 30000
447
},
448
http: {
449
timeout: 90000,
450
retries: 3
451
},
452
customMethods: {
453
loginAs: function(username, password) {
454
return this
455
.elementById('username')
456
.clear()
457
.type(username)
458
.elementById('password')
459
.clear()
460
.type(password)
461
.elementById('login-button')
462
.click();
463
}
464
}
465
});
466
```