Permalink
Browse files

Add CasperJS utility functions, and tests for code and

Markdown cell execution.

Code cell test is based on https://gist.github.com/ellisonbg/3329929.
  • Loading branch information...
1 parent 53a38cb commit 9c117a5de7fdcb4031ac91ae005effd0d3db2b8f @dwyde committed Apr 2, 2013
View
29 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();
+});
View
30 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, '<h1>Foo</h1>', 'Markdown converted to HTML.');
+});
+
+casper.deleteCurrentNotebook();
+
+// Run the browser automation.
+casper.run(function() {
+ this.test.done();
+});
View
45 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);
+ });
+};

0 comments on commit 9c117a5

Please sign in to comment.