util.js
127 lines
| 4.0 KiB
| application/javascript
|
JavascriptLexer
David Wyde
|
r13249 | // | ||
// Utility functions for the HTML notebook's CasperJS tests. | ||||
// | ||||
// Get the URL of a notebook server on which to run tests. | ||||
Paul Ivanov
|
r13275 | casper.get_notebook_server = function () { | ||
Paul Ivanov
|
r13271 | port = casper.cli.get("port") | ||
port = (typeof port === 'undefined') ? '8888' : port; | ||||
return 'http://127.0.0.1:' + port | ||||
David Wyde
|
r13249 | }; | ||
// Create and open a new notebook. | ||||
Paul Ivanov
|
r13275 | casper.open_new_notebook = function () { | ||
var baseUrl = this.get_notebook_server(); | ||||
Paul Ivanov
|
r13284 | this.start(baseUrl); | ||
this.thenClick('button#new_notebook'); | ||||
this.waitForPopup(''); | ||||
Paul Ivanov
|
r13290 | this.withPopup('', function () {this.waitForSelector('.CodeMirror-code');}); | ||
Paul Ivanov
|
r13284 | 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); | ||||
}); | ||||
Paul Ivanov
|
r13258 | // initially, the cells aren't created, so wait for them to appear | ||
this.waitForSelector('.CodeMirror-code'); | ||||
Paul Ivanov
|
r13290 | // and make sure the kernel has started | ||
Paul Ivanov
|
r13302 | this.waitFor( this.kernel_running ); | ||
}; | ||||
// return whether or not the kernel is running | ||||
casper.kernel_running = function kernel_running() { | ||||
return this.evaluate(function kernel_running() { | ||||
return IPython.notebook.kernel.running; | ||||
Paul Ivanov
|
r13290 | }); | ||
David Wyde
|
r13249 | }; | ||
// Shut down the current notebook's kernel. | ||||
Paul Ivanov
|
r13275 | casper.shutdown_current_kernel = function () { | ||
David Wyde
|
r13249 | this.thenEvaluate(function() { | ||
David Wyde
|
r13255 | IPython.notebook.kernel.kill(); | ||
David Wyde
|
r13249 | }); | ||
}; | ||||
// Delete created notebook. | ||||
Paul Ivanov
|
r13275 | casper.delete_current_notebook = function () { | ||
David Wyde
|
r13249 | this.thenEvaluate(function() { | ||
var nbData = $('body').data(); | ||||
var url = nbData.baseProjectUrl + 'notebooks/' + nbData.notebookId; | ||||
$.ajax(url, { | ||||
type: 'DELETE', | ||||
}); | ||||
}); | ||||
}; | ||||
Paul Ivanov
|
r13294 | // wait for output in a given cell | ||
casper.wait_for_output = function (cell_num) { | ||||
this.then(function() { | ||||
this.waitFor(function (c) { | ||||
return this.evaluate(function get_output(c) { | ||||
var cell = IPython.notebook.get_cell(c); | ||||
return cell.output_area.outputs.length != 0; | ||||
}, | ||||
// pass parameter from the test suite js to the browser code js | ||||
{c : cell_num}); | ||||
Paul Ivanov
|
r13463 | }, | ||
function then() { }, | ||||
function timeout() { | ||||
this.echo("wait_for_output timedout!"); | ||||
Paul Ivanov
|
r13294 | }); | ||
}); | ||||
}; | ||||
Paul Ivanov
|
r13299 | // return the output of a given cell | ||
casper.get_output_cell = function (cell_num) { | ||||
var result = casper.evaluate(function (c) { | ||||
var cell = IPython.notebook.get_cell(c); | ||||
return cell.output_area.outputs[0]; | ||||
}, | ||||
{c : cell_num}); | ||||
return result; | ||||
}; | ||||
Paul Ivanov
|
r13300 | // return the number of cells in the notebook | ||
casper.get_cells_length = function () { | ||||
var result = casper.evaluate(function () { | ||||
return IPython.notebook.get_cells().length; | ||||
}) | ||||
return result; | ||||
}; | ||||
David Wyde
|
r13253 | // Wrap a notebook test to reduce boilerplate. | ||
Paul Ivanov
|
r13275 | casper.notebook_test = function(test) { | ||
this.open_new_notebook(); | ||||
David Wyde
|
r13253 | this.then(test); | ||
Paul Ivanov
|
r13285 | //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. | ||||
Paul Ivanov
|
r13288 | this.shutdown_current_kernel(); | ||
Paul Ivanov
|
r13285 | |||
Paul Ivanov
|
r13275 | //XXX: the implementation of delete_current_notebook is currently broken | ||
Paul Ivanov
|
r13285 | // 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 | ||||
Paul Ivanov
|
r13275 | //this.delete_current_notebook(); | ||
David Wyde
|
r13253 | |||
// Run the browser automation. | ||||
this.run(function() { | ||||
this.test.done(); | ||||
}); | ||||
}; | ||||
Paul Ivanov
|
r13309 | casper.options.waitTimeout=10000 | ||
Paul Ivanov
|
r13272 | casper.on('waitFor.timeout', function onWaitForTimeout(timeout) { | ||
Paul Ivanov
|
r13275 | this.echo("Timeout for " + casper.get_notebook_server()); | ||
Paul Ivanov
|
r13272 | this.echo("Is the notebook server running?"); | ||
}); | ||||
David Wyde
|
r13249 | // Pass `console.log` calls from page JS to casper. | ||
casper.printLog = function () { | ||||
this.on('remote.message', function(msg) { | ||||
this.echo('Remote message caught: ' + msg); | ||||
}); | ||||
}; | ||||