util.js
243 lines
| 7.4 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 () { | ||
this.open(this.popups[0].url); | ||||
}); | ||||
Brian E. Granger
|
r14965 | // Make sure the kernel has started | ||
Paul Ivanov
|
r13302 | this.waitFor( this.kernel_running ); | ||
MinRK
|
r14933 | // track the IPython busy/idle state | ||
this.thenEvaluate(function () { | ||||
$([IPython.events]).on('status_idle.Kernel',function () { | ||||
IPython._status = 'idle'; | ||||
}); | ||||
$([IPython.events]).on('status_busy.Kernel',function () { | ||||
IPython._status = 'busy'; | ||||
}); | ||||
}); | ||||
Paul Ivanov
|
r13302 | }; | ||
Brian E. Granger
|
r14965 | // Return whether or not the kernel is running. | ||
Paul Ivanov
|
r13302 | 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 | }); | ||
Brian E. Granger
|
r14965 | // We close the page right after this so we need to give it time to complete. | ||
this.wait(1000); | ||||
David Wyde
|
r13249 | }; | ||
// Delete created notebook. | ||||
Paul Ivanov
|
r13275 | casper.delete_current_notebook = function () { | ||
Brian E. Granger
|
r14965 | // For some unknown reason, this doesn't work?!? | ||
David Wyde
|
r13249 | this.thenEvaluate(function() { | ||
Brian E. Granger
|
r14965 | IPython.notebook.delete(); | ||
David Wyde
|
r13249 | }); | ||
}; | ||||
Matthias BUSSONNIER
|
r15042 | casper.wait_for_busy = function () { | ||
this.waitFor(function () { | ||||
return this.evaluate(function () { | ||||
return IPython._status == 'busy'; | ||||
}); | ||||
}); | ||||
}; | ||||
MinRK
|
r14933 | casper.wait_for_idle = function () { | ||
MinRK
|
r14934 | this.waitFor(function () { | ||
return this.evaluate(function () { | ||||
return IPython._status == 'idle'; | ||||
MinRK
|
r14933 | }); | ||
}); | ||||
}; | ||||
MinRK
|
r14904 | // wait for the nth output in a given cell | ||
casper.wait_for_output = function (cell_num, out_num) { | ||||
MinRK
|
r14933 | this.wait_for_idle(); | ||
MinRK
|
r14904 | out_num = out_num || 0; | ||
this.then(function() { | ||||
this.waitFor(function (c, o) { | ||||
return this.evaluate(function get_output(c, o) { | ||||
var cell = IPython.notebook.get_cell(c); | ||||
return cell.output_area.outputs.length > o; | ||||
}, | ||||
// pass parameter from the test suite js to the browser code js | ||||
{c : cell_num, o : out_num}); | ||||
}); | ||||
Jonathan Frederic
|
r14435 | }, | ||
function then() { }, | ||||
function timeout() { | ||||
this.echo("wait_for_output timed out!"); | ||||
Paul Ivanov
|
r13294 | }); | ||
}; | ||||
MinRK
|
r14864 | // return an output of a given cell | ||
Paul Ivanov
|
r14147 | casper.get_output_cell = function (cell_num, out_num) { | ||
out_num = out_num || 0; | ||||
var result = casper.evaluate(function (c, o) { | ||||
Paul Ivanov
|
r13299 | var cell = IPython.notebook.get_cell(c); | ||
Paul Ivanov
|
r14147 | return cell.output_area.outputs[o]; | ||
Paul Ivanov
|
r13299 | }, | ||
Paul Ivanov
|
r14147 | {c : cell_num, o : out_num}); | ||
MinRK
|
r14864 | if (!result) { | ||
var num_outputs = casper.evaluate(function (c) { | ||||
var cell = IPython.notebook.get_cell(c); | ||||
return cell.output_area.outputs.length; | ||||
}, | ||||
{c : cell_num}); | ||||
this.test.assertTrue(false, | ||||
"Cell " + cell_num + " has no output #" + out_num + " (" + num_outputs + " total)" | ||||
); | ||||
} else { | ||||
return result; | ||||
} | ||||
Paul Ivanov
|
r13299 | }; | ||
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; | ||||
}; | ||||
Jonathan Frederic
|
r13765 | // Set the text content of a cell. | ||
casper.set_cell_text = function(index, text){ | ||||
this.evaluate(function (index, text) { | ||||
var cell = IPython.notebook.get_cell(index); | ||||
cell.set_text(text); | ||||
MinRK
|
r14933 | }, index, text); | ||
Jonathan Frederic
|
r13765 | }; | ||
Jonathan Frederic
|
r13766 | // Inserts a cell at the bottom of the notebook | ||
// Returns the new cell's index. | ||||
casper.insert_cell_at_bottom = function(cell_type){ | ||||
Brian E. Granger
|
r14965 | cell_type = cell_type || 'code'; | ||
Jonathan Frederic
|
r13766 | |||
return this.evaluate(function (cell_type) { | ||||
var cell = IPython.notebook.insert_cell_at_bottom(cell_type); | ||||
return IPython.notebook.find_cell_index(cell); | ||||
MinRK
|
r14933 | }, cell_type); | ||
Jonathan Frederic
|
r13766 | }; | ||
Jonathan Frederic
|
r13765 | // Insert a cell at the bottom of the notebook and set the cells text. | ||
// Returns the new cell's index. | ||||
casper.append_cell = function(text, cell_type) { | ||||
Jonathan Frederic
|
r13766 | var index = this.insert_cell_at_bottom(cell_type); | ||
Jonathan Frederic
|
r13765 | if (text !== undefined) { | ||
Jonathan Frederic
|
r13766 | this.set_cell_text(index, text); | ||
Jonathan Frederic
|
r13765 | } | ||
return index; | ||||
}; | ||||
// Asynchronously executes a cell by index. | ||||
// Returns the cell's index. | ||||
casper.execute_cell = function(index){ | ||||
var that = this; | ||||
this.then(function(){ | ||||
that.evaluate(function (index) { | ||||
var cell = IPython.notebook.get_cell(index); | ||||
cell.execute(); | ||||
MinRK
|
r14933 | }, index); | ||
Jonathan Frederic
|
r13765 | }); | ||
return index; | ||||
}; | ||||
// Synchronously executes a cell by index. | ||||
// Optionally accepts a then_callback parameter. then_callback will get called | ||||
// when the cell has finished executing. | ||||
// Returns the cell's index. | ||||
casper.execute_cell_then = function(index, then_callback) { | ||||
Jonathan Frederic
|
r13766 | var return_val = this.execute_cell(index); | ||
Jonathan Frederic
|
r13765 | |||
MinRK
|
r14933 | this.wait_for_idle(); | ||
Jonathan Frederic
|
r13765 | |||
var that = this; | ||||
this.then(function(){ | ||||
if (then_callback!==undefined) { | ||||
then_callback.apply(that, [index]); | ||||
} | ||||
}); | ||||
return return_val; | ||||
}; | ||||
// Utility function that allows us to easily check if an element exists | ||||
// within a cell. Uses JQuery selector to look for the element. | ||||
casper.cell_element_exists = function(index, selector){ | ||||
return casper.evaluate(function (index, selector) { | ||||
var $cell = IPython.notebook.get_cell(index).element; | ||||
return $cell.find(selector).length > 0; | ||||
}, index, selector); | ||||
}; | ||||
// Utility function that allows us to execute a jQuery function on an | ||||
// element within a cell. | ||||
casper.cell_element_function = function(index, selector, function_name, function_args){ | ||||
return casper.evaluate(function (index, selector, function_name, function_args) { | ||||
var $cell = IPython.notebook.get_cell(index).element; | ||||
var $el = $cell.find(selector); | ||||
return $el[function_name].apply($el, function_args); | ||||
}, index, selector, function_name, function_args); | ||||
}; | ||||
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); | ||
Brian E. Granger
|
r14965 | |||
// Kill the kernel and delete the notebook. | ||||
Paul Ivanov
|
r13288 | this.shutdown_current_kernel(); | ||
Brian E. Granger
|
r14965 | // This is still broken but shouldn't be a problem for now. | ||
// this.delete_current_notebook(); | ||||
Paul Ivanov
|
r13285 | |||
Brian E. Granger
|
r14965 | // This is required to clean up the page we just finished with. If we don't call this | ||
// casperjs will leak file descriptors of all the open WebSockets in that page. We | ||||
// have to set this.page=null so that next time casper.start runs, it will create a | ||||
// new page from scratch. | ||||
this.then(function () { | ||||
this.page.close(); | ||||
this.page = null; | ||||
}); | ||||
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); | ||||
}); | ||||
}; | ||||