From f5000f416e508d444f60f145511efa4dba884e00 Mon Sep 17 00:00:00 2001 From: David Wyde Date: Tue, 2 Apr 2013 17:27:02 -0500 Subject: [PATCH] Add CasperJS utility functions, and tests for code and Markdown cell execution. Code cell test is based on https://gist.github.com/ellisonbg/3329929. --- .../casperjs/test_cases/execute_code_cell.js | 29 ++++++++++++ .../casperjs/test_cases/render_markdown.js | 30 +++++++++++++ .../html/notebook/tests/casperjs/util.js | 45 +++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 IPython/frontend/html/notebook/tests/casperjs/test_cases/execute_code_cell.js create mode 100644 IPython/frontend/html/notebook/tests/casperjs/test_cases/render_markdown.js create mode 100644 IPython/frontend/html/notebook/tests/casperjs/util.js diff --git a/IPython/frontend/html/notebook/tests/casperjs/test_cases/execute_code_cell.js b/IPython/frontend/html/notebook/tests/casperjs/test_cases/execute_code_cell.js new file mode 100644 index 000000000..17ac61c63 --- /dev/null +++ b/IPython/frontend/html/notebook/tests/casperjs/test_cases/execute_code_cell.js @@ -0,0 +1,29 @@ +// +// Test code cell execution. +// +casper.openNewNotebook(); + +casper.then(function () { + this.evaluate(function () { + var cell = IPython.notebook.get_selected_cell(); + cell.set_text('a=10; print a'); + cell.execute(); + }); +}); + +casper.wait(2000); + +casper.then(function () { + var result = this.evaluate(function () { + var cell = IPython.notebook.get_cell(0); + var output = cell.element.find('.output_area').find('pre').html(); + return output; + }) + this.test.assertEquals(result, '10\n', 'stdout output matches') +}); + +casper.deleteCurrentNotebook(); + +casper.run(function() { + this.test.done(); +}); diff --git a/IPython/frontend/html/notebook/tests/casperjs/test_cases/render_markdown.js b/IPython/frontend/html/notebook/tests/casperjs/test_cases/render_markdown.js new file mode 100644 index 000000000..86a074adb --- /dev/null +++ b/IPython/frontend/html/notebook/tests/casperjs/test_cases/render_markdown.js @@ -0,0 +1,30 @@ +// +// Test that a Markdown cell is rendered to HTML. +// +casper.openNewNotebook(); + +casper.then(function () { + var output = this.evaluate(function() { + // Does it make more sense to test the UI or the JS API here? + // + // $('#cell_type').val('markdown'); + // $('#cell_type').change(); + // $('#run_b').click(); + // + // $('#to_markdown').click(); // fails via jQuery UI menubar + // $('#run_cell').click(); // fails via jQuery UI menubar + IPython.notebook.to_markdown(); + var cell = IPython.notebook.get_selected_cell(); + cell.set_text('# Foo'); + cell.render(); + return cell.get_rendered(); + }); + casper.test.assertEquals(output, '

Foo

', 'Markdown converted to HTML.'); +}); + +casper.deleteCurrentNotebook(); + +// Run the browser automation. +casper.run(function() { + this.test.done(); +}); diff --git a/IPython/frontend/html/notebook/tests/casperjs/util.js b/IPython/frontend/html/notebook/tests/casperjs/util.js new file mode 100644 index 000000000..911d5e877 --- /dev/null +++ b/IPython/frontend/html/notebook/tests/casperjs/util.js @@ -0,0 +1,45 @@ +// +// Utility functions for the HTML notebook's CasperJS tests. +// + +// Get the URL of a notebook server on which to run tests. +casper.getNotebookServer = function () { + return 'http://127.0.0.1:8888'; +}; + +// Create and open a new notebook. +casper.openNewNotebook = function () { + var baseUrl = this.getNotebookServer(); + this.start(baseUrl + '/new'); +}; + +// Shut down the current notebook's kernel. +casper._shutdownCurrentKernel = function () { + this.thenEvaluate(function() { + var baseUrl = $('body').data('baseProjectUrl'); + var kernelId = IPython.notebook.kernel.kernel_id; + var url = baseUrl + 'kernels/' + kernelId; + $.ajax(url, { + type: 'DELETE', + }); + }); +}; + +// Delete created notebook. +casper.deleteCurrentNotebook = function () { + this._shutdownCurrentKernel(); + this.thenEvaluate(function() { + var nbData = $('body').data(); + var url = nbData.baseProjectUrl + 'notebooks/' + nbData.notebookId; + $.ajax(url, { + type: 'DELETE', + }); + }); +}; + +// Pass `console.log` calls from page JS to casper. +casper.printLog = function () { + this.on('remote.message', function(msg) { + this.echo('Remote message caught: ' + msg); + }); +};