forked from mozilla/openbadges-backpack
/
acceptance-test-utils.js
62 lines (52 loc) · 1.69 KB
/
acceptance-test-utils.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
57
58
59
60
61
62
var soda = require('soda'),
config = require('./local-config').config,
app = require('../app.js');
soda.prototype.gracefulExit = function() {
return this.end(function(err) {
this.testComplete(function() {
if (err)
throw err;
console.log("Test successful.");
// TODO: Why doesn't app.close() exit the process?
process.exit(0);
});
});
};
soda.prototype.waitForVisibleContent = function(args) {
var hasContent = exports.scriptify(function(window, args) {
function collapseWhitespace(text) {
return text.replace(/\n/g, ' ').replace(/\s+/g, ' ');
}
var element = window.document.querySelector(args.selector);
var html = element && collapseWhitespace(element.innerHTML);
if (!html)
return false;
return html.indexOf(collapseWhitespace(args.content)) != -1;
}, args);
return this.waitForVisible("css=" + args.selector)
.waitForCondition(hasContent, 4000);
};
exports.scriptify = function(func, args) {
var code = '(' + func.toString() + ')' +
'(selenium.browserbot.getCurrentWindow(), ' +
JSON.stringify(args) + ');';
return code;
};
exports.createClient = function(options) {
options = options || {};
app.listen(8888);
if (options.extensions)
for (var name in options.extensions) {
soda.prototype[name] = options.extensions[name];
}
var browser = soda.createClient({
host: config.host
, port: config.port || 4444
, url: config.url || 'http://localhost:8888/'
, browser: config.browser || 'firefox'
});
browser.on('command', function(cmd, args){
console.log(' \x1b[33m%s\x1b[0m: %s', cmd, args.join(', '));
});
return browser;
};