-
Notifications
You must be signed in to change notification settings - Fork 1
/
bs-hub.js
56 lines (46 loc) · 1.52 KB
/
bs-hub.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
const request = require('sync-request');
var HUB = "http://hub-cloud.browserstack.com/wd/hub";
var session;
// Base quest function
function sendRequest(method, url, body) {
return request(method, url, {
json: body,
headers : {
"user-agent": "rohanimmanuel/bs-rerun",
"Authorization" : "Basic " + new Buffer.from(process.env.BROWSERSTACK_USERNAME + ":" + process.env.BROWSERSTACK_ACCESS_KEY).toString("base64")
}
});
}
// Start Session
function startSession(capabilities) {
console.log("STARTING SESSION");
var res = sendRequest("POST", HUB + "/session", {
desiredCapabilities: capabilities
});
var body = JSON.parse(res.getBody('utf8'))
session = body.sessionId;
console.log("SESSION STARTED AT: " + getSessionLink() + "");
return res;
}
// Stop Session
function stopSession() {
var res = sessionCommand("DELETE", "", {});
session = null;
console.log("SESSION STOPED");
return res;
}
// Session Command
function sessionCommand(method, endpoint, body) {
if(method != "DELETE" && endpoint != "") console.log(method + "\t" + endpoint);
return sendRequest(method, HUB + "/session/" + session + endpoint, body);
}
// Get Session Link
function getSessionLink() {
return "https://automate.browserstack.com/dashboard/v2/sessions/" + session;
}
// Module exports
module.exports.sendRequest = sendRequest;
module.exports.startSession = startSession;
module.exports.stopSession = stopSession;
module.exports.sessionCommand = sessionCommand;
module.exports.getSessionLink = getSessionLink;