##// END OF EJS Templates
Pythonize me captin'...
Jonathan Frederic -
Show More
@@ -2,15 +2,15
2 // Utility functions for the HTML notebook's CasperJS tests.
2 // Utility functions for the HTML notebook's CasperJS tests.
3 //
3 //
4
4
5 // Get the URL of a notebook server on which to run tests.
6 casper.get_notebook_server = function () {
5 casper.get_notebook_server = function () {
6 // Get the URL of a notebook server on which to run tests.
7 port = casper.cli.get("port");
7 port = casper.cli.get("port");
8 port = (typeof port === 'undefined') ? '8888' : port;
8 port = (typeof port === 'undefined') ? '8888' : port;
9 return 'http://127.0.0.1:' + port;
9 return 'http://127.0.0.1:' + port;
10 };
10 };
11
11
12 // Create and open a new notebook.
13 casper.open_new_notebook = function () {
12 casper.open_new_notebook = function () {
13 // Create and open a new notebook.
14 var baseUrl = this.get_notebook_server();
14 var baseUrl = this.get_notebook_server();
15 this.start(baseUrl);
15 this.start(baseUrl);
16 this.thenClick('button#new_notebook');
16 this.thenClick('button#new_notebook');
@@ -34,15 +34,15 casper.open_new_notebook = function () {
34 });
34 });
35 };
35 };
36
36
37 // Return whether or not the kernel is running.
38 casper.kernel_running = function kernel_running() {
37 casper.kernel_running = function kernel_running() {
38 // Return whether or not the kernel is running.
39 return this.evaluate(function kernel_running() {
39 return this.evaluate(function kernel_running() {
40 return IPython.notebook.kernel.running;
40 return IPython.notebook.kernel.running;
41 });
41 });
42 };
42 };
43
43
44 // Shut down the current notebook's kernel.
45 casper.shutdown_current_kernel = function () {
44 casper.shutdown_current_kernel = function () {
45 // Shut down the current notebook's kernel.
46 this.thenEvaluate(function() {
46 this.thenEvaluate(function() {
47 IPython.notebook.kernel.kill();
47 IPython.notebook.kernel.kill();
48 });
48 });
@@ -50,8 +50,9 casper.shutdown_current_kernel = function () {
50 this.wait(1000);
50 this.wait(1000);
51 };
51 };
52
52
53 // Delete created notebook.
54 casper.delete_current_notebook = function () {
53 casper.delete_current_notebook = function () {
54 // Delete created notebook.
55
55 // For some unknown reason, this doesn't work?!?
56 // For some unknown reason, this doesn't work?!?
56 this.thenEvaluate(function() {
57 this.thenEvaluate(function() {
57 IPython.notebook.delete();
58 IPython.notebook.delete();
@@ -59,6 +60,7 casper.delete_current_notebook = function () {
59 };
60 };
60
61
61 casper.wait_for_busy = function () {
62 casper.wait_for_busy = function () {
63 // Waits for the notebook to enter a busy state.
62 this.waitFor(function () {
64 this.waitFor(function () {
63 return this.evaluate(function () {
65 return this.evaluate(function () {
64 return IPython._status == 'busy';
66 return IPython._status == 'busy';
@@ -67,6 +69,7 casper.wait_for_busy = function () {
67 };
69 };
68
70
69 casper.wait_for_idle = function () {
71 casper.wait_for_idle = function () {
72 // Waits for the notebook to idle.
70 this.waitFor(function () {
73 this.waitFor(function () {
71 return this.evaluate(function () {
74 return this.evaluate(function () {
72 return IPython._status == 'idle';
75 return IPython._status == 'idle';
@@ -74,8 +77,8 casper.wait_for_idle = function () {
74 });
77 });
75 };
78 };
76
79
77 // wait for the nth output in a given cell
78 casper.wait_for_output = function (cell_num, out_num) {
80 casper.wait_for_output = function (cell_num, out_num) {
81 // wait for the nth output in a given cell
79 this.wait_for_idle();
82 this.wait_for_idle();
80 out_num = out_num || 0;
83 out_num = out_num || 0;
81 this.then(function() {
84 this.then(function() {
@@ -94,14 +97,14 casper.wait_for_output = function (cell_num, out_num) {
94 });
97 });
95 };
98 };
96
99
97 // wait for a widget msg que to reach 0
98 //
99 // Parameters
100 // ----------
101 // widget_info : object
102 // Object which contains info related to the widget. The model_id property
103 // is used to identify the widget.
104 casper.wait_for_widget = function (widget_info) {
100 casper.wait_for_widget = function (widget_info) {
101 // wait for a widget msg que to reach 0
102 //
103 // Parameters
104 // ----------
105 // widget_info : object
106 // Object which contains info related to the widget. The model_id property
107 // is used to identify the widget.
105 this.waitFor(function () {
108 this.waitFor(function () {
106 var pending = this.evaluate(function (m) {
109 var pending = this.evaluate(function (m) {
107 return IPython.notebook.kernel.widget_manager.get_model(m).pending_msgs;
110 return IPython.notebook.kernel.widget_manager.get_model(m).pending_msgs;
@@ -115,8 +118,8 casper.wait_for_widget = function (widget_info) {
115 });
118 });
116 };
119 };
117
120
118 // return an output of a given cell
119 casper.get_output_cell = function (cell_num, out_num) {
121 casper.get_output_cell = function (cell_num, out_num) {
122 // return an output of a given cell
120 out_num = out_num || 0;
123 out_num = out_num || 0;
121 var result = casper.evaluate(function (c, o) {
124 var result = casper.evaluate(function (c, o) {
122 var cell = IPython.notebook.get_cell(c);
125 var cell = IPython.notebook.get_cell(c);
@@ -137,33 +140,33 casper.get_output_cell = function (cell_num, out_num) {
137 }
140 }
138 };
141 };
139
142
140 // return the number of cells in the notebook
141 casper.get_cells_length = function () {
143 casper.get_cells_length = function () {
144 // return the number of cells in the notebook
142 var result = casper.evaluate(function () {
145 var result = casper.evaluate(function () {
143 return IPython.notebook.get_cells().length;
146 return IPython.notebook.get_cells().length;
144 });
147 });
145 return result;
148 return result;
146 };
149 };
147
150
148 // Set the text content of a cell.
149 casper.set_cell_text = function(index, text){
151 casper.set_cell_text = function(index, text){
152 // Set the text content of a cell.
150 this.evaluate(function (index, text) {
153 this.evaluate(function (index, text) {
151 var cell = IPython.notebook.get_cell(index);
154 var cell = IPython.notebook.get_cell(index);
152 cell.set_text(text);
155 cell.set_text(text);
153 }, index, text);
156 }, index, text);
154 };
157 };
155
158
156 // Get the text content of a cell.
157 casper.get_cell_text = function(index){
159 casper.get_cell_text = function(index){
160 // Get the text content of a cell.
158 return this.evaluate(function (index) {
161 return this.evaluate(function (index) {
159 var cell = IPython.notebook.get_cell(index);
162 var cell = IPython.notebook.get_cell(index);
160 return cell.get_text();
163 return cell.get_text();
161 }, index);
164 }, index);
162 };
165 };
163
166
164 // Inserts a cell at the bottom of the notebook
165 // Returns the new cell's index.
166 casper.insert_cell_at_bottom = function(cell_type){
167 casper.insert_cell_at_bottom = function(cell_type){
168 // Inserts a cell at the bottom of the notebook
169 // Returns the new cell's index.
167 cell_type = cell_type || 'code';
170 cell_type = cell_type || 'code';
168
171
169 return this.evaluate(function (cell_type) {
172 return this.evaluate(function (cell_type) {
@@ -172,9 +175,9 casper.insert_cell_at_bottom = function(cell_type){
172 }, cell_type);
175 }, cell_type);
173 };
176 };
174
177
175 // Insert a cell at the bottom of the notebook and set the cells text.
176 // Returns the new cell's index.
177 casper.append_cell = function(text, cell_type) {
178 casper.append_cell = function(text, cell_type) {
179 // Insert a cell at the bottom of the notebook and set the cells text.
180 // Returns the new cell's index.
178 var index = this.insert_cell_at_bottom(cell_type);
181 var index = this.insert_cell_at_bottom(cell_type);
179 if (text !== undefined) {
182 if (text !== undefined) {
180 this.set_cell_text(index, text);
183 this.set_cell_text(index, text);
@@ -182,9 +185,9 casper.append_cell = function(text, cell_type) {
182 return index;
185 return index;
183 };
186 };
184
187
185 // Asynchronously executes a cell by index.
186 // Returns the cell's index.
187 casper.execute_cell = function(index){
188 casper.execute_cell = function(index){
189 // Asynchronously executes a cell by index.
190 // Returns the cell's index.
188 var that = this;
191 var that = this;
189 this.then(function(){
192 this.then(function(){
190 that.evaluate(function (index) {
193 that.evaluate(function (index) {
@@ -195,11 +198,11 casper.execute_cell = function(index){
195 return index;
198 return index;
196 };
199 };
197
200
198 // Synchronously executes a cell by index.
199 // Optionally accepts a then_callback parameter. then_callback will get called
200 // when the cell has finished executing.
201 // Returns the cell's index.
202 casper.execute_cell_then = function(index, then_callback) {
201 casper.execute_cell_then = function(index, then_callback) {
202 // Synchronously executes a cell by index.
203 // Optionally accepts a then_callback parameter. then_callback will get called
204 // when the cell has finished executing.
205 // Returns the cell's index.
203 var return_val = this.execute_cell(index);
206 var return_val = this.execute_cell(index);
204
207
205 this.wait_for_idle();
208 this.wait_for_idle();
@@ -214,18 +217,18 casper.execute_cell_then = function(index, then_callback) {
214 return return_val;
217 return return_val;
215 };
218 };
216
219
217 // Utility function that allows us to easily check if an element exists
218 // within a cell. Uses JQuery selector to look for the element.
219 casper.cell_element_exists = function(index, selector){
220 casper.cell_element_exists = function(index, selector){
221 // Utility function that allows us to easily check if an element exists
222 // within a cell. Uses JQuery selector to look for the element.
220 return casper.evaluate(function (index, selector) {
223 return casper.evaluate(function (index, selector) {
221 var $cell = IPython.notebook.get_cell(index).element;
224 var $cell = IPython.notebook.get_cell(index).element;
222 return $cell.find(selector).length > 0;
225 return $cell.find(selector).length > 0;
223 }, index, selector);
226 }, index, selector);
224 };
227 };
225
228
226 // Utility function that allows us to execute a jQuery function on an
227 // element within a cell.
228 casper.cell_element_function = function(index, selector, function_name, function_args){
229 casper.cell_element_function = function(index, selector, function_name, function_args){
230 // Utility function that allows us to execute a jQuery function on an
231 // element within a cell.
229 return casper.evaluate(function (index, selector, function_name, function_args) {
232 return casper.evaluate(function (index, selector, function_name, function_args) {
230 var $cell = IPython.notebook.get_cell(index).element;
233 var $cell = IPython.notebook.get_cell(index).element;
231 var $el = $cell.find(selector);
234 var $el = $cell.find(selector);
@@ -233,9 +236,10 casper.cell_element_function = function(index, selector, function_name, function
233 }, index, selector, function_name, function_args);
236 }, index, selector, function_name, function_args);
234 };
237 };
235
238
236 // Validate the entire dual mode state of the notebook. Make sure no more than
237 // one cell is selected, focused, in edit mode, etc...
238 casper.validate_notebook_state = function(message, mode, cell_index) {
239 casper.validate_notebook_state = function(message, mode, cell_index) {
240 // Validate the entire dual mode state of the notebook. Make sure no more than
241 // one cell is selected, focused, in edit mode, etc...
242
239 // General tests.
243 // General tests.
240 this.test.assertEquals(this.get_keyboard_mode(), this.get_notebook_mode(),
244 this.test.assertEquals(this.get_keyboard_mode(), this.get_notebook_mode(),
241 message + '; keyboard and notebook modes match');
245 message + '; keyboard and notebook modes match');
@@ -274,15 +278,16 casper.validate_notebook_state = function(message, mode, cell_index) {
274 }
278 }
275 };
279 };
276
280
277 // Select a cell in the notebook.
278 casper.select_cell = function(index) {
281 casper.select_cell = function(index) {
282 // Select a cell in the notebook.
279 this.evaluate(function (i) {
283 this.evaluate(function (i) {
280 IPython.notebook.select(i);
284 IPython.notebook.select(i);
281 }, {i: index});
285 }, {i: index});
282 };
286 };
283
287
284 // Emulate a click on a cell's editor.
285 casper.click_cell_editor = function(index) {
288 casper.click_cell_editor = function(index) {
289 // Emulate a click on a cell's editor.
290
286 // Code Mirror does not play nicely with emulated brower events.
291 // Code Mirror does not play nicely with emulated brower events.
287 // Instead of trying to emulate a click, here we run code similar to
292 // Instead of trying to emulate a click, here we run code similar to
288 // the code used in Code Mirror that handles the mousedown event on a
293 // the code used in Code Mirror that handles the mousedown event on a
@@ -294,22 +299,22 casper.click_cell_editor = function(index) {
294 }, {i: index});
299 }, {i: index});
295 };
300 };
296
301
297 // Set the Code Mirror instance cursor's location.
298 casper.set_cell_editor_cursor = function(index, line_index, char_index) {
302 casper.set_cell_editor_cursor = function(index, line_index, char_index) {
303 // Set the Code Mirror instance cursor's location.
299 this.evaluate(function (i, l, c) {
304 this.evaluate(function (i, l, c) {
300 IPython.notebook.get_cell(i).code_mirror.setCursor(l, c);
305 IPython.notebook.get_cell(i).code_mirror.setCursor(l, c);
301 }, {i: index, l: line_index, c: char_index});
306 }, {i: index, l: line_index, c: char_index});
302 };
307 };
303
308
304 // Focus the notebook div.
305 casper.focus_notebook = function() {
309 casper.focus_notebook = function() {
310 // Focus the notebook div.
306 this.evaluate(function (){
311 this.evaluate(function (){
307 $('#notebook').focus();
312 $('#notebook').focus();
308 }, {});
313 }, {});
309 };
314 };
310
315
311 // Emulate a keydown in the notebook.
312 casper.trigger_keydown = function() {
316 casper.trigger_keydown = function() {
317 // Emulate a keydown in the notebook.
313 for (var i = 0; i < arguments.length; i++) {
318 for (var i = 0; i < arguments.length; i++) {
314 this.evaluate(function (k) {
319 this.evaluate(function (k) {
315 var element = $(document);
320 var element = $(document);
@@ -319,25 +324,25 casper.trigger_keydown = function() {
319 }
324 }
320 };
325 };
321
326
322 // Get the mode of the keyboard manager.
323 casper.get_keyboard_mode = function() {
327 casper.get_keyboard_mode = function() {
328 // Get the mode of the keyboard manager.
324 return this.evaluate(function() {
329 return this.evaluate(function() {
325 return IPython.keyboard_manager.mode;
330 return IPython.keyboard_manager.mode;
326 }, {});
331 }, {});
327 };
332 };
328
333
329 // Get the mode of the notebook.
330 casper.get_notebook_mode = function() {
334 casper.get_notebook_mode = function() {
335 // Get the mode of the notebook.
331 return this.evaluate(function() {
336 return this.evaluate(function() {
332 return IPython.notebook.mode;
337 return IPython.notebook.mode;
333 }, {});
338 }, {});
334 };
339 };
335
340
336 // Get a single cell.
337 //
338 // Note: Handles to DOM elements stored in the cell will be useless once in
339 // CasperJS context.
340 casper.get_cell = function(index) {
341 casper.get_cell = function(index) {
342 // Get a single cell.
343 //
344 // Note: Handles to DOM elements stored in the cell will be useless once in
345 // CasperJS context.
341 return this.evaluate(function(i) {
346 return this.evaluate(function(i) {
342 var cell = IPython.notebook.get_cell(i);
347 var cell = IPython.notebook.get_cell(i);
343 if (cell) {
348 if (cell) {
@@ -347,8 +352,8 casper.get_cell = function(index) {
347 }, {i : index});
352 }, {i : index});
348 };
353 };
349
354
350 // Make sure a cell's editor is the only editor focused on the page.
351 casper.is_cell_editor_focused = function(index) {
355 casper.is_cell_editor_focused = function(index) {
356 // Make sure a cell's editor is the only editor focused on the page.
352 return this.evaluate(function(i) {
357 return this.evaluate(function(i) {
353 var focused_textarea = $('#notebook .CodeMirror-focused textarea');
358 var focused_textarea = $('#notebook .CodeMirror-focused textarea');
354 if (focused_textarea.length > 1) { throw 'More than one Code Mirror editor is focused at once!'; }
359 if (focused_textarea.length > 1) { throw 'More than one Code Mirror editor is focused at once!'; }
@@ -364,22 +369,22 casper.is_cell_editor_focused = function(index) {
364 }, {i : index});
369 }, {i : index});
365 };
370 };
366
371
367 // Check if a cell is the only cell selected.
368 // Pass null as the index to check if no cells are selected.
369 casper.is_only_cell_selected = function(index) {
372 casper.is_only_cell_selected = function(index) {
373 // Check if a cell is the only cell selected.
374 // Pass null as the index to check if no cells are selected.
370 return this.is_only_cell_on(index, 'selected', 'unselected');
375 return this.is_only_cell_on(index, 'selected', 'unselected');
371 };
376 };
372
377
373 // Check if a cell is the only cell in edit mode.
374 // Pass null as the index to check if all of the cells are in command mode.
375 casper.is_only_cell_edit = function(index) {
378 casper.is_only_cell_edit = function(index) {
379 // Check if a cell is the only cell in edit mode.
380 // Pass null as the index to check if all of the cells are in command mode.
376 return this.is_only_cell_on(index, 'edit_mode', 'command_mode');
381 return this.is_only_cell_on(index, 'edit_mode', 'command_mode');
377 };
382 };
378
383
379 // Check if a cell is the only cell with the `on_class` DOM class applied to it.
380 // All of the other cells are checked for the `off_class` DOM class.
381 // Pass null as the index to check if all of the cells have the `off_class`.
382 casper.is_only_cell_on = function(i, on_class, off_class) {
384 casper.is_only_cell_on = function(i, on_class, off_class) {
385 // Check if a cell is the only cell with the `on_class` DOM class applied to it.
386 // All of the other cells are checked for the `off_class` DOM class.
387 // Pass null as the index to check if all of the cells have the `off_class`.
383 var cells_length = this.get_cells_length();
388 var cells_length = this.get_cells_length();
384 for (var j = 0; j < cells_length; j++) {
389 for (var j = 0; j < cells_length; j++) {
385 if (j === i) {
390 if (j === i) {
@@ -395,8 +400,8 casper.is_only_cell_on = function(i, on_class, off_class) {
395 return true;
400 return true;
396 };
401 };
397
402
398 // Check if a cell has a class.
399 casper.cell_has_class = function(index, classes) {
403 casper.cell_has_class = function(index, classes) {
404 // Check if a cell has a class.
400 return this.evaluate(function(i, c) {
405 return this.evaluate(function(i, c) {
401 var cell = IPython.notebook.get_cell(i);
406 var cell = IPython.notebook.get_cell(i);
402 if (cell) {
407 if (cell) {
@@ -406,8 +411,8 casper.cell_has_class = function(index, classes) {
406 }, {i : index, c: classes});
411 }, {i : index, c: classes});
407 };
412 };
408
413
409 // Wrap a notebook test to reduce boilerplate.
410 casper.notebook_test = function(test) {
414 casper.notebook_test = function(test) {
415 // Wrap a notebook test to reduce boilerplate.
411 this.open_new_notebook();
416 this.open_new_notebook();
412 this.then(test);
417 this.then(test);
413
418
@@ -465,8 +470,8 casper.on('waitFor.timeout', function onWaitForTimeout(timeout) {
465 this.echo("Is the notebook server running?");
470 this.echo("Is the notebook server running?");
466 });
471 });
467
472
468 // Pass `console.log` calls from page JS to casper.
469 casper.print_log = function () {
473 casper.print_log = function () {
474 // Pass `console.log` calls from page JS to casper.
470 this.on('remote.message', function(msg) {
475 this.on('remote.message', function(msg) {
471 this.echo('Remote message caught: ' + msg);
476 this.echo('Remote message caught: ' + msg);
472 });
477 });
General Comments 0
You need to be logged in to leave comments. Login now