discourse/spec/phantom_js/smoke_test.js

245 lines
6.2 KiB
JavaScript
Raw Normal View History

/*global phantom:true */
2015-07-27 05:07:42 -04:00
console.log("Starting Discourse Smoke Test");
2013-02-20 20:07:22 -05:00
2015-07-27 05:07:42 -04:00
var system = require("system");
if (system.args.length !== 2) {
console.log("Expecting: phantomjs {smoke_test.js} {url}");
2013-02-20 20:07:22 -05:00
phantom.exit(1);
}
2015-07-27 05:07:42 -04:00
var TIMEOUT = 10000;
var page = require("webpage").create();
page.viewportSize = {
width: 1366,
height: 768
};
// In the browser, when the cookies are disabled, it also disables the localStorage
// Here, we're mocking that behavior and making sure the application doesn't blow up
page.onInitialized = function() {
page.evaluate(function() {
localStorage["disableLocalStorage"] = true;
})
}
2015-07-27 05:07:42 -04:00
// page.onConsoleMessage = function(msg) {
// console.log(msg);
// }
2013-02-20 20:07:22 -05:00
2015-07-27 05:07:42 -04:00
page.waitFor = function(desc, fn, cb) {
var start = +new Date();
2013-02-20 20:07:22 -05:00
2015-07-27 05:07:42 -04:00
var check = function() {
2013-02-20 20:07:22 -05:00
var r;
2015-07-27 05:07:42 -04:00
try { r = page.evaluate(fn); } catch (err) { }
2013-02-20 20:07:22 -05:00
2013-02-21 00:01:18 -05:00
var diff = (+new Date()) - start;
2015-07-27 05:07:42 -04:00
if (r) {
console.log("PASSED: " + desc + " - " + diff + "ms");
cb(true);
2013-02-20 20:07:22 -05:00
} else {
2015-07-27 05:07:42 -04:00
if (diff > TIMEOUT) {
console.log("FAILED: " + desc + " - " + diff + "ms");
cb(false);
2013-02-20 20:07:22 -05:00
} else {
2015-07-27 05:07:42 -04:00
setTimeout(check, 25);
2013-02-20 20:07:22 -05:00
}
}
};
check();
};
2013-02-21 00:01:18 -05:00
var actions = [];
var test = function(desc, fn) {
2015-07-27 05:07:42 -04:00
actions.push({ test: fn, desc: desc });
}
2013-02-21 00:01:18 -05:00
2015-07-27 05:07:42 -04:00
var exec = function(desc, fn) {
actions.push({ exec: fn, desc: desc });
}
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 });
}
2015-08-21 15:06:47 -04:00
// var screenshot = function(filename) {
// actions.push({ screenshot: filename });
// }
2013-02-21 00:01:18 -05:00
2015-07-27 05:07:42 -04:00
var run = function() {
2013-02-21 00:01:18 -05:00
var allPassed = true;
2015-07-27 05:07:42 -04:00
2013-02-21 00:01:18 -05:00
var done = function() {
2015-07-27 05:07:42 -04:00
console.log(allPassed ? "ALL PASSED" : "SMOKE TEST FAILED");
2013-02-21 00:01:18 -05:00
phantom.exit();
2013-02-20 20:07:22 -05:00
};
2015-07-27 05:07:42 -04:00
var performNextAction = function() {
if (!allPassed || actions.length === 0) {
2013-02-21 00:01:18 -05:00
done();
2015-07-27 05:07:42 -04:00
} else {
2013-02-21 00:01:18 -05:00
var action = actions[0];
actions = actions.splice(1);
2015-07-27 05:07:42 -04:00
if (action.test) {
page.waitFor(action.desc, action.test, function(success) {
2015-08-21 15:06:47 -04:00
allPassed = allPassed && success;
2013-02-21 00:01:18 -05:00
performNextAction();
});
2015-07-27 05:07:42 -04:00
} else if (action.exec) {
console.log("EXEC: " + action.desc);
page.evaluate(action.exec);
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);
2013-02-21 00:01:18 -05:00
performNextAction();
}
}
};
performNextAction();
};
2013-02-20 20:07:22 -05:00
2015-07-27 05:07:42 -04:00
var runTests = function() {
test("expect a log in button", function() {
return $(".login-button").text().trim() === "Log In";
});
2013-02-20 20:07:22 -05:00
test("at least one topic shows up", function() {
2015-07-27 05:07:42 -04:00
return document.querySelector(".topic-list tbody tr");
2013-02-21 00:01:18 -05:00
});
2015-07-27 05:07:42 -04:00
execAsync("navigate to 1st topic", 500, function() {
if ($(".main-link > a:first").length > 0) {
$(".main-link > a:first").click(); // topic list page
} else {
$(".featured-topic a.title:first").click(); // categories page
}
2013-02-21 00:01:18 -05:00
});
2015-07-27 05:07:42 -04:00
test("at least one post body", function() {
return document.querySelector(".topic-post");
2013-02-21 00:01:18 -05:00
});
2015-07-27 05:07:42 -04:00
execAsync("click on the 1st user", 500, function() {
// remove the popup action for testing
$(".topic-meta-data a:first").data("ember-action", "");
$(".topic-meta-data a:first").focus().click();
2013-02-21 00:01:18 -05:00
});
2015-07-27 05:07:42 -04:00
test("user has details", function() {
return document.querySelector("#user-card .names");
});
2015-07-27 05:07:42 -04:00
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;
});
2015-07-27 05:07:42 -04:00
execAsync("submit the reply", 6000, function() {
$("#reply-control .create").click();
2013-02-21 00:01:18 -05:00
});
2015-07-27 05:07:42 -04:00
test("reply is created", function() {
return !document.querySelector(".saving-text")
&& $(".topic-post").length === 2;
2013-02-21 00:01:18 -05:00
});
run();
};
2015-08-21 15:06:47 -04:00
page.open(system.args[1], function() {
2015-07-27 05:07:42 -04:00
console.log("OPENED: " + system.args[1]);
runTests();
2013-02-20 20:07:22 -05:00
});