2013-04-03 06:27:02 +08:00
|
|
|
//
|
|
|
|
// Utility functions for the HTML notebook's CasperJS tests.
|
|
|
|
//
|
|
|
|
|
|
|
|
// Get the URL of a notebook server on which to run tests.
|
2013-09-27 08:12:40 +08:00
|
|
|
casper.get_notebook_server = function () {
|
2013-09-27 06:32:59 +08:00
|
|
|
port = casper.cli.get("port")
|
|
|
|
port = (typeof port === 'undefined') ? '8888' : port;
|
|
|
|
return 'http://127.0.0.1:' + port
|
2013-04-03 06:27:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Create and open a new notebook.
|
2013-09-27 08:12:40 +08:00
|
|
|
casper.open_new_notebook = function () {
|
|
|
|
var baseUrl = this.get_notebook_server();
|
2013-10-12 04:28:00 +08:00
|
|
|
this.start(baseUrl);
|
|
|
|
this.thenClick('button#new_notebook');
|
|
|
|
this.waitForPopup('');
|
|
|
|
|
2013-10-17 02:37:53 +08:00
|
|
|
this.withPopup('', function () {this.waitForSelector('.CodeMirror-code');});
|
2013-10-12 04:28:00 +08:00
|
|
|
this.then(function () {
|
|
|
|
// XXX: Kind of odd, the next line works for one test, but not when
|
|
|
|
// running multiple tests back-to-back, so we will just point the main
|
|
|
|
// casper browser to the same URL as the popup we just grabbed.
|
|
|
|
|
|
|
|
//this.page = this.popups[0];
|
|
|
|
this.open(this.popups[0].url);
|
|
|
|
});
|
|
|
|
|
2013-09-24 22:39:11 +08:00
|
|
|
// initially, the cells aren't created, so wait for them to appear
|
|
|
|
this.waitForSelector('.CodeMirror-code');
|
2013-10-17 02:37:53 +08:00
|
|
|
// and make sure the kernel has started
|
|
|
|
this.waitFor(function kernel_ready() {
|
|
|
|
return this.evaluate(function kernel_ready() {
|
|
|
|
return IPython.notebook.kernel.running;
|
|
|
|
});
|
|
|
|
});
|
2013-04-03 06:27:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Shut down the current notebook's kernel.
|
2013-09-27 08:12:40 +08:00
|
|
|
casper.shutdown_current_kernel = function () {
|
2013-04-03 06:27:02 +08:00
|
|
|
this.thenEvaluate(function() {
|
2013-04-04 02:28:43 +08:00
|
|
|
IPython.notebook.kernel.kill();
|
2013-04-03 06:27:02 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Delete created notebook.
|
2013-09-27 08:12:40 +08:00
|
|
|
casper.delete_current_notebook = function () {
|
2013-04-03 06:27:02 +08:00
|
|
|
this.thenEvaluate(function() {
|
|
|
|
var nbData = $('body').data();
|
|
|
|
var url = nbData.baseProjectUrl + 'notebooks/' + nbData.notebookId;
|
|
|
|
$.ajax(url, {
|
|
|
|
type: 'DELETE',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-04-04 00:39:50 +08:00
|
|
|
// Wrap a notebook test to reduce boilerplate.
|
2013-09-27 08:12:40 +08:00
|
|
|
casper.notebook_test = function(test) {
|
|
|
|
this.open_new_notebook();
|
2013-04-04 00:39:50 +08:00
|
|
|
this.then(test);
|
2013-10-15 04:09:32 +08:00
|
|
|
//XXX: we get sporadic error messages when shutting down some of the tests.
|
|
|
|
// Since the entire server will go down at the end of running the test
|
|
|
|
// suite, it's ok for now to not try to shut anything down.
|
2013-10-15 08:13:01 +08:00
|
|
|
this.shutdown_current_kernel();
|
2013-10-15 04:09:32 +08:00
|
|
|
|
2013-09-27 08:12:40 +08:00
|
|
|
//XXX: the implementation of delete_current_notebook is currently broken
|
2013-10-15 04:09:32 +08:00
|
|
|
// it's not a big deal, since the notebook directory will be deleted on
|
|
|
|
// cleanup, but we should add tests for deleting the notebook separately
|
2013-09-27 08:12:40 +08:00
|
|
|
//this.delete_current_notebook();
|
2013-04-04 00:39:50 +08:00
|
|
|
|
|
|
|
// Run the browser automation.
|
|
|
|
this.run(function() {
|
|
|
|
this.test.done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-27 06:35:23 +08:00
|
|
|
casper.options.waitTimeout=5000
|
|
|
|
casper.on('waitFor.timeout', function onWaitForTimeout(timeout) {
|
2013-09-27 08:12:40 +08:00
|
|
|
this.echo("Timeout for " + casper.get_notebook_server());
|
2013-09-27 06:35:23 +08:00
|
|
|
this.echo("Is the notebook server running?");
|
|
|
|
});
|
|
|
|
|
2013-04-03 06:27:02 +08:00
|
|
|
// Pass `console.log` calls from page JS to casper.
|
|
|
|
casper.printLog = function () {
|
|
|
|
this.on('remote.message', function(msg) {
|
|
|
|
this.echo('Remote message caught: ' + msg);
|
|
|
|
});
|
|
|
|
};
|