Created
January 31, 2018 14:28
-
-
Save ashwingonsalves/ec0d1df50e1603e5286eefce3c234a99 to your computer and use it in GitHub Desktop.
Pass a BrowserStack session ID to retrieve the HAR logs (provided "browserstack.networkLogs" = true is used)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https'); | |
const { exec } = require('child_process'); | |
function getHarLogs (url) { | |
return new Promise ( (resolve, reject)=> { | |
https.get(url, (res) => { | |
if (res.statusCode !== 200){ | |
throw "ERROR unable to retrieve HAR logs from BrowserStack: StatusCode - " + res.statusCode; | |
} | |
res.setEncoding('utf8'); | |
var buffer = ""; | |
res.on('data', (harLogs) => { | |
buffer += harLogs; | |
}); | |
res.on('end', () => { | |
resolve(buffer); | |
}); | |
}).on('error', (err) => { | |
console.error("error", err); | |
}); | |
}); | |
} | |
function getBrowserStackSessionLogs (sessionId) { | |
return new Promise((resolve, reject) => { | |
exec(`curl -u 'USERNAME:ACCESS_KEY' https://api.browserstack.com/automate/sessions/${sessionId}.json`, (error, stdout, stderr) => { | |
if (error) { | |
console.error(`exec error: ${error}`); | |
reject(error); | |
} | |
resolve(JSON.parse(stdout)); | |
}).on('error', (err) => { | |
console.error(err); | |
throw error; | |
}); | |
}); | |
} | |
// getHarLogs().then(result=> {console.log(result)}).catch(error=>{console.log("harlogs", error)}); | |
getBrowserStackSessionLogs("<SESSION_ID>").then(result => { return getHarLogs(result.automation_session.har_logs_url) }).then(result => { console.log(result) }).catch(error => { console.log(error) }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment