Permalink
Browse files

Wrap CasperJS tests in a helper function to reduce boilerplate.

  • Loading branch information...
1 parent 5f2b2eb commit 7c087fd9b387cfdb17c9b0637995a1046afb5e9d @dwyde committed Apr 3, 2013
@@ -1,9 +1,7 @@
//
// Check for errors with up and down arrow presses in an empty notebook.
//
-casper.openNewNotebook();
-
-casper.then(function () {
+casper.notebookTest(function () {
var result = this.evaluate(function() {
var ncells = IPython.notebook.ncells(),
i;
@@ -20,12 +18,5 @@ casper.then(function () {
$(document).trigger(down_press);
return true;
});
- casper.test.assertTrue(result, 'Up/down arrow okay in empty notebook.');
-});
-
-casper.deleteCurrentNotebook();
-
-// Run the browser automation.
-casper.run(function() {
- this.test.done();
+ this.test.assertTrue(result, 'Up/down arrow okay in empty notebook.');
});
@@ -1,29 +1,21 @@
//
// Test code cell execution.
//
-casper.openNewNotebook();
-
-casper.then(function () {
+casper.notebookTest(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();
+ this.wait(2000);
-casper.run(function() {
- this.test.done();
+ 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')
+ });
});
@@ -1,9 +1,7 @@
//
// Test that a Markdown cell is rendered to HTML.
//
-casper.openNewNotebook();
-
-casper.then(function () {
+casper.notebookTest(function () {
var output = this.evaluate(function() {
// Does it make more sense to test the UI or the JS API here?
//
@@ -19,12 +17,5 @@ casper.then(function () {
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();
+ this.test.assertEquals(output, '<h1>Foo</h1>', 'Markdown converted to HTML.');
});
@@ -14,7 +14,7 @@ casper.openNewNotebook = function () {
};
// Shut down the current notebook's kernel.
-casper._shutdownCurrentKernel = function () {
+casper.shutdownCurrentKernel = function () {
this.thenEvaluate(function() {
var baseUrl = $('body').data('baseProjectUrl');
var kernelId = IPython.notebook.kernel.kernel_id;
@@ -27,7 +27,6 @@ casper._shutdownCurrentKernel = function () {
// Delete created notebook.
casper.deleteCurrentNotebook = function () {
- this._shutdownCurrentKernel();
this.thenEvaluate(function() {
var nbData = $('body').data();
var url = nbData.baseProjectUrl + 'notebooks/' + nbData.notebookId;
@@ -37,6 +36,19 @@ casper.deleteCurrentNotebook = function () {
});
};
+// 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) {

0 comments on commit 7c087fd

Please sign in to comment.