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.
|
|
|
|
casper.getNotebookServer = 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.
|
|
|
|
casper.openNewNotebook = function () {
|
|
|
|
var baseUrl = this.getNotebookServer();
|
|
|
|
this.start(baseUrl + '/new');
|
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-04-03 06:27:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Shut down the current notebook's kernel.
|
2013-04-04 00:39:50 +08:00
|
|
|
casper.shutdownCurrentKernel = 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.
|
|
|
|
casper.deleteCurrentNotebook = function () {
|
|
|
|
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.
|
|
|
|
casper.notebookTest = function(test) {
|
|
|
|
this.openNewNotebook();
|
|
|
|
this.then(test);
|
|
|
|
this.shutdownCurrentKernel();
|
2013-09-27 05:30:28 +08:00
|
|
|
//XXX: the implementation of deleteCurrentNotebook is currently broken
|
|
|
|
// 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
|
|
|
|
//this.deleteCurrentNotebook();
|
2013-04-04 00:39:50 +08:00
|
|
|
|
|
|
|
// Run the browser automation.
|
|
|
|
this.run(function() {
|
|
|
|
this.test.done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|