-
Notifications
You must be signed in to change notification settings - Fork 2
/
driverMetadata.js
61 lines (56 loc) · 2 KB
/
driverMetadata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// const utils = require('@percy/sdk-utils');
const { Cache } = require('./cache');
const { RequestInterceptor } = require('node-request-interceptor');
const withDefaultInterceptors = require('node-request-interceptor/lib/presets/default');
class DriverMetadata {
constructor(driver) {
this.driver = driver;
this.sessionId = null;
if (this.driver.constructor.name === 'Browser') {
this.type = 'wdio';
} else {
this.type = 'wd';
}
}
async getSessionId() {
if (!this.sessionId) {
if (this.type === 'wdio') this.sessionId = await this.driver.sessionId;
if (this.type === 'wd') this.sessionId = await (await this.driver.getSession()).getId();
}
return this.sessionId;
}
async getCapabilities() {
return await Cache.withCache(Cache.capabilities, await this.getSessionId(), async () => {
if (this.type === 'wdio') {
return await this.driver.capabilities;
} else {
const session = await this.driver.getSession();
const capabilities = Object.fromEntries(session.getCapabilities().map_);
return capabilities;
}
});
}
async getCommandExecutorUrl() {
return await Cache.withCache(Cache.commandExecutorUrl, await this.getSessionId(), async () => {
if (this.type === 'wdio') {
return `${this.driver.options.protocol}://${this.driver.options.hostname}${this.driver.options.path}`;
} else {
// To intercept request from driver. used to get remote server url
const interceptor = new RequestInterceptor(withDefaultInterceptors.default);
let commandExecutorUrl = '';
interceptor.use((req) => {
const url = req.url.href;
commandExecutorUrl = url.split('/session')[0];
});
// making a call so we can intercept commandExecutorUrl
await this.driver.getCurrentUrl();
// To stop intercepting request
interceptor.restore();
return commandExecutorUrl;
}
});
}
}
module.exports = {
DriverMetadata
};