Skip to content

Instantly share code, notes, and snippets.

@bernii
Last active December 13, 2015 16:58
Show Gist options
  • Save bernii/4944153 to your computer and use it in GitHub Desktop.
Save bernii/4944153 to your computer and use it in GitHub Desktop.
Example code using WD.js and Jasmine with Sauce Labs
// You need to have the following installed:
// https://github.com/admc/wd
// https://github.com/kriskowal/q
var wd = require('wd')
, Q = require('q')
, request = require('request')
, assert = require('assert')
, host = "ondemand.saucelabs.com"
, port = 80
, username = YOUR_USERNAME_GOES_HERE
, accessKey = YOUR_ACCESS_KEY_GOES_HERE
// using promisified version of webdriver
, browser = wd.promiseRemote(host, port, username, accessKey);
browser.on('status', function(info){
console.log('\x1b[36m%s\x1b[0m', info);
});
browser.on('command', function(meth, path, data){
console.log(' > \x1b[33m%s\x1b[0m: %s', meth, path, data || '');
});
// general rest call helper function using promises
var api = function (url, method, data) {
var deferred = Q.defer();
request(
{ method: method
, uri: ["https://", username, ":", accessKey, "@saucelabs.com/rest", url].join("")
, headers: {'Content-Type': 'application/json'}
, body: JSON.stringify(data)
}
, function (error, response, body) {
deferred.resolve(response.body);
}
);
return deferred.promise;
};
function waitUntilResultsAreAvailable(js, timeout, start){
if (start === undefined) { start = new Date(); }
if (new Date() - start > timeout) { throw new Error("Timeout: Element not there"); }
return browser.eval(js)
.then(function (jsValue) {
if (jsValue !== null) { return jsValue; }
else { return waitUntilResultsAreAvailable(js, timeout, start); }
});
}
// test case
browser.init({
browserName: 'chrome',
tags: ["examples"],
name: "Jasmine sample test",
// provide build name if you want to see frontend tests
// summary on your jobs page
build: "frontend-tests-example"
}).then(function () {
return browser.get("http://saucelabs.com/test_helpers/front_tests/index.html");
}).then(function () {
// get jasmine output as JSON
return waitUntilResultsAreAvailable("jasmine.getJSReport && jasmine.getJSReport()", 3000);
}).then(function (jsreport) {
// make an API call to Sauce - set custom-data with 'jasmine' data
var data = {
'custom-data': {jasmine: jsreport}
};
return api(["/v1/", username, "/jobs/", browser.sessionID].join(""), "PUT", data);
}).then(function (body) {
console.log("CONGRATS - WE'RE DONE\n",
"Check out test results at http://saucelabs.com/jobs/" + browser.sessionID + "\n",
body);
}).fin(function () {
return browser.quit();
}).done();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment