Notebook CasperJS tests #3125

Closed
wants to merge 8 commits into
from
View
13 IPython/frontend/html/notebook/tests/casperjs/README.md
@@ -0,0 +1,13 @@
+Regression tests for the web notebook. These tests depend on
+[CasperJS](http://casperjs.org/), which in turn requires
+a recent version of [PhantomJS](http://phantomjs.org/).
+
+Run the tests:
+
+```sh
+/path/to/bin/casperjs test --includes=util.js test_cases
+```
+
+The file `util.js` contains utility functions for tests,
+including a hardcoded path to a running notebook server
+(http://127.0.0.1:8888 by default).
View
22 IPython/frontend/html/notebook/tests/casperjs/test_cases/empty_nb_arrow_keys.js
@@ -0,0 +1,22 @@
+//
+// Check for errors with up and down arrow presses in an empty notebook.
+//
+casper.notebookTest(function () {
+ var result = this.evaluate(function() {
+ var ncells = IPython.notebook.ncells(),
+ i;
+
+ // Delete all cells.
+ for (i = 0; i < ncells; i++) {
+ IPython.notebook.delete_cell();
+ }
+
+ // Simulate the "up arrow" and "down arrow" keys.
+ var up_press = $.Event('keydown', {which: $.ui.keyCode.UP});
+ $(document).trigger(up_press);
+ var down_press = $.Event('keydown', {which: $.ui.keyCode.DOWN});
+ $(document).trigger(down_press);
+ return true;
+ });
+ this.test.assertTrue(result, 'Up/down arrow okay in empty notebook.');
+});
View
21 IPython/frontend/html/notebook/tests/casperjs/test_cases/execute_code_cell.js
@@ -0,0 +1,21 @@
+//
+// Test code cell execution.
+//
+casper.notebookTest(function () {
+ this.evaluate(function () {
+ var cell = IPython.notebook.get_selected_cell();
+ cell.set_text('a=10; print a');
+ cell.execute();
+ });
+
+ this.wait(2000);
+
+ this.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')
+ });
+});
View
37 IPython/frontend/html/notebook/tests/casperjs/test_cases/merge_cells.js
@@ -0,0 +1,37 @@
+//
+// Test merging two notebook cells.
+//
+casper.notebookTest(function() {
+ var output = this.evaluate(function () {
+ // Fill in test data.
+ var set_cell_text = function () {
+ var cell_one = IPython.notebook.get_selected_cell();
+ cell_one.set_text('a = 5');
+
+ IPython.notebook.insert_cell_below('code');
+ var cell_two = IPython.notebook.get_selected_cell();
+ cell_two.set_text('print a');
+ };
+
+ // merge_cell_above()
+ set_cell_text();
+ IPython.notebook.merge_cell_above();
+ var merged_above = IPython.notebook.get_selected_cell();
+
+ // merge_cell_below()
+ set_cell_text();
+ IPython.notebook.select(0);
+ IPython.notebook.merge_cell_below();
+ var merged_below = IPython.notebook.get_selected_cell();
+
+ return {
+ above: merged_above.get_text(),
+ below: merged_below.get_text()
+ };
+ });
+
+ this.test.assertEquals(output.above, 'a = 5\nprint a',
+ 'Successful insert_cell_above().');
+ this.test.assertEquals(output.below, 'a = 5\nprint a',
+ 'Successful insert_cell_below().');
+});
View
36 IPython/frontend/html/notebook/tests/casperjs/test_cases/render_markdown.js
@@ -0,0 +1,36 @@
+//
+// Test that a Markdown cell is rendered to HTML.
+//
+casper.notebookTest(function () {
+ // Test JavaScript models.
+ var output = this.evaluate(function () {
+ IPython.notebook.to_markdown();
+ var cell = IPython.notebook.get_selected_cell();
+ cell.set_text('# Foo');
+ cell.render();
+ return cell.get_rendered();
+ });
+ this.test.assertEquals(output, '<h1>Foo</h1>', 'Markdown JS API works.');
+
+ // Test menubar entries.
+ output = this.evaluate(function () {
+ $('#to_code').mouseenter().click();
+ $('#to_markdown').mouseenter().click();
+ var cell = IPython.notebook.get_selected_cell();
+ cell.set_text('# Foo');
+ $('#run_cell').mouseenter().click();
+ return cell.get_rendered();
+ });
+ this.test.assertEquals(output, '<h1>Foo</h1>', 'Markdown menubar items work.');
+
+ // Test toolbar buttons.
+ output = this.evaluate(function () {
+ $('#cell_type').val('code').change();
+ $('#cell_type').val('markdown').change();
+ var cell = IPython.notebook.get_selected_cell();
+ cell.set_text('# Foo');
+ $('#run_b').click();
+ return cell.get_rendered();
+ });
+ this.test.assertEquals(output, '<h1>Foo</h1>', 'Markdown toolbar items work.');
+});
View
52 IPython/frontend/html/notebook/tests/casperjs/util.js
@@ -0,0 +1,52 @@
+//
+// 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() {
+ IPython.notebook.kernel.kill();
+ });
+};
+
+// Delete created notebook.
+casper.deleteCurrentNotebook = function () {
+ this.thenEvaluate(function() {
+ var nbData = $('body').data();
+ var url = nbData.baseProjectUrl + 'notebooks/' + nbData.notebookId;
+ $.ajax(url, {
+ type: 'DELETE',
+ });
+ });
+};
+
+// Wrap a notebook test to reduce boilerplate.
+casper.notebookTest = function(test) {
+ this.openNewNotebook();
+ this.then(test);
+ this.shutdownCurrentKernel();
+ this.deleteCurrentNotebook();
+
+ // Run the browser automation.
+ this.run(function() {
+ this.test.done();
+ });
+};
+
+// Pass `console.log` calls from page JS to casper.
+casper.printLog = function () {
+ this.on('remote.message', function(msg) {
+ this.echo('Remote message caught: ' + msg);
+ });
+};