##// END OF EJS Templates
Merge pull request #5714 from minrk/warn-api-errors...
Merge pull request #5714 from minrk/warn-api-errors log error message when API requests fail

File last commit:

r15942:9fe4cd5e
r16460:1c4b0f63 merge
Show More
util.js
478 lines | 15.4 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.
//
Paul Ivanov
pep8 style function names
r13275 casper.get_notebook_server = function () {
Jonathan Frederic
Pythonize me captin'...
r15942 // Get the URL of a notebook server on which to run tests.
Jonathan Frederic
demsemicolons
r15939 port = casper.cli.get("port");
Paul Ivanov
updated js tests README, --port= now optional
r13271 port = (typeof port === 'undefined') ? '8888' : port;
Jonathan Frederic
demsemicolons
r15939 return 'http://127.0.0.1:' + port;
David Wyde
Add CasperJS utility functions, and tests for code and...
r13249 };
Paul Ivanov
pep8 style function names
r13275 casper.open_new_notebook = function () {
Jonathan Frederic
Pythonize me captin'...
r15942 // Create and open a new notebook.
Paul Ivanov
pep8 style function names
r13275 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 };
casper.kernel_running = function kernel_running() {
Jonathan Frederic
Pythonize me captin'...
r15942 // Return whether or not the kernel is running.
Paul Ivanov
created a kernel_running method
r13302 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 };
Paul Ivanov
pep8 style function names
r13275 casper.shutdown_current_kernel = function () {
Jonathan Frederic
Pythonize me captin'...
r15942 // Shut down the current notebook's kernel.
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 };
Paul Ivanov
pep8 style function names
r13275 casper.delete_current_notebook = function () {
Jonathan Frederic
Pythonize me captin'...
r15942 // Delete created notebook.
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 () {
Jonathan Frederic
Pythonize me captin'...
r15942 // Waits for the notebook to enter a busy state.
Matthias BUSSONNIER
more subtle kernel indicator...
r15042 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 () {
Jonathan Frederic
Pythonize me captin'...
r15942 // Waits for the notebook to idle.
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 casper.wait_for_output = function (cell_num, out_num) {
Jonathan Frederic
Pythonize me captin'...
r15942 // wait for the nth output in a given cell
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 casper.wait_for_widget = function (widget_info) {
Jonathan Frederic
Pythonize me captin'...
r15942 // 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.
Jonathan Frederic
Remove sleep from the following,...
r14970 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});
Jonathan Frederic
demsemicolons
r15939 if (pending === 0) {
Jonathan Frederic
Remove sleep from the following,...
r14970 return true;
} else {
return false;
}
});
Jonathan Frederic
demsemicolons
r15939 };
Jonathan Frederic
Remove sleep from the following,...
r14970
Paul Ivanov
add test for custom mimetypes
r14147 casper.get_output_cell = function (cell_num, out_num) {
Jonathan Frederic
Pythonize me captin'...
r15942 // return an output of a given cell
Paul Ivanov
add test for custom mimetypes
r14147 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 casper.get_cells_length = function () {
Jonathan Frederic
Pythonize me captin'...
r15942 // return the number of cells in the notebook
Paul Ivanov
add checks for new cell added using shift-enter
r13300 var result = casper.evaluate(function () {
return IPython.notebook.get_cells().length;
Jonathan Frederic
demsemicolons
r15939 });
Paul Ivanov
add checks for new cell added using shift-enter
r13300 return result;
};
Jonathan Frederic
Added new utility functions to the casper.js testing utils.
r13765 casper.set_cell_text = function(index, text){
Jonathan Frederic
Pythonize me captin'...
r15942 // Set the text content of a cell.
Jonathan Frederic
Added new utility functions to the casper.js testing utils.
r13765 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
Added get_cell_text
r15929 casper.get_cell_text = function(index){
Jonathan Frederic
Pythonize me captin'...
r15942 // Get the text content of a cell.
Jonathan Frederic
Added get_cell_text
r15929 return this.evaluate(function (index) {
var cell = IPython.notebook.get_cell(index);
return cell.get_text();
}, index);
};
Jonathan Frederic
Fixed bug where cell was not getting inserted because index is optional.
r13766 casper.insert_cell_at_bottom = function(cell_type){
Jonathan Frederic
Pythonize me captin'...
r15942 // Inserts a cell at the bottom of the notebook
// Returns the new cell's index.
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 casper.append_cell = function(text, cell_type) {
Jonathan Frederic
Pythonize me captin'...
r15942 // Insert a cell at the bottom of the notebook and set the cells text.
// Returns the new cell's index.
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;
};
casper.execute_cell = function(index){
Jonathan Frederic
Pythonize me captin'...
r15942 // Asynchronously executes a cell by index.
// Returns the cell's index.
Jonathan Frederic
Added new utility functions to the casper.js testing utils.
r13765 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;
};
casper.execute_cell_then = function(index, then_callback) {
Jonathan Frederic
Pythonize me captin'...
r15942 // 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.
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;
};
casper.cell_element_exists = function(index, selector){
Jonathan Frederic
Pythonize me captin'...
r15942 // Utility function that allows us to easily check if an element exists
// within a cell. Uses JQuery selector to look for the element.
Jonathan Frederic
Added new utility functions to the casper.js testing utils.
r13765 return casper.evaluate(function (index, selector) {
var $cell = IPython.notebook.get_cell(index).element;
return $cell.find(selector).length > 0;
}, index, selector);
};
casper.cell_element_function = function(index, selector, function_name, function_args){
Jonathan Frederic
Pythonize me captin'...
r15942 // Utility function that allows us to execute a jQuery function on an
// element within a cell.
Jonathan Frederic
Added new utility functions to the casper.js testing utils.
r13765 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);
};
Jonathan Frederic
Move validate notebook state into utils
r15935 casper.validate_notebook_state = function(message, mode, cell_index) {
Jonathan Frederic
Pythonize me captin'...
r15942 // Validate the entire dual mode state of the notebook. Make sure no more than
// one cell is selected, focused, in edit mode, etc...
Jonathan Frederic
Move validate notebook state into utils
r15935 // General tests.
this.test.assertEquals(this.get_keyboard_mode(), this.get_notebook_mode(),
message + '; keyboard and notebook modes match');
// Is the selected cell the only cell that is selected?
if (cell_index!==undefined) {
this.test.assert(this.is_only_cell_selected(cell_index),
message + '; cell ' + cell_index + ' is the only cell selected');
}
// Mode specific tests.
if (mode==='command') {
// Are the notebook and keyboard manager in command mode?
this.test.assertEquals(this.get_keyboard_mode(), 'command',
message + '; in command mode');
// Make sure there isn't a single cell in edit mode.
this.test.assert(this.is_only_cell_edit(null),
message + '; all cells in command mode');
this.test.assert(this.is_cell_editor_focused(null),
message + '; no cell editors are focused while in command mode');
} else if (mode==='edit') {
// Are the notebook and keyboard manager in edit mode?
this.test.assertEquals(this.get_keyboard_mode(), 'edit',
message + '; in edit mode');
if (cell_index!==undefined) {
// Is the specified cell the only cell in edit mode?
this.test.assert(this.is_only_cell_edit(cell_index),
message + '; cell ' + cell_index + ' is the only cell in edit mode');
// Is the specified cell the only cell with a focused code mirror?
this.test.assert(this.is_cell_editor_focused(cell_index),
message + '; cell ' + cell_index + '\'s editor is appropriately focused');
}
} else {
this.test.assert(false, message + '; ' + mode + ' is an unknown mode');
}
};
Jonathan Frederic
Moved util functions into util.js
r15932
casper.select_cell = function(index) {
Jonathan Frederic
Pythonize me captin'...
r15942 // Select a cell in the notebook.
Jonathan Frederic
Moved util functions into util.js
r15932 this.evaluate(function (i) {
IPython.notebook.select(i);
}, {i: index});
};
casper.click_cell_editor = function(index) {
Jonathan Frederic
Pythonize me captin'...
r15942 // Emulate a click on a cell's editor.
Jonathan Frederic
Moved util functions into util.js
r15932 // Code Mirror does not play nicely with emulated brower events.
// Instead of trying to emulate a click, here we run code similar to
// the code used in Code Mirror that handles the mousedown event on a
// region of codemirror that the user can focus.
this.evaluate(function (i) {
Jonathan Frederic
Fixed problem with split tests,...
r15933 var cm = IPython.notebook.get_cell(i).code_mirror;
Jonathan Frederic
Moved util functions into util.js
r15932 if (cm.options.readOnly != "nocursor" && (document.activeElement != cm.display.input))
cm.display.input.focus();
}, {i: index});
};
Jonathan Frederic
Fixed problem with split tests,...
r15933 casper.set_cell_editor_cursor = function(index, line_index, char_index) {
Jonathan Frederic
Pythonize me captin'...
r15942 // Set the Code Mirror instance cursor's location.
Jonathan Frederic
Fixed problem with split tests,...
r15933 this.evaluate(function (i, l, c) {
IPython.notebook.get_cell(i).code_mirror.setCursor(l, c);
}, {i: index, l: line_index, c: char_index});
};
Jonathan Frederic
Moved util functions into util.js
r15932 casper.focus_notebook = function() {
Jonathan Frederic
Pythonize me captin'...
r15942 // Focus the notebook div.
Jonathan Frederic
Moved util functions into util.js
r15932 this.evaluate(function (){
$('#notebook').focus();
}, {});
};
casper.trigger_keydown = function() {
Jonathan Frederic
Pythonize me captin'...
r15942 // Emulate a keydown in the notebook.
Jonathan Frederic
Moved util functions into util.js
r15932 for (var i = 0; i < arguments.length; i++) {
this.evaluate(function (k) {
Jonathan Frederic
Removed trigger keydown from keyboard.js,...
r15937 var element = $(document);
var event = IPython.keyboard.shortcut_to_event(k, 'keydown');
element.trigger(event);
Jonathan Frederic
Moved util functions into util.js
r15932 }, {k: arguments[i]});
}
};
casper.get_keyboard_mode = function() {
Jonathan Frederic
Pythonize me captin'...
r15942 // Get the mode of the keyboard manager.
Jonathan Frederic
Moved util functions into util.js
r15932 return this.evaluate(function() {
return IPython.keyboard_manager.mode;
}, {});
};
casper.get_notebook_mode = function() {
Jonathan Frederic
Pythonize me captin'...
r15942 // Get the mode of the notebook.
Jonathan Frederic
Moved util functions into util.js
r15932 return this.evaluate(function() {
return IPython.notebook.mode;
}, {});
};
casper.get_cell = function(index) {
Jonathan Frederic
Pythonize me captin'...
r15942 // Get a single cell.
//
// Note: Handles to DOM elements stored in the cell will be useless once in
// CasperJS context.
Jonathan Frederic
Moved util functions into util.js
r15932 return this.evaluate(function(i) {
var cell = IPython.notebook.get_cell(i);
if (cell) {
return cell;
}
return null;
}, {i : index});
};
casper.is_cell_editor_focused = function(index) {
Jonathan Frederic
Pythonize me captin'...
r15942 // Make sure a cell's editor is the only editor focused on the page.
Jonathan Frederic
Moved util functions into util.js
r15932 return this.evaluate(function(i) {
Jonathan Frederic
HUGE speed improvements to dual mode tests
r15934 var focused_textarea = $('#notebook .CodeMirror-focused textarea');
if (focused_textarea.length > 1) { throw 'More than one Code Mirror editor is focused at once!'; }
if (i === null) {
return focused_textarea.length === 0;
} else {
var cell = IPython.notebook.get_cell(i);
if (cell) {
return cell.code_mirror.getInputField() == focused_textarea[0];
}
Jonathan Frederic
Moved util functions into util.js
r15932 }
return false;
}, {i : index});
};
casper.is_only_cell_selected = function(index) {
Jonathan Frederic
Pythonize me captin'...
r15942 // Check if a cell is the only cell selected.
// Pass null as the index to check if no cells are selected.
Jonathan Frederic
Moved util functions into util.js
r15932 return this.is_only_cell_on(index, 'selected', 'unselected');
};
casper.is_only_cell_edit = function(index) {
Jonathan Frederic
Pythonize me captin'...
r15942 // Check if a cell is the only cell in edit mode.
// Pass null as the index to check if all of the cells are in command mode.
Jonathan Frederic
Moved util functions into util.js
r15932 return this.is_only_cell_on(index, 'edit_mode', 'command_mode');
};
casper.is_only_cell_on = function(i, on_class, off_class) {
Jonathan Frederic
Pythonize me captin'...
r15942 // Check if a cell is the only cell with the `on_class` DOM class applied to it.
// All of the other cells are checked for the `off_class` DOM class.
// Pass null as the index to check if all of the cells have the `off_class`.
Jonathan Frederic
Moved util functions into util.js
r15932 var cells_length = this.get_cells_length();
for (var j = 0; j < cells_length; j++) {
if (j === i) {
if (this.cell_has_class(j, off_class) || !this.cell_has_class(j, on_class)) {
return false;
}
} else {
if (!this.cell_has_class(j, off_class) || this.cell_has_class(j, on_class)) {
return false;
}
}
}
return true;
};
casper.cell_has_class = function(index, classes) {
Jonathan Frederic
Pythonize me captin'...
r15942 // Check if a cell has a class.
Jonathan Frederic
Moved util functions into util.js
r15932 return this.evaluate(function(i, c) {
var cell = IPython.notebook.get_cell(i);
if (cell) {
return cell.element.hasClass(c);
}
return false;
}, {i : index, c: classes});
};
Paul Ivanov
pep8 style function names
r13275 casper.notebook_test = function(test) {
Jonathan Frederic
Pythonize me captin'...
r15942 // Wrap a notebook test to reduce boilerplate.
Paul Ivanov
pep8 style function names
r13275 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');
Jonathan Frederic
demsemicolons
r15939 };
Brian E. Granger
Fixing casperjs tests to run on casperjs 1.0.x.
r15081
casper.open_dashboard = function () {
// Start casper by opening the dashboard page.
var baseUrl = this.get_notebook_server();
this.start(baseUrl);
this.wait_for_dashboard();
Jonathan Frederic
demsemicolons
r15939 };
Brian E. Granger
Fixing casperjs tests to run on casperjs 1.0.x.
r15081
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();
});
Jonathan Frederic
demsemicolons
r15939 };
Brian E. Granger
Fixing casperjs tests to run on casperjs 1.0.x.
r15081
Jonathan Frederic
demsemicolons
r15939 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?");
});
Jonathan Frederic
HUGE speed improvements to dual mode tests
r15934 casper.print_log = function () {
Jonathan Frederic
Pythonize me captin'...
r15942 // Pass `console.log` calls from page JS to casper.
David Wyde
Add CasperJS utility functions, and tests for code and...
r13249 this.on('remote.message', function(msg) {
this.echo('Remote message caught: ' + msg);
});
};