##// END OF EJS Templates
DRY: style in one place, removed code duplication
DRY: style in one place, removed code duplication

File last commit:

r15201:029ac024
r15801:bf507b30
Show More
util.js
292 lines | 8.6 KiB | application/javascript | JavascriptLexer
David Wyde
Add CasperJS utility functions, and tests for code and...
r13249 //
// Utility functions for the HTML notebook's CasperJS tests.
//
// Get the URL of a notebook server on which to run tests.
Paul Ivanov
pep8 style function names
r13275 casper.get_notebook_server = function () {
Paul Ivanov
updated js tests README, --port= now optional
r13271 port = casper.cli.get("port")
port = (typeof port === 'undefined') ? '8888' : port;
return 'http://127.0.0.1:' + port
David Wyde
Add CasperJS utility functions, and tests for code and...
r13249 };
// Create and open a new notebook.
Paul Ivanov
pep8 style function names
r13275 casper.open_new_notebook = function () {
var baseUrl = this.get_notebook_server();
Paul Ivanov
use dashboard to simulate clicking new notebook
r13284 this.start(baseUrl);
this.thenClick('button#new_notebook');
this.waitForPopup('');
Paul Ivanov
make sure kernel started running
r13290 this.withPopup('', function () {this.waitForSelector('.CodeMirror-code');});
Paul Ivanov
use dashboard to simulate clicking new notebook
r13284 this.then(function () {
this.open(this.popups[0].url);
});
Brian E. Granger
Fixing issues with js tests....
r14965 // Make sure the kernel has started
Paul Ivanov
created a kernel_running method
r13302 this.waitFor( this.kernel_running );
MinRK
add utils.wait_for_idle in js tests...
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
created a kernel_running method
r13302 };
Brian E. Granger
Fixing issues with js tests....
r14965 // Return whether or not the kernel is running.
Paul Ivanov
created a kernel_running method
r13302 casper.kernel_running = function kernel_running() {
return this.evaluate(function kernel_running() {
return IPython.notebook.kernel.running;
Paul Ivanov
make sure kernel started running
r13290 });
David Wyde
Add CasperJS utility functions, and tests for code and...
r13249 };
// Shut down the current notebook's kernel.
Paul Ivanov
pep8 style function names
r13275 casper.shutdown_current_kernel = function () {
David Wyde
Add CasperJS utility functions, and tests for code and...
r13249 this.thenEvaluate(function() {
David Wyde
Use existing IPython method to kill kernels.
r13255 IPython.notebook.kernel.kill();
David Wyde
Add CasperJS utility functions, and tests for code and...
r13249 });
Brian E. Granger
Fixing issues with js tests....
r14965 // We close the page right after this so we need to give it time to complete.
this.wait(1000);
David Wyde
Add CasperJS utility functions, and tests for code and...
r13249 };
// Delete created notebook.
Paul Ivanov
pep8 style function names
r13275 casper.delete_current_notebook = function () {
Brian E. Granger
Fixing issues with js tests....
r14965 // For some unknown reason, this doesn't work?!?
David Wyde
Add CasperJS utility functions, and tests for code and...
r13249 this.thenEvaluate(function() {
Brian E. Granger
Fixing issues with js tests....
r14965 IPython.notebook.delete();
David Wyde
Add CasperJS utility functions, and tests for code and...
r13249 });
};
Matthias BUSSONNIER
more subtle kernel indicator...
r15042 casper.wait_for_busy = function () {
this.waitFor(function () {
return this.evaluate(function () {
return IPython._status == 'busy';
});
});
};
MinRK
add utils.wait_for_idle in js tests...
r14933 casper.wait_for_idle = function () {
MinRK
remove superfluous then
r14934 this.waitFor(function () {
return this.evaluate(function () {
return IPython._status == 'idle';
MinRK
add utils.wait_for_idle in js tests...
r14933 });
});
};
MinRK
add wait_for_output(cell, index)...
r14904 // wait for the nth output in a given cell
casper.wait_for_output = function (cell_num, out_num) {
MinRK
add utils.wait_for_idle in js tests...
r14933 this.wait_for_idle();
MinRK
add wait_for_output(cell, index)...
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
Cleaned up utilities in widget casper js tests
r14435 },
function then() { },
function timeout() {
this.echo("wait_for_output timed out!");
Paul Ivanov
wait_for_output utility function
r13294 });
};
Jonathan Frederic
Remove sleep from the following,...
r14970 // wait for a widget msg que to reach 0
//
// Parameters
// ----------
// widget_info : object
// Object which contains info related to the widget. The model_id property
// is used to identify the widget.
casper.wait_for_widget = function (widget_info) {
this.waitFor(function () {
var pending = this.evaluate(function (m) {
return IPython.notebook.kernel.widget_manager.get_model(m).pending_msgs;
}, {m: widget_info.model_id});
if (pending == 0) {
return true;
} else {
return false;
}
});
}
MinRK
get_output_cell fails with no such output...
r14864 // return an output of a given cell
Paul Ivanov
add test for custom mimetypes
r14147 casper.get_output_cell = function (cell_num, out_num) {
out_num = out_num || 0;
var result = casper.evaluate(function (c, o) {
Paul Ivanov
refactor of get_output_cell
r13299 var cell = IPython.notebook.get_cell(c);
Paul Ivanov
add test for custom mimetypes
r14147 return cell.output_area.outputs[o];
Paul Ivanov
refactor of get_output_cell
r13299 },
Paul Ivanov
add test for custom mimetypes
r14147 {c : cell_num, o : out_num});
MinRK
get_output_cell fails with no such output...
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
refactor of get_output_cell
r13299 };
Paul Ivanov
add checks for new cell added using shift-enter
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
Added new utility functions to the casper.js testing utils.
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
add utils.wait_for_idle in js tests...
r14933 }, index, text);
Jonathan Frederic
Added new utility functions to the casper.js testing utils.
r13765 };
Jonathan Frederic
Fixed bug where cell was not getting inserted because index is optional.
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
Fixing issues with js tests....
r14965 cell_type = cell_type || 'code';
Jonathan Frederic
Fixed bug where cell was not getting inserted because index is optional.
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
add utils.wait_for_idle in js tests...
r14933 }, cell_type);
Jonathan Frederic
Fixed bug where cell was not getting inserted because index is optional.
r13766 };
Jonathan Frederic
Added new utility functions to the casper.js testing utils.
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
Fixed bug where cell was not getting inserted because index is optional.
r13766 var index = this.insert_cell_at_bottom(cell_type);
Jonathan Frederic
Added new utility functions to the casper.js testing utils.
r13765 if (text !== undefined) {
Jonathan Frederic
Fixed bug where cell was not getting inserted because index is optional.
r13766 this.set_cell_text(index, text);
Jonathan Frederic
Added new utility functions to the casper.js testing utils.
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
add utils.wait_for_idle in js tests...
r14933 }, index);
Jonathan Frederic
Added new utility functions to the casper.js testing utils.
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
Fixed bug where cell was not getting inserted because index is optional.
r13766 var return_val = this.execute_cell(index);
Jonathan Frederic
Added new utility functions to the casper.js testing utils.
r13765
MinRK
add utils.wait_for_idle in js tests...
r14933 this.wait_for_idle();
Jonathan Frederic
Added new utility functions to the casper.js testing utils.
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
Wrap CasperJS tests in a helper function to reduce boilerplate.
r13253 // Wrap a notebook test to reduce boilerplate.
Paul Ivanov
pep8 style function names
r13275 casper.notebook_test = function(test) {
this.open_new_notebook();
David Wyde
Wrap CasperJS tests in a helper function to reduce boilerplate.
r13253 this.then(test);
Brian E. Granger
Fixing issues with js tests....
r14965
// Kill the kernel and delete the notebook.
Paul Ivanov
try to shutdown at the end of every notebook run...
r13288 this.shutdown_current_kernel();
Brian E. Granger
Fixing issues with js tests....
r14965 // This is still broken but shouldn't be a problem for now.
// this.delete_current_notebook();
Paul Ivanov
don't shutdown kernel after every test
r13285
Brian E. Granger
Fixing issues with js tests....
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
Wrap CasperJS tests in a helper function to reduce boilerplate.
r13253
// Run the browser automation.
this.run(function() {
this.test.done();
});
};
Brian E. Granger
Fixing casperjs tests to run on casperjs 1.0.x.
r15081 casper.wait_for_dashboard = function () {
// Wait for the dashboard list to load.
casper.waitForSelector('.list_item');
}
casper.open_dashboard = function () {
// Start casper by opening the dashboard page.
var baseUrl = this.get_notebook_server();
this.start(baseUrl);
this.wait_for_dashboard();
}
casper.dashboard_test = function (test) {
// Open the dashboard page and run a test.
this.open_dashboard();
this.then(test);
this.then(function () {
this.page.close();
this.page = null;
});
// Run the browser automation.
this.run(function() {
this.test.done();
});
}
Paul Ivanov
give travis 10 seconds to start server
r13309 casper.options.waitTimeout=10000
Paul Ivanov
more informative message on server timeout
r13272 casper.on('waitFor.timeout', function onWaitForTimeout(timeout) {
Paul Ivanov
pep8 style function names
r13275 this.echo("Timeout for " + casper.get_notebook_server());
Paul Ivanov
more informative message on server timeout
r13272 this.echo("Is the notebook server running?");
});
David Wyde
Add CasperJS utility functions, and tests for code and...
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);
});
};