From a717f5c03077e1a288e8595d3cb5fc92a6b7357b Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Wed, 23 Oct 2013 23:10:26 +0100 Subject: [PATCH] Initial working saucelabs test result recording --- saucelabs.js | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/saucelabs.js b/saucelabs.js index 29a3a215d..de0c3d7ee 100644 --- a/saucelabs.js +++ b/saucelabs.js @@ -7,6 +7,7 @@ var server = connect.createServer( ).listen(port); // Tell saucelabs to run some tests +var browser = JSON.parse(process.env['SAUCE_BROWSER']); var username = process.env['SAUCE_USERNAME']; var accessKey = process.env['SAUCE_ACCESS_KEY']; @@ -17,13 +18,50 @@ request.post( { auth: { user: username, pass: accessKey }, json: { - platforms: [[ "Windows 7", "chrome", "27" ]], + platforms: [ browser ], url: "http://localhost:" + port + "/test/index.html", framework: "qunit" } }, function (error, response, body) { - console.log(response.statusCode); - console.log(body); + if (response.statusCode == 200) { + var testIdentifier = body; + waitForTestCompletion(testIdentifier); + } else { + console.error("Failed to submit test to SauceLabs, status " + response.statusCode + ", body:\n" + JSON.stringify(body)); + process.exit(3); + } } -); \ No newline at end of file +); + +function waitForTestCompletion(testIdentifier) { + request.post( + 'https://saucelabs.com/rest/v1/' + username + '/js-tests/status', + { + auth: { user: username, pass: accessKey }, + json: testIdentifier + }, + function (error, response, body) { + if (response.statusCode == 200) { + console.log(JSON.stringify(body)); + if (body["completed"] == true) { + handleTestResults(body["js tests"]); + } else { + waitForTestCompletion(testIdentifier); + } + } else { + console.error("Failed to check test status on SauceLabs, status " + response.statusCode + ", body:\n" + JSON.stringify(body)); + process.exit(4); + } + } + ); +} + +function handleTestResults(results) { + var allTestsSuccessful = results.reduce(function (passedSoFar, result) { + return passedSoFar && !!result['passed'] + }, true); + + console.log(allTestsSuccessful ? "Test passed" : "Test failed"); + process.exit(allTestsSuccessful ? 0 : 1); +} \ No newline at end of file