##// END OF EJS Templates
refactor of get_output_cell
Paul Ivanov -
Show More
@@ -1,45 +1,39
1 1 //
2 2 // Test kernel interrupt
3 3 //
4 4 casper.notebook_test(function () {
5 5 this.evaluate(function () {
6 6 var cell = IPython.notebook.get_cell(0);
7 7 cell.set_text('import time\nfor x in range(3):\n time.sleep(1)');
8 8 cell.execute();
9 9 });
10 10
11 11
12 12 // interrupt using menu item (Kernel -> Interrupt)
13 13 this.thenClick('li#int_kernel');
14 14
15 15 this.wait_for_output(0);
16 16
17 17 this.then(function () {
18 var result = this.evaluate(function () {
19 var cell = IPython.notebook.get_cell(0);
20 return cell.output_area.outputs[0].ename;
21 })
22 this.test.assertEquals(result, 'KeyboardInterrupt', 'keyboard interrupt (mouseclick)');
18 var result = this.get_output_cell(0);
19 this.test.assertEquals(result.ename, 'KeyboardInterrupt', 'keyboard interrupt (mouseclick)');
23 20 });
24 21
25 22 // run cell 0 again, now interrupting using keyboard shortcut
26 23 this.thenEvaluate(function () {
27 24 cell.clear_output();
28 25 cell.execute();
29 26 });
30 27
31 28 // interrupt using Ctrl-M I keyboard shortcut
32 29 this.thenEvaluate( function() {
33 30 IPython.utils.press_ghetto(IPython.utils.keycodes.I)
34 31 });
35 32
36 33 this.wait_for_output(0);
37 34
38 35 this.then(function () {
39 var result = this.evaluate(function () {
40 var cell = IPython.notebook.get_cell(0);
41 return cell.output_area.outputs[0].ename;
42 });
43 this.test.assertEquals(result, 'KeyboardInterrupt', 'keyboard interrupt (shortcut)');
36 var result = this.get_output_cell(0);
37 this.test.assertEquals(result.ename, 'KeyboardInterrupt', 'keyboard interrupt (shortcut)');
44 38 });
45 39 });
@@ -1,60 +1,49
1 1 //
2 2 // Test code cell execution.
3 3 //
4 4 casper.notebook_test(function () {
5 5 this.evaluate(function () {
6 6 var cell = IPython.notebook.get_cell(0);
7 7 cell.set_text('a=10; print(a)');
8 8 cell.execute();
9 9 });
10 10
11 11 this.wait_for_output(0);
12 12
13 // refactor this into just a get_output(0)
13 14 this.then(function () {
14 var result = this.evaluate(function () {
15 var cell = IPython.notebook.get_cell(0);
16 var output = cell.output_area.outputs[0].text;
17 return output;
18 })
19 this.test.assertEquals(result, '10\n', 'cell execute (using js)')
15 var result = this.get_output_cell(0);
16 this.test.assertEquals(result.text, '10\n', 'cell execute (using js)')
20 17 });
21 18
22 19
23 20 // do it again with the keyboard shortcut
24 21 this.thenEvaluate(function () {
25 22 var cell = IPython.notebook.get_cell(0);
26 23 cell.set_text('a=11; print(a)');
27 24 cell.clear_output()
28 25 IPython.utils.press_ctrl_enter();
29 26 });
30 27
31 28 this.wait_for_output(0);
32 29
33 30 this.then(function () {
34 var result = this.evaluate(function () {
35 var cell = IPython.notebook.get_cell(0);
36 var output = cell.output_area.outputs[0].text;
37 return output;
38 })
39 this.test.assertEquals(result, '11\n', 'cell execute (using ctrl-enter)')
31 var result = this.get_output_cell(0);
32 this.test.assertEquals(result.text, '11\n', 'cell execute (using ctrl-enter)')
40 33 });
41 34
42 35 // do it again with the keyboard shortcut
43 36 this.thenEvaluate(function () {
44 37 var cell = IPython.notebook.get_cell(0);
45 38 cell.set_text('a=12; print(a)');
46 39 cell.clear_output()
47 40 IPython.utils.press_shift_enter();
48 41 });
49 42
50 43 this.wait_for_output(0);
51 44
52 45 this.then(function () {
53 var result = this.evaluate(function () {
54 var cell = IPython.notebook.get_cell(0);
55 var output = cell.output_area.outputs[0].text;
56 return output;
57 })
58 this.test.assertEquals(result, '12\n', 'cell execute (using shift-enter)')
46 var result = this.get_output_cell(0);
47 this.test.assertEquals(result.text, '12\n', 'cell execute (using shift-enter)')
59 48 });
60 49 });
@@ -1,102 +1,112
1 1 //
2 2 // Utility functions for the HTML notebook's CasperJS tests.
3 3 //
4 4
5 5 // Get the URL of a notebook server on which to run tests.
6 6 casper.get_notebook_server = function () {
7 7 port = casper.cli.get("port")
8 8 port = (typeof port === 'undefined') ? '8888' : port;
9 9 return 'http://127.0.0.1:' + port
10 10 };
11 11
12 12 // Create and open a new notebook.
13 13 casper.open_new_notebook = function () {
14 14 var baseUrl = this.get_notebook_server();
15 15 this.start(baseUrl);
16 16 this.thenClick('button#new_notebook');
17 17 this.waitForPopup('');
18 18
19 19 this.withPopup('', function () {this.waitForSelector('.CodeMirror-code');});
20 20 this.then(function () {
21 21 // XXX: Kind of odd, the next line works for one test, but not when
22 22 // running multiple tests back-to-back, so we will just point the main
23 23 // casper browser to the same URL as the popup we just grabbed.
24 24
25 25 //this.page = this.popups[0];
26 26 this.open(this.popups[0].url);
27 27 });
28 28
29 29 // initially, the cells aren't created, so wait for them to appear
30 30 this.waitForSelector('.CodeMirror-code');
31 31 // and make sure the kernel has started
32 32 this.waitFor(function kernel_ready() {
33 33 return this.evaluate(function kernel_ready() {
34 34 return IPython.notebook.kernel.running;
35 35 });
36 36 });
37 37 };
38 38
39 39 // Shut down the current notebook's kernel.
40 40 casper.shutdown_current_kernel = function () {
41 41 this.thenEvaluate(function() {
42 42 IPython.notebook.kernel.kill();
43 43 });
44 44 };
45 45
46 46 // Delete created notebook.
47 47 casper.delete_current_notebook = function () {
48 48 this.thenEvaluate(function() {
49 49 var nbData = $('body').data();
50 50 var url = nbData.baseProjectUrl + 'notebooks/' + nbData.notebookId;
51 51 $.ajax(url, {
52 52 type: 'DELETE',
53 53 });
54 54 });
55 55 };
56 56
57 57 // wait for output in a given cell
58 58 casper.wait_for_output = function (cell_num) {
59 59 this.then(function() {
60 60 this.waitFor(function (c) {
61 61 return this.evaluate(function get_output(c) {
62 62 var cell = IPython.notebook.get_cell(c);
63 63 return cell.output_area.outputs.length != 0;
64 64 },
65 65 // pass parameter from the test suite js to the browser code js
66 66 {c : cell_num});
67 67 });
68 68 });
69 69 };
70 70
71 // return the output of a given cell
72 casper.get_output_cell = function (cell_num) {
73 var result = casper.evaluate(function (c) {
74 var cell = IPython.notebook.get_cell(c);
75 return cell.output_area.outputs[0];
76 },
77 {c : cell_num});
78 return result;
79 };
80
71 81 // Wrap a notebook test to reduce boilerplate.
72 82 casper.notebook_test = function(test) {
73 83 this.open_new_notebook();
74 84 this.then(test);
75 85 //XXX: we get sporadic error messages when shutting down some of the tests.
76 86 // Since the entire server will go down at the end of running the test
77 87 // suite, it's ok for now to not try to shut anything down.
78 88 this.shutdown_current_kernel();
79 89
80 90 //XXX: the implementation of delete_current_notebook is currently broken
81 91 // it's not a big deal, since the notebook directory will be deleted on
82 92 // cleanup, but we should add tests for deleting the notebook separately
83 93 //this.delete_current_notebook();
84 94
85 95 // Run the browser automation.
86 96 this.run(function() {
87 97 this.test.done();
88 98 });
89 99 };
90 100
91 101 casper.options.waitTimeout=5000
92 102 casper.on('waitFor.timeout', function onWaitForTimeout(timeout) {
93 103 this.echo("Timeout for " + casper.get_notebook_server());
94 104 this.echo("Is the notebook server running?");
95 105 });
96 106
97 107 // Pass `console.log` calls from page JS to casper.
98 108 casper.printLog = function () {
99 109 this.on('remote.message', function(msg) {
100 110 this.echo('Remote message caught: ' + msg);
101 111 });
102 112 };
General Comments 0
You need to be logged in to leave comments. Login now