discourse/spec/phantom_js/smoke_test.js

131 lines
2.6 KiB
JavaScript
Raw Normal View History

/*global phantom:true */
2013-02-21 09:07:22 +08:00
console.log('Starting Smoke Test');
var system = require('system');
if(system.args.length !== 2) {
console.log("expecting phantomjs {smoke_test.js} {base_url}");
phantom.exit(1);
}
var page = require('webpage').create();
2013-02-21 13:01:18 +08:00
page.waitFor = function(desc, fn, timeout, after) {
2013-02-21 09:07:22 +08:00
var check,start,promise;
start = +new Date();
promise = {};
check = function() {
var r;
try {
r = page.evaluate(fn);
}
2013-02-21 09:07:22 +08:00
catch(err) {
// next time
}
2013-02-21 13:01:18 +08:00
var diff = (+new Date()) - start;
2013-02-21 09:07:22 +08:00
if(r) {
2013-02-21 13:01:18 +08:00
console.log("PASSED: " + desc + " " + diff + "ms" );
after(true);
2013-02-21 09:07:22 +08:00
} else {
2013-02-21 13:01:18 +08:00
if(diff > timeout) {
console.log("FAILED: " + desc + " " + diff + "ms");
after(false);
2013-02-21 09:07:22 +08:00
} else {
setTimeout(check, 50);
}
}
};
check();
};
2013-02-21 13:01:18 +08:00
var actions = [];
var test = function(desc, fn) {
actions.push({test: fn, desc: desc});
};
var navigate = function(desc, fn) {
actions.push({navigate: fn, desc: desc});
};
var run = function(){
var allPassed = true;
var done = function() {
if(allPassed) {
console.log("ALL PASSED");
2013-02-21 09:07:22 +08:00
} else {
2013-02-21 13:01:18 +08:00
console.log("SMOKE TEST FAILED");
2013-02-21 09:07:22 +08:00
}
2013-02-21 13:01:18 +08:00
phantom.exit();
2013-02-21 09:07:22 +08:00
};
2013-02-21 13:01:18 +08:00
var performNextAction = function(){
if(actions.length === 0) {
done();
}
else{
var action = actions[0];
actions = actions.splice(1);
if(action.test) {
page.waitFor(action.desc, action.test, 10000, function(success){
allPassed = allPassed && success;
performNextAction();
});
}
2013-02-21 13:01:18 +08:00
else if(action.navigate) {
console.log("NAVIGATE: " + action.desc);
page.evaluate(action.navigate);
performNextAction();
}
}
};
performNextAction();
};
2013-02-21 09:07:22 +08:00
2013-02-21 13:01:18 +08:00
page.runTests = function(){
2013-02-21 09:07:22 +08:00
test("at least one topic shows up", function() {
return $('#topic-list tbody tr').length > 0;
2013-02-21 13:01:18 +08:00
});
test("expect a log in button", function(){
return $('.current-username .btn').text() === 'Log In';
2013-02-21 13:01:18 +08:00
});
navigate("navigate to first topic", function(){
2013-02-21 13:37:17 +08:00
Em.run.later(function(){
$('.main-link a:first').click();
2013-02-21 13:37:17 +08:00
}, 500);
2013-02-21 13:01:18 +08:00
});
test("at least one post body", function(){
return $('.topic-post').length > 0;
2013-02-21 13:01:18 +08:00
});
2013-02-21 13:01:18 +08:00
navigate("navigate to first user", function(){
2013-02-21 13:37:17 +08:00
// for whatever reason the clicks do not respond at the begining
// defering
Em.run.later(function(){
$('.topic-meta-data a:first').focus().click();
2013-02-21 13:37:17 +08:00
},500);
2013-02-21 13:01:18 +08:00
});
test("has about me section",function(){
return $('.about-me').length === 1;
2013-02-21 13:01:18 +08:00
});
run();
};
page.open(system.args[1], function (status) {
console.log("Opened " + system.args[1]);
page.runTests();
2013-02-21 09:07:22 +08:00
});