-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
150 lines (124 loc) · 4.25 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const request = require('sync-request');
const BSHub = require('./bs-hub');
// download raw logs
function downloadRawLogs(url) {
var res = request("GET", url, {});
return res.getBody('utf8');
}
// analyze raw logs
function getSessionDetails(url) {
// Prep Logs
console.log("Downloading Raw Logs");
var rawLogs = downloadRawLogs(url);
console.log("Analyzing Raw Logs");
var lineBreak = "\r\n" + rawLogs.substr(0,4);
var lines = rawLogs.split(lineBreak);
// init variables
var requests = [];
var responces = [];
var elements = {};
var windows = {};
// get request method endpoint and body
// and get responce body
lines.forEach((line, i) => {
// Remove create session requests and responces
if([0,1,2,3].includes(i)) return;
// Prep line
line = line.split(" ");
// Requests
if(line[2] == "REQUEST") {
var method = line[5];
var endpoint = "/" + line[6].split("/").slice(3).join("/");
var data = JSON.parse(line.slice(7).join(" "));
if(data == "") data = '{}';
requests.push({
method: method,
endpoint: endpoint,
data: data
});
}
// Responces
else if(line[2] == "RESPONSE") {
responces.push(JSON.parse(line.slice(3).join(" ")).value);
}
});
// update
requests.forEach((req, i) => {
if(req.endpoint == '/element') {
elements[Object.values(responces[i])[0]] = [i, null];
}
else if(req.endpoint == '/elements') {
Object.values(responces[i]).forEach((elmID, j) => {
elements[elmID] = [i, j];
});
}
else if(req.endpoint.includes('/element/')) {
if(req.endpoint.includes("/active")) return;
var elmID = req.endpoint.split('/')[2];
requests[i].update = elements[elmID];
requests[i].update.unshift(elmID);
}
else if(req.endpoint == '/window_handle') {
windows[responces[i]] = [i, null];
}
else if(req.endpoint == '/window_handles') {
responces[i].forEach((windowsHandle, j) => {
windows[windowsHandle] = [i, j];
});
}
else if(req.endpoint == '/window' && req.method == 'POST') {
var windowsHandle = req.data.name;
requests[i].update = windows[windowsHandle];
requests[i].update.unshift(windowsHandle);
}
else if(req.endpoint.includes('/window/')) {
var windowsHandle = req.endpoint.split('/')[2];
requests[i].update = windows[windowsHandle];
requests[i].update.unshift(windowsHandle);
}
});
return requests;
}
function updateEndpoint(endpoint, base, elmid) {
return endpoint.replace(base, elmid);
}
function runSession(capabilities, url) {
var responces = [];
var ogRequests = getSessionDetails(url);
BSHub.startSession(capabilities);
ogRequests.forEach(req => {
if(req.hasOwnProperty('update')) {
if(req.endpoint.includes('/element/')) {
if(req.update[2] == null) {
// update element id form /element
req.endpoint = updateEndpoint(req.endpoint, req.update[0], Object.values(responces[req.update[1]])[0]);
} else {
// update element id form /elements
req.endpoint = updateEndpoint(req.endpoint, req.update[0], Object.values(responces[req.update[1]])[req.update[2]]);
}
}
else if(req.endpoint.includes('/window/')) {
if(req.update[2] == null) {
// update windowsHandle id form /window
req.endpoint = updateEndpoint(req.endpoint, req.update[0], responces[req.update[1]]);
} else {
// update windowsHandle id form /windows
req.endpoint = updateEndpoint(req.endpoint, req.update[0], responces[req.update[1]][req.update[2]]);
}
}
else if(req.endpoint == '/window' && req.method == 'POST') {
if(req.update[2] == null) {
// update windowsHandle id form /window
req.data.name = responces[req.update[1]];
} else {
// update windowsHandle id form /windows
req.data.name = responces[req.update[1]][req.update[2]];
}
}
}
responces.push(JSON.parse(BSHub.sessionCommand(req.method, req.endpoint, req.data).getBody('utf8')).value);
});
BSHub.stopSession();
;}
// export module
module.exports.runSession = runSession;