util.js
832 lines
| 27.9 KiB
| application/javascript
|
JavascriptLexer
David Wyde
|
r13249 | // | ||
// Utility functions for the HTML notebook's CasperJS tests. | ||||
// | ||||
Paul Ivanov
|
r13275 | casper.get_notebook_server = function () { | ||
Jonathan Frederic
|
r15942 | // Get the URL of a notebook server on which to run tests. | ||
Jonathan Frederic
|
r18433 | var port = casper.cli.get("port"); | ||
Paul Ivanov
|
r13271 | port = (typeof port === 'undefined') ? '8888' : port; | ||
Jonathan Frederic
|
r18445 | return casper.cli.get("url") || ('http://127.0.0.1:' + port); | ||
David Wyde
|
r13249 | }; | ||
Paul Ivanov
|
r13275 | casper.open_new_notebook = function () { | ||
Jonathan Frederic
|
r15942 | // Create and open a new notebook. | ||
Paul Ivanov
|
r13275 | var baseUrl = this.get_notebook_server(); | ||
Paul Ivanov
|
r13284 | this.start(baseUrl); | ||
Jonathan Frederic
|
r17212 | this.waitFor(this.page_loaded); | ||
Min RK
|
r20106 | this.waitForSelector('#kernel-python2 a, #kernel-python3 a'); | ||
Jonathan Frederic
|
r19698 | this.thenClick('#kernel-python2 a, #kernel-python3 a'); | ||
Paul Ivanov
|
r13284 | this.waitForPopup(''); | ||
Paul Ivanov
|
r13290 | this.withPopup('', function () {this.waitForSelector('.CodeMirror-code');}); | ||
Paul Ivanov
|
r13284 | this.then(function () { | ||
this.open(this.popups[0].url); | ||||
}); | ||||
Jonathan Frederic
|
r17212 | this.waitFor(this.page_loaded); | ||
Paul Ivanov
|
r13284 | |||
Jonathan Frederic
|
r18903 | // Hook the log and error methods of the console, forcing them to | ||
// serialize their arguments before printing. This allows the | ||||
// Objects to cross into the phantom/slimer regime for display. | ||||
Jonathan Frederic
|
r18901 | this.thenEvaluate(function(){ | ||
Jonathan Frederic
|
r18903 | var serialize_arguments = function(f, context) { | ||
Jonathan Frederic
|
r18901 | return function() { | ||
var pretty_arguments = []; | ||||
for (var i = 0; i < arguments.length; i++) { | ||||
var value = arguments[i]; | ||||
if (value instanceof Object) { | ||||
Jonathan Frederic
|
r18904 | var name = value.name || 'Object'; | ||
// Print a JSON string representation of the object. | ||||
// If we don't do this, [Object object] gets printed | ||||
// by casper, which is useless. The long regular | ||||
// expression reduces the verbosity of the JSON. | ||||
pretty_arguments.push(name + ' {' + JSON.stringify(value, null, ' ') | ||||
.replace(/(\s+)?({)?(\s+)?(}(\s+)?,?)?(\s+)?(\s+)?\n/g, '\n') | ||||
.replace(/\n(\s+)?\n/g, '\n')); | ||||
Jonathan Frederic
|
r18901 | } else { | ||
pretty_arguments.push(value); | ||||
} | ||||
} | ||||
f.apply(context, pretty_arguments); | ||||
}; | ||||
}; | ||||
Jonathan Frederic
|
r18903 | console.log = serialize_arguments(console.log, console); | ||
console.error = serialize_arguments(console.error, console); | ||||
Jonathan Frederic
|
r18901 | }); | ||
Brian E. Granger
|
r14965 | // Make sure the kernel has started | ||
Jonathan Frederic
|
r17212 | this.waitFor(this.kernel_running); | ||
MinRK
|
r14933 | // track the IPython busy/idle state | ||
this.thenEvaluate(function () { | ||||
MinRK
|
r17324 | require(['base/js/namespace', 'base/js/events'], function (IPython, events) { | ||
Jessica B. Hamrick
|
r18238 | events.on('kernel_idle.Kernel',function () { | ||
MinRK
|
r17324 | IPython._status = 'idle'; | ||
}); | ||||
Jessica B. Hamrick
|
r18238 | events.on('kernel_busy.Kernel',function () { | ||
MinRK
|
r17324 | IPython._status = 'busy'; | ||
}); | ||||
MinRK
|
r14933 | }); | ||
}); | ||||
Jonathan Frederic
|
r16828 | |||
// Because of the asynchronous nature of SlimerJS (Gecko), we need to make | ||||
// sure the notebook has actually been loaded into the IPython namespace | ||||
// before running any tests. | ||||
this.waitFor(function() { | ||||
return this.evaluate(function () { | ||||
return IPython.notebook; | ||||
}); | ||||
}); | ||||
Paul Ivanov
|
r13302 | }; | ||
Jonathan Frederic
|
r17212 | casper.page_loaded = function() { | ||
Jonathan Frederic
|
r15942 | // Return whether or not the kernel is running. | ||
Jonathan Frederic
|
r17212 | return this.evaluate(function() { | ||
Jessica B. Hamrick
|
r18076 | return typeof IPython !== "undefined" && | ||
MinRK
|
r17318 | IPython.page !== undefined; | ||
Jonathan Frederic
|
r17212 | }); | ||
}; | ||||
casper.kernel_running = function() { | ||||
// Return whether or not the kernel is running. | ||||
return this.evaluate(function() { | ||||
Jonathan Frederic
|
r18449 | return IPython && | ||
IPython.notebook && | ||||
IPython.notebook.kernel && | ||||
IPython.notebook.kernel.is_connected(); | ||||
Paul Ivanov
|
r13290 | }); | ||
David Wyde
|
r13249 | }; | ||
Jessica B. Hamrick
|
r18223 | casper.kernel_disconnected = function() { | ||
return this.evaluate(function() { | ||||
return IPython.notebook.kernel.is_fully_disconnected(); | ||||
}); | ||||
}; | ||||
Jessica B. Hamrick
|
r18232 | casper.wait_for_kernel_ready = function () { | ||
this.waitFor(this.kernel_running); | ||||
this.thenEvaluate(function () { | ||||
IPython._kernel_ready = false; | ||||
IPython.notebook.kernel.kernel_info( | ||||
function () { | ||||
IPython._kernel_ready = true; | ||||
}); | ||||
}); | ||||
this.waitFor(function () { | ||||
return this.evaluate(function () { | ||||
return IPython._kernel_ready; | ||||
}); | ||||
}); | ||||
}; | ||||
Paul Ivanov
|
r13275 | casper.shutdown_current_kernel = function () { | ||
Jonathan Frederic
|
r15942 | // Shut down the current notebook's kernel. | ||
David Wyde
|
r13249 | this.thenEvaluate(function() { | ||
Thomas Kluyver
|
r17224 | IPython.notebook.session.delete(); | ||
David Wyde
|
r13249 | }); | ||
Brian E. Granger
|
r14965 | // We close the page right after this so we need to give it time to complete. | ||
this.wait(1000); | ||||
David Wyde
|
r13249 | }; | ||
Paul Ivanov
|
r13275 | casper.delete_current_notebook = function () { | ||
Jonathan Frederic
|
r15942 | // Delete created notebook. | ||
Brian E. Granger
|
r14965 | // For some unknown reason, this doesn't work?!? | ||
David Wyde
|
r13249 | this.thenEvaluate(function() { | ||
Brian E. Granger
|
r14965 | IPython.notebook.delete(); | ||
David Wyde
|
r13249 | }); | ||
}; | ||||
Matthias BUSSONNIER
|
r15042 | casper.wait_for_busy = function () { | ||
Jonathan Frederic
|
r15942 | // Waits for the notebook to enter a busy state. | ||
Matthias BUSSONNIER
|
r15042 | this.waitFor(function () { | ||
return this.evaluate(function () { | ||||
return IPython._status == 'busy'; | ||||
}); | ||||
}); | ||||
}; | ||||
MinRK
|
r14933 | casper.wait_for_idle = function () { | ||
Jonathan Frederic
|
r15942 | // Waits for the notebook to idle. | ||
MinRK
|
r14934 | this.waitFor(function () { | ||
return this.evaluate(function () { | ||||
return IPython._status == 'idle'; | ||||
MinRK
|
r14933 | }); | ||
}); | ||||
}; | ||||
MinRK
|
r14904 | casper.wait_for_output = function (cell_num, out_num) { | ||
Jonathan Frederic
|
r15942 | // wait for the nth output in a given cell | ||
MinRK
|
r14933 | this.wait_for_idle(); | ||
MinRK
|
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
|
r14435 | }, | ||
function then() { }, | ||||
function timeout() { | ||||
this.echo("wait_for_output timed out!"); | ||||
Paul Ivanov
|
r13294 | }); | ||
}; | ||||
Jonathan Frederic
|
r14970 | casper.wait_for_widget = function (widget_info) { | ||
Jonathan Frederic
|
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
|
r18910 | |||
// Clear the results of a previous query, if they exist. Make sure a | ||||
// dictionary exists to store the async results in. | ||||
this.thenEvaluate(function(model_id) { | ||||
if (window.pending_msgs === undefined) { | ||||
window.pending_msgs = {}; | ||||
} else { | ||||
window.pending_msgs[model_id] = -1; | ||||
} | ||||
}, {model_id: widget_info.model_id}); | ||||
// Wait for the pending messages to be 0. | ||||
Jonathan Frederic
|
r14970 | this.waitFor(function () { | ||
Jonathan Frederic
|
r18910 | var pending = this.evaluate(function (model_id) { | ||
// Get the model. Once the model is had, store it's pending_msgs | ||||
// count in the window's dictionary. | ||||
IPython.notebook.kernel.widget_manager.get_model(model_id) | ||||
.then(function(model) { | ||||
window.pending_msgs[model_id] = model.pending_msgs; | ||||
}); | ||||
// Return the pending_msgs result. | ||||
return window.pending_msgs[model_id]; | ||||
}, {model_id: widget_info.model_id}); | ||||
Jonathan Frederic
|
r14970 | |||
Jonathan Frederic
|
r15939 | if (pending === 0) { | ||
Jonathan Frederic
|
r14970 | return true; | ||
} else { | ||||
return false; | ||||
} | ||||
}); | ||||
Jonathan Frederic
|
r15939 | }; | ||
Jonathan Frederic
|
r14970 | |||
Paul Ivanov
|
r14147 | casper.get_output_cell = function (cell_num, out_num) { | ||
Jonathan Frederic
|
r15942 | // return an output of a given cell | ||
Paul Ivanov
|
r14147 | out_num = out_num || 0; | ||
var result = casper.evaluate(function (c, o) { | ||||
Paul Ivanov
|
r13299 | var cell = IPython.notebook.get_cell(c); | ||
Paul Ivanov
|
r14147 | return cell.output_area.outputs[o]; | ||
Paul Ivanov
|
r13299 | }, | ||
Paul Ivanov
|
r14147 | {c : cell_num, o : out_num}); | ||
MinRK
|
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
|
r13299 | }; | ||
Paul Ivanov
|
r13300 | casper.get_cells_length = function () { | ||
Jonathan Frederic
|
r15942 | // return the number of cells in the notebook | ||
Paul Ivanov
|
r13300 | var result = casper.evaluate(function () { | ||
return IPython.notebook.get_cells().length; | ||||
Jonathan Frederic
|
r15939 | }); | ||
Paul Ivanov
|
r13300 | return result; | ||
}; | ||||
Jonathan Frederic
|
r13765 | casper.set_cell_text = function(index, text){ | ||
Jonathan Frederic
|
r15942 | // Set the text content of a cell. | ||
Jonathan Frederic
|
r13765 | this.evaluate(function (index, text) { | ||
var cell = IPython.notebook.get_cell(index); | ||||
cell.set_text(text); | ||||
MinRK
|
r14933 | }, index, text); | ||
Jonathan Frederic
|
r13765 | }; | ||
Jonathan Frederic
|
r15929 | casper.get_cell_text = function(index){ | ||
Jonathan Frederic
|
r15942 | // Get the text content of a cell. | ||
Jonathan Frederic
|
r15929 | return this.evaluate(function (index) { | ||
var cell = IPython.notebook.get_cell(index); | ||||
return cell.get_text(); | ||||
}, index); | ||||
}; | ||||
Jonathan Frederic
|
r13766 | casper.insert_cell_at_bottom = function(cell_type){ | ||
Jonathan Frederic
|
r15942 | // Inserts a cell at the bottom of the notebook | ||
// Returns the new cell's index. | ||||
Jonathan Frederic
|
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
|
r14933 | }, cell_type); | ||
Jonathan Frederic
|
r13766 | }; | ||
Jonathan Frederic
|
r13765 | casper.append_cell = function(text, cell_type) { | ||
Jonathan Frederic
|
r15942 | // Insert a cell at the bottom of the notebook and set the cells text. | ||
// Returns the new cell's index. | ||||
Jonathan Frederic
|
r13766 | var index = this.insert_cell_at_bottom(cell_type); | ||
Jonathan Frederic
|
r13765 | if (text !== undefined) { | ||
Jonathan Frederic
|
r13766 | this.set_cell_text(index, text); | ||
Jonathan Frederic
|
r13765 | } | ||
return index; | ||||
}; | ||||
MinRK
|
r18170 | casper.execute_cell = function(index, expect_failure){ | ||
Jonathan Frederic
|
r15942 | // Asynchronously executes a cell by index. | ||
// Returns the cell's index. | ||||
MinRK
|
r18170 | |||
if (expect_failure === undefined) expect_failure = false; | ||||
Jonathan Frederic
|
r13765 | var that = this; | ||
this.then(function(){ | ||||
that.evaluate(function (index) { | ||||
var cell = IPython.notebook.get_cell(index); | ||||
cell.execute(); | ||||
MinRK
|
r14933 | }, index); | ||
Jonathan Frederic
|
r13765 | }); | ||
MinRK
|
r18170 | this.wait_for_idle(); | ||
this.then(function () { | ||||
var error = that.evaluate(function (index) { | ||||
var cell = IPython.notebook.get_cell(index); | ||||
var outputs = cell.output_area.outputs; | ||||
for (var i = 0; i < outputs.length; i++) { | ||||
if (outputs[i].output_type == 'error') { | ||||
return outputs[i]; | ||||
} | ||||
} | ||||
return false; | ||||
}, index); | ||||
if (error === null) { | ||||
this.test.fail("Failed to check for error output"); | ||||
} | ||||
if (expect_failure && error === false) { | ||||
this.test.fail("Expected error while running cell"); | ||||
} else if (!expect_failure && error !== false) { | ||||
this.test.fail("Error running cell:\n" + error.traceback.join('\n')); | ||||
} | ||||
}); | ||||
Jonathan Frederic
|
r13765 | return index; | ||
}; | ||||
MinRK
|
r18170 | casper.execute_cell_then = function(index, then_callback, expect_failure) { | ||
Jonathan Frederic
|
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. | ||||
MinRK
|
r18170 | var return_val = this.execute_cell(index, expect_failure); | ||
Jonathan Frederic
|
r13765 | |||
MinRK
|
r14933 | this.wait_for_idle(); | ||
Jonathan Frederic
|
r13765 | |||
var that = this; | ||||
this.then(function(){ | ||||
if (then_callback!==undefined) { | ||||
then_callback.apply(that, [index]); | ||||
} | ||||
MinRK
|
r18170 | }); | ||
Jonathan Frederic
|
r13765 | |||
return return_val; | ||||
}; | ||||
Jonathan Frederic
|
r18910 | casper.wait_for_element = function(index, selector){ | ||
Jonathan Frederic
|
r18909 | // Utility function that allows us to easily wait for an element | ||
// within a cell. Uses JQuery selector to look for the element. | ||||
var that = this; | ||||
this.waitFor(function() { | ||||
return that.cell_element_exists(index, selector); | ||||
Jonathan Frederic
|
r18910 | }); | ||
Jonathan Frederic
|
r18909 | }; | ||
Jonathan Frederic
|
r13765 | casper.cell_element_exists = function(index, selector){ | ||
Jonathan Frederic
|
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
|
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
|
r15942 | // Utility function that allows us to execute a jQuery function on an | ||
// element within a cell. | ||||
Jonathan Frederic
|
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
|
r15935 | casper.validate_notebook_state = function(message, mode, cell_index) { | ||
Jonathan Frederic
|
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
|
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), | ||||
Matthias Bussonnier
|
r19702 | message + '; cell ' + cell_index + ' is the only cell in edit mode '+ this.cells_modes()); | ||
Jonathan Frederic
|
r15935 | // 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
|
r15932 | |||
casper.select_cell = function(index) { | ||||
Jonathan Frederic
|
r15942 | // Select a cell in the notebook. | ||
Jonathan Frederic
|
r15932 | this.evaluate(function (i) { | ||
IPython.notebook.select(i); | ||||
}, {i: index}); | ||||
}; | ||||
casper.click_cell_editor = function(index) { | ||||
Jonathan Frederic
|
r15942 | // Emulate a click on a cell's editor. | ||
Jonathan Frederic
|
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
|
r15933 | var cm = IPython.notebook.get_cell(i).code_mirror; | ||
Matthias Bussonnier
|
r19739 | if (cm.options.readOnly != "nocursor" && (document.activeElement != cm.display.input)){ | ||
Jonathan Frederic
|
r15932 | cm.display.input.focus(); | ||
Matthias Bussonnier
|
r19739 | } | ||
Jonathan Frederic
|
r15932 | }, {i: index}); | ||
}; | ||||
Jonathan Frederic
|
r15933 | casper.set_cell_editor_cursor = function(index, line_index, char_index) { | ||
Jonathan Frederic
|
r15942 | // Set the Code Mirror instance cursor's location. | ||
Jonathan Frederic
|
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
|
r15932 | casper.focus_notebook = function() { | ||
Jonathan Frederic
|
r15942 | // Focus the notebook div. | ||
Jonathan Frederic
|
r15932 | this.evaluate(function (){ | ||
$('#notebook').focus(); | ||||
}, {}); | ||||
}; | ||||
casper.trigger_keydown = function() { | ||||
Jonathan Frederic
|
r15942 | // Emulate a keydown in the notebook. | ||
Jonathan Frederic
|
r15932 | for (var i = 0; i < arguments.length; i++) { | ||
this.evaluate(function (k) { | ||||
Jonathan Frederic
|
r15937 | var element = $(document); | ||
var event = IPython.keyboard.shortcut_to_event(k, 'keydown'); | ||||
element.trigger(event); | ||||
Jonathan Frederic
|
r15932 | }, {k: arguments[i]}); | ||
} | ||||
}; | ||||
casper.get_keyboard_mode = function() { | ||||
Jonathan Frederic
|
r15942 | // Get the mode of the keyboard manager. | ||
Jonathan Frederic
|
r15932 | return this.evaluate(function() { | ||
return IPython.keyboard_manager.mode; | ||||
}, {}); | ||||
}; | ||||
casper.get_notebook_mode = function() { | ||||
Jonathan Frederic
|
r15942 | // Get the mode of the notebook. | ||
Jonathan Frederic
|
r15932 | return this.evaluate(function() { | ||
return IPython.notebook.mode; | ||||
}, {}); | ||||
}; | ||||
casper.get_cell = function(index) { | ||||
Jonathan Frederic
|
r15942 | // Get a single cell. | ||
// | ||||
// Note: Handles to DOM elements stored in the cell will be useless once in | ||||
// CasperJS context. | ||||
Jonathan Frederic
|
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
|
r15942 | // Make sure a cell's editor is the only editor focused on the page. | ||
Jonathan Frederic
|
r15932 | return this.evaluate(function(i) { | ||
Jonathan Frederic
|
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
|
r15932 | } | ||
return false; | ||||
}, {i : index}); | ||||
}; | ||||
casper.is_only_cell_selected = function(index) { | ||||
Jonathan Frederic
|
r15942 | // Check if a cell is the only cell selected. | ||
// Pass null as the index to check if no cells are selected. | ||||
Jonathan Frederic
|
r15932 | return this.is_only_cell_on(index, 'selected', 'unselected'); | ||
}; | ||||
casper.is_only_cell_edit = function(index) { | ||||
Jonathan Frederic
|
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. | ||||
Bussonnier Matthias
|
r19671 | var cells_length = this.get_cells_length(); | ||
for (var j = 0; j < cells_length; j++) { | ||||
Bussonnier Matthias
|
r19672 | if (j === index) { | ||
Matthias Bussonnier
|
r19702 | if (!this.cell_mode_is(j, 'edit')) { | ||
Bussonnier Matthias
|
r19671 | return false; | ||
} | ||||
} else { | ||||
Matthias Bussonnier
|
r19702 | if (this.cell_mode_is(j, 'edit')) { | ||
Bussonnier Matthias
|
r19671 | return false; | ||
} | ||||
} | ||||
} | ||||
return true; | ||||
Jonathan Frederic
|
r15932 | }; | ||
casper.is_only_cell_on = function(i, on_class, off_class) { | ||||
Jonathan Frederic
|
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
|
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; | ||||
}; | ||||
Matthias Bussonnier
|
r19702 | casper.cells_modes = function(){ | ||
return this.evaluate(function(){ | ||||
return IPython.notebook.get_cells().map(function(x,c){return x.mode}) | ||||
}, {}); | ||||
}; | ||||
Bussonnier Matthias
|
r19671 | |||
casper.cell_mode_is = function(index, mode) { | ||||
// Check if a cell is in a specific mode | ||||
Bussonnier Matthias
|
r19672 | return this.evaluate(function(i, m) { | ||
Bussonnier Matthias
|
r19671 | var cell = IPython.notebook.get_cell(i); | ||
if (cell) { | ||||
Bussonnier Matthias
|
r19672 | return cell.mode === m; | ||
Bussonnier Matthias
|
r19671 | } | ||
return false; | ||||
}, {i : index, m: mode}); | ||||
}; | ||||
Jonathan Frederic
|
r15932 | casper.cell_has_class = function(index, classes) { | ||
Jonathan Frederic
|
r15942 | // Check if a cell has a class. | ||
Jonathan Frederic
|
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}); | ||||
}; | ||||
Jonathan Frederic
|
r16843 | casper.is_cell_rendered = function (index) { | ||
Jonathan Frederic
|
r16840 | return this.evaluate(function(i) { | ||
return !!IPython.notebook.get_cell(i).rendered; | ||||
Jonathan Frederic
|
r16843 | }, {i:index}); | ||
Jonathan Frederic
|
r16840 | }; | ||
Jonathan Frederic
|
r16839 | casper.assert_colors_equal = function (hex_color, local_color, msg) { | ||
// Tests to see if two colors are equal. | ||||
// | ||||
// Parameters | ||||
// hex_color: string | ||||
// Hexadecimal color code, with or without preceeding hash character. | ||||
// local_color: string | ||||
// Local color representation. Can either be hexadecimal (default for | ||||
// phantom) or rgb (default for slimer). | ||||
// Remove parentheses, hashes, semi-colons, and space characters. | ||||
hex_color = hex_color.replace(/[\(\); #]/, ''); | ||||
local_color = local_color.replace(/[\(\); #]/, ''); | ||||
// If the local color is rgb, clean it up and replace | ||||
if (local_color.substr(0,3).toLowerCase() == 'rgb') { | ||||
Matthias Bussonnier
|
r19739 | var components = local_color.substr(3).split(','); | ||
Jonathan Frederic
|
r16839 | local_color = ''; | ||
for (var i = 0; i < components.length; i++) { | ||||
var part = parseInt(components[i]).toString(16); | ||||
while (part.length < 2) part = '0' + part; | ||||
local_color += part; | ||||
} | ||||
} | ||||
this.test.assertEquals(hex_color.toUpperCase(), local_color.toUpperCase(), msg); | ||||
}; | ||||
Paul Ivanov
|
r13275 | casper.notebook_test = function(test) { | ||
Jonathan Frederic
|
r15942 | // Wrap a notebook test to reduce boilerplate. | ||
Paul Ivanov
|
r13275 | this.open_new_notebook(); | ||
Brian E. Granger
|
r14965 | |||
Jonathan Frederic
|
r16828 | // Echo whether or not we are running this test using SlimerJS | ||
if (this.evaluate(function(){ | ||||
return typeof InstallTrigger !== 'undefined'; // Firefox 1.0+ | ||||
Jonathan Frederic
|
r16841 | })) { | ||
console.log('This test is running in SlimerJS.'); | ||||
this.slimerjs = true; | ||||
} | ||||
Jonathan Frederic
|
r16828 | |||
// Make sure to remove the onbeforeunload callback. This callback is | ||||
jon
|
r16861 | // responsible for the "Are you sure you want to quit?" type messages. | ||
Jonathan Frederic
|
r16828 | // PhantomJS ignores these prompts, SlimerJS does not which causes hangs. | ||
this.then(function(){ | ||||
this.evaluate(function(){ | ||||
window.onbeforeunload = function(){}; | ||||
MinRK
|
r17338 | }); | ||
Jonathan Frederic
|
r16828 | }); | ||
this.then(test); | ||||
Brian E. Granger
|
r14965 | // Kill the kernel and delete the notebook. | ||
Paul Ivanov
|
r13288 | this.shutdown_current_kernel(); | ||
Brian E. Granger
|
r14965 | // This is still broken but shouldn't be a problem for now. | ||
// this.delete_current_notebook(); | ||||
Paul Ivanov
|
r13285 | |||
Brian E. Granger
|
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
|
r13253 | |||
// Run the browser automation. | ||||
this.run(function() { | ||||
this.test.done(); | ||||
}); | ||||
}; | ||||
Brian E. Granger
|
r15081 | casper.wait_for_dashboard = function () { | ||
// Wait for the dashboard list to load. | ||||
casper.waitForSelector('.list_item'); | ||||
Jonathan Frederic
|
r15939 | }; | ||
Brian E. Granger
|
r15081 | |||
casper.open_dashboard = function () { | ||||
// Start casper by opening the dashboard page. | ||||
var baseUrl = this.get_notebook_server(); | ||||
this.start(baseUrl); | ||||
Jonathan Frederic
|
r17212 | this.waitFor(this.page_loaded); | ||
Brian E. Granger
|
r15081 | this.wait_for_dashboard(); | ||
Jonathan Frederic
|
r15939 | }; | ||
Brian E. Granger
|
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
|
r15939 | }; | ||
Brian E. Granger
|
r15081 | |||
Jessica B. Hamrick
|
r18223 | // note that this will only work for UNIQUE events -- if you want to | ||
// listen for the same event twice, this will not work! | ||||
Jessica B. Hamrick
|
r18233 | casper.event_test = function (name, events, action, timeout) { | ||
Jessica B. Hamrick
|
r18223 | |||
// set up handlers to listen for each of the events | ||||
this.thenEvaluate(function (events) { | ||||
var make_handler = function (event) { | ||||
return function () { | ||||
IPython._events_triggered.push(event); | ||||
IPython.notebook.events.off(event, null, IPython._event_handlers[event]); | ||||
delete IPython._event_handlers[event]; | ||||
}; | ||||
}; | ||||
IPython._event_handlers = {}; | ||||
IPython._events_triggered = []; | ||||
for (var i=0; i < events.length; i++) { | ||||
IPython._event_handlers[events[i]] = make_handler(events[i]); | ||||
IPython.notebook.events.on(events[i], IPython._event_handlers[events[i]]); | ||||
} | ||||
}, [events]); | ||||
// execute the requested action | ||||
this.then(action); | ||||
// wait for all the events to be triggered | ||||
this.waitFor(function () { | ||||
return this.evaluate(function (events) { | ||||
return IPython._events_triggered.length >= events.length; | ||||
}, [events]); | ||||
Jessica B. Hamrick
|
r18233 | }, undefined, undefined, timeout); | ||
Jessica B. Hamrick
|
r18223 | |||
// test that the events were triggered in the proper order | ||||
this.then(function () { | ||||
var triggered = this.evaluate(function () { | ||||
return IPython._events_triggered; | ||||
}); | ||||
var handlers = this.evaluate(function () { | ||||
return Object.keys(IPython._event_handlers); | ||||
}); | ||||
this.test.assertEquals(triggered.length, events.length, name + ': ' + events.length + ' events were triggered'); | ||||
this.test.assertEquals(handlers.length, 0, name + ': all handlers triggered'); | ||||
for (var i=0; i < events.length; i++) { | ||||
this.test.assertEquals(triggered[i], events[i], name + ': ' + events[i] + ' was triggered'); | ||||
} | ||||
}); | ||||
// turn off any remaining event listeners | ||||
this.thenEvaluate(function () { | ||||
for (var event in IPython._event_handlers) { | ||||
IPython.notebook.events.off(event, null, IPython._event_handlers[event]); | ||||
delete IPython._event_handlers[event]; | ||||
} | ||||
}); | ||||
}; | ||||
Jonathan Frederic
|
r15939 | casper.options.waitTimeout=10000; | ||
Paul Ivanov
|
r13272 | casper.on('waitFor.timeout', function onWaitForTimeout(timeout) { | ||
Paul Ivanov
|
r13275 | this.echo("Timeout for " + casper.get_notebook_server()); | ||
Paul Ivanov
|
r13272 | this.echo("Is the notebook server running?"); | ||
}); | ||||
Jonathan Frederic
|
r15934 | casper.print_log = function () { | ||
Jonathan Frederic
|
r15942 | // Pass `console.log` calls from page JS to casper. | ||
David Wyde
|
r13249 | this.on('remote.message', function(msg) { | ||
this.echo('Remote message caught: ' + msg); | ||||
}); | ||||
}; | ||||
MinRK
|
r17338 | |||
casper.on("page.error", function onError(msg, trace) { | ||||
// show errors in the browser | ||||
Jonathan Frederic
|
r18900 | this.echo("Page Error"); | ||
Jonathan Frederic
|
r18903 | this.echo(" Message: " + msg.split('\n').join('\n ')); | ||
Jonathan Frederic
|
r18900 | this.echo(" Call stack:"); | ||
var local_path = this.get_notebook_server(); | ||||
MinRK
|
r17338 | for (var i = 0; i < trace.length; i++) { | ||
var frame = trace[i]; | ||||
var file = frame.file; | ||||
// shorten common phantomjs evaluate url | ||||
// this will have a different value on slimerjs | ||||
if (file === "phantomjs://webpage.evaluate()") { | ||||
file = "evaluate"; | ||||
} | ||||
Jonathan Frederic
|
r18900 | // remove the version tag from the path | ||
file = file.replace(/(\?v=[0-9abcdef]+)/, ''); | ||||
// remove the local address from the beginning of the path | ||||
if (file.indexOf(local_path) === 0) { | ||||
file = file.substr(local_path.length); | ||||
MinRK
|
r17338 | } | ||
Jonathan Frederic
|
r18901 | var frame_text = (frame.function.length > 0) ? " in " + frame.function : ""; | ||
this.echo(" line " + frame.line + " of " + file + frame_text); | ||||
MinRK
|
r17338 | } | ||
}); | ||||
casper.capture_log = function () { | ||||
// show captured errors | ||||
var captured_log = []; | ||||
var seen_errors = 0; | ||||
this.on('remote.message', function(msg) { | ||||
captured_log.push(msg); | ||||
}); | ||||
Jonathan Frederic
|
r18901 | |||
var that = this; | ||||
MinRK
|
r17338 | this.test.on("test.done", function (result) { | ||
// test.done runs per-file, | ||||
// but suiteResults is per-suite (directory) | ||||
var current_errors; | ||||
if (this.suiteResults) { | ||||
// casper 1.1 has suiteResults | ||||
current_errors = this.suiteResults.countErrors() + this.suiteResults.countFailed(); | ||||
} else { | ||||
// casper 1.0 has testResults instead | ||||
current_errors = this.testResults.failed; | ||||
} | ||||
if (current_errors > seen_errors && captured_log.length > 0) { | ||||
casper.echo("\nCaptured console.log:"); | ||||
for (var i = 0; i < captured_log.length; i++) { | ||||
Jonathan Frederic
|
r18901 | var output = String(captured_log[i]).split('\n'); | ||
for (var j = 0; j < output.length; j++) { | ||||
casper.echo(" " + output[j]); | ||||
} | ||||
MinRK
|
r17338 | } | ||
} | ||||
Jonathan Frederic
|
r18901 | |||
MinRK
|
r17338 | seen_errors = current_errors; | ||
captured_log = []; | ||||
}); | ||||
}; | ||||
Jonathan Frederic
|
r18906 | casper.interact = function() { | ||
// Start an interactive Javascript console. | ||||
var system = require('system'); | ||||
system.stdout.writeLine('JS interactive console.'); | ||||
system.stdout.writeLine('Type `exit` to quit.'); | ||||
function read_line() { | ||||
system.stdout.writeLine('JS: '); | ||||
var line = system.stdin.readLine(); | ||||
return line; | ||||
} | ||||
var input = read_line(); | ||||
while (input.trim() != 'exit') { | ||||
var output = this.evaluate(function(code) { | ||||
return String(eval(code)); | ||||
}, {code: input}); | ||||
system.stdout.writeLine('\nOut: ' + output); | ||||
input = read_line(); | ||||
} | ||||
}; | ||||
MinRK
|
r17338 | casper.capture_log(); | ||