Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Last active September 18, 2019 20:14
Show Gist options
  • Save sauceaaron/f0c25372e75da2c35ea9db15487584a5 to your computer and use it in GitHub Desktop.
Save sauceaaron/f0c25372e75da2c35ea9db15487584a5 to your computer and use it in GitHub Desktop.
var usage = 'USAGE: node update_sauce_test.js <session_id> passed|failed [--rdc]'
var https = require('https')
args = process.argv.slice(2)
# check if this is a real device test
var rdc = false
if (args.includes('--rdc'))
{
rdc = true
args.splice(args.indexOf('--rdc'), 1)
}
# get session id and test result
if (args.length != 2) { console.log(usage) }
var session_id = args[0]
var test_result = args[1]
# get credentials
var user = process.env.SAUCE_USERNAME
var key = process.env.SAUCE_ACCESS_KEY
# get endpoint
var url = `https://saucelabs.com/rest/v1/${user}/jobs/${session_id}`
var host = 'saucelabs.com'
var path = `/rest/v1/${user}/jobs/${session_id}`
# set credentials and endpoint for rdc
if (rdc)
{
user = process.env.TESTOBJECT_USERNAME
key = process.env.TESTOBJECT_API_KEY
url = `https://app.testobject.com/api/rest/v2/appium/session/${session_id}/test`
host = 'app.testobject.com'
path = `/api/rest/v2/appium/session/${session_id}/test`
}
# encode credentials for Basic Authentication
var auth = Buffer.from(user + ':' + key).toString('base64')
# set status (anything other than "passed" is a failure)
var status = test_result.toLowerCase() == 'passed' ? true : false
var body = JSON.stringify({ passed: status })
# set all options
var options = {
'method': 'PUT',
'host': host,
'path': path,
'headers': {
'Authorization' : 'Basic ' + auth,
'Content-Type' : 'application/json',
},
'body' : body
}
# build request
var request = https.request(options, function(response) {
var body = ''
response.on('error', function(error) { console.err(error.getMessage()) })
response.on('data', function(data) { body += data })
response.on('end', function() { console.log(body) })
})
# add error handler and execute request
request.on('error', function(e) { console.err('problem with request: ' + e.message) })
request.write(body)
request.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment