FIX: improve smoke tests

This commit is contained in:
Régis Hanol 2015-07-27 11:07:42 +02:00
parent 1eb8f710f1
commit 2473a00b26
4 changed files with 199 additions and 76 deletions

View file

@ -22,4 +22,30 @@ User.seed do |u|
u.trust_level = TrustLevel[4] u.trust_level = TrustLevel[4]
end end
Group.user_trust_level_change!(-1 ,TrustLevel[4]) Group.user_trust_level_change!(-1, TrustLevel[4])
# User for the smoke tests
if ENV["SMOKE"] == "1"
smoke_user = User.seed do |u|
u.id = 0
u.name = "smoke_user"
u.username = "smoke_user"
u.username_lower = "smoke_user"
u.email = "smoke_user@discourse.org"
u.password = "P4ssw0rd"
u.email_direct = false
u.email_digests = false
u.email_private_messages = false
u.active = true
u.approved = true
u.approved_at = Time.now
u.trust_level = TrustLevel[3]
end.first
EmailToken.seed do |et|
et.id = 1
et.user_id = smoke_user.id
et.email = smoke_user.email
et.confirmed = true
end
end

View file

@ -1,6 +1,5 @@
desc "run phantomjs based smoke tests on current build" desc "run phantomjs based smoke tests on current build"
task "smoke:test" do task "smoke:test" do
phantom_path = File.expand_path('~/phantomjs/bin/phantomjs') phantom_path = File.expand_path('~/phantomjs/bin/phantomjs')
phantom_path = nil unless File.exists?(phantom_path) phantom_path = nil unless File.exists?(phantom_path)
phantom_path = phantom_path || 'phantomjs' phantom_path = phantom_path || 'phantomjs'

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 KiB

View file

@ -1,41 +1,45 @@
/*global phantom:true */ /*global phantom:true */
console.log('Starting Smoke Test'); console.log("Starting Discourse Smoke Test");
var system = require('system');
if(system.args.length !== 2) { var system = require("system");
console.log("expecting phantomjs {smoke_test.js} {base_url}");
if (system.args.length !== 2) {
console.log("Expecting: phantomjs {smoke_test.js} {url}");
phantom.exit(1); phantom.exit(1);
} }
var page = require('webpage').create(); var TIMEOUT = 10000;
var page = require("webpage").create();
page.waitFor = function(desc, fn, timeout, after) { page.viewportSize = {
var check,start,promise; width: 1366,
height: 768
};
start = +new Date(); // page.onConsoleMessage = function(msg) {
promise = {}; // console.log(msg);
check = function() { // }
page.waitFor = function(desc, fn, cb) {
var start = +new Date();
var check = function() {
var r; var r;
try { try { r = page.evaluate(fn); } catch (err) { }
r = page.evaluate(fn);
}
catch(err) {
// next time
}
var diff = (+new Date()) - start; var diff = (+new Date()) - start;
if(r) { if (r) {
console.log("PASSED: " + desc + " " + diff + "ms" ); console.log("PASSED: " + desc + " - " + diff + "ms");
after(true); cb(true);
} else { } else {
if(diff > timeout) { if (diff > TIMEOUT) {
console.log("FAILED: " + desc + " " + diff + "ms"); console.log("FAILED: " + desc + " - " + diff + "ms");
after(false); cb(false);
} else { } else {
setTimeout(check, 50); setTimeout(check, 25);
} }
} }
}; };
@ -47,40 +51,61 @@ page.waitFor = function(desc, fn, timeout, after) {
var actions = []; var actions = [];
var test = function(desc, fn) { var test = function(desc, fn) {
actions.push({test: fn, desc: desc}); actions.push({ test: fn, desc: desc });
}; }
var navigate = function(desc, fn) { var exec = function(desc, fn) {
actions.push({navigate: fn, desc: desc}); actions.push({ exec: fn, desc: desc });
}; }
var run = function(){ var execAsync = function(desc, delay, fn) {
actions.push({ execAsync: fn, delay: delay, desc: desc });
}
var upload = function(input, path) {
actions.push({ upload: path, input: input });
}
var screenshot = function(filename) {
actions.push({ screenshot: filename });
}
var run = function() {
var allPassed = true; var allPassed = true;
var done = function() { var done = function() {
if(allPassed) { console.log(allPassed ? "ALL PASSED" : "SMOKE TEST FAILED");
console.log("ALL PASSED");
} else {
console.log("SMOKE TEST FAILED");
}
phantom.exit(); phantom.exit();
}; };
var performNextAction = function(){ var performNextAction = function() {
if(actions.length === 0) { if (!allPassed || actions.length === 0) {
done(); done();
} } else {
else{
var action = actions[0]; var action = actions[0];
actions = actions.splice(1); actions = actions.splice(1);
if(action.test) { if (action.test) {
page.waitFor(action.desc, action.test, 10000, function(success){ page.waitFor(action.desc, action.test, function(success) {
allPassed = allPassed && success; allPassed &= success;
performNextAction(); performNextAction();
}); });
} } else if (action.exec) {
else if(action.navigate) { console.log("EXEC: " + action.desc);
console.log("NAVIGATE: " + action.desc); page.evaluate(action.exec);
page.evaluate(action.navigate); performNextAction();
} else if (action.execAsync) {
console.log("EXEC ASYNC: " + action.desc + " - " + action.delay + "ms");
setTimeout(function() {
page.evaluate(action.execAsync);
performNextAction();
}, action.delay);
} else if (action.upload) {
console.log("UPLOAD: " + action.upload);
page.uploadFile(action.input, action.upload);
performNextAction();
} else if (action.screenshot) {
console.log("SCREENSHOT: " + action.screenshot);
page.render(action.screenshot);
performNextAction(); performNextAction();
} }
} }
@ -89,50 +114,123 @@ var run = function(){
performNextAction(); performNextAction();
}; };
page.runTests = function(){ var runTests = function() {
test("expect a log in button", function() {
return $(".login-button").text().trim() === "Log In";
});
test("at least one topic shows up", function() { test("at least one topic shows up", function() {
return $('.topic-list tbody tr').length > 0; return document.querySelector(".topic-list tbody tr");
}); });
test("expect a log in button", function(){ execAsync("navigate to 1st topic", 500, function() {
return $('.login-button').text().trim() === 'Log In'; if ($(".main-link > a:first").length > 0) {
}); $(".main-link > a:first").click(); // topic list page
navigate("navigate to first topic", function(){
Em.run.later(function(){
if ($('.main-link > a:first').length > 0) {
$('.main-link > a:first').click(); // topic list page
} else { } else {
$('.featured-topic a.title:first').click(); // categories page $(".featured-topic a.title:first").click(); // categories page
} }
}, 500);
}); });
test("at least one post body", function(){ test("at least one post body", function() {
return $('.topic-post').length > 0; return document.querySelector(".topic-post");
}); });
navigate("navigate to first user", function(){ execAsync("click on the 1st user", 500, function() {
// for whatever reason the clicks do not respond at the begining // remove the popup action for testing
// defering $(".topic-meta-data a:first").data("ember-action", "");
Em.run.later(function(){ $(".topic-meta-data a:first").focus().click();
// Remove the popup action for testing
$('.topic-meta-data a:first').data('ember-action', '');
$('.topic-meta-data a:first').focus().click();
},500);
}); });
test("has details",function(){ test("user has details", function() {
return $('#user-card .names').length === 1; return document.querySelector("#user-card .names");
});
exec("open login modal", function() {
$(".login-button").click();
});
test("login modal is open", function() {
return document.querySelector(".login-modal");
});
exec("type in credentials & log in", function() {
$("#login-account-name").val("smoke_user").trigger("change");
$("#login-account-password").val("P4ssw0rd").trigger("change");
$(".login-modal .btn-primary").click();
});
test("is logged in", function() {
return document.querySelector(".current-user");
});
exec("compose new topic", function() {
var date = " (" + (+new Date()) + ")",
title = "This is a new topic" + date,
post = "I can write a new topic inside the smoke test!" + date + "\n\n";
$("#create-topic").click();
$("#reply-title").val(title).trigger("change");
$("#reply-control .wmd-input").val(post).trigger("change");
$("#reply-control .wmd-input").focus()[0].setSelectionRange(post.length, post.length);
});
exec("open upload modal", function() {
$(".wmd-image-button").click();
});
test("upload modal is open", function() {
return document.querySelector("#filename-input");
});
upload("#filename-input", "spec/fixtures/images/large & unoptimized.png");
exec("click upload button", function() {
$(".modal .btn-primary").click();
});
test("image is uploaded", function() {
return document.querySelector(".cooked img");
});
exec("submit the topic", function() {
$("#reply-control .create").click();
});
test("topic is created", function() {
return document.querySelector(".fancy-title");
});
exec("click reply button", function() {
$(".post-controls:first .create").click();
});
test("composer is open", function() {
return document.querySelector("#reply-control .wmd-input");
});
exec("compose reply", function() {
var post = "I can even write a reply inside the smoke test ;) (" + (+new Date()) + ")";
$("#reply-control .wmd-input").val(post).trigger("change");
});
test("waiting for the preview", function() {
return $(".wmd-preview").text().trim().indexOf("I can even write") === 0;
});
execAsync("submit the reply", 6000, function() {
$("#reply-control .create").click();
});
test("reply is created", function() {
return !document.querySelector(".saving-text")
&& $(".topic-post").length === 2;
}); });
run(); run();
}; };
page.open(system.args[1], function (status) { page.open(system.args[1], function(status) {
console.log("Opened " + system.args[1]); console.log("OPENED: " + system.args[1]);
page.runTests(); runTests();
}); });