##// END OF EJS Templates
Merge pull request #4645 from jdfreder/casperjs_utils...
Paul Ivanov -
r13816:7d0bedb0 merge
parent child Browse files
Show More
@@ -1,127 +1,209 b''
1 //
1 //
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.
5 // Get the URL of a notebook server on which to run tests.
6 casper.get_notebook_server = function () {
6 casper.get_notebook_server = function () {
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.
12 // Create and open a new notebook.
13 casper.open_new_notebook = function () {
13 casper.open_new_notebook = function () {
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');
17 this.waitForPopup('');
17 this.waitForPopup('');
18
18
19 this.withPopup('', function () {this.waitForSelector('.CodeMirror-code');});
19 this.withPopup('', function () {this.waitForSelector('.CodeMirror-code');});
20 this.then(function () {
20 this.then(function () {
21 // XXX: Kind of odd, the next line works for one test, but not when
21 // XXX: Kind of odd, the next line works for one test, but not when
22 // running multiple tests back-to-back, so we will just point the main
22 // running multiple tests back-to-back, so we will just point the main
23 // casper browser to the same URL as the popup we just grabbed.
23 // casper browser to the same URL as the popup we just grabbed.
24
24
25 //this.page = this.popups[0];
25 //this.page = this.popups[0];
26 this.open(this.popups[0].url);
26 this.open(this.popups[0].url);
27 });
27 });
28
28
29 // initially, the cells aren't created, so wait for them to appear
29 // initially, the cells aren't created, so wait for them to appear
30 this.waitForSelector('.CodeMirror-code');
30 this.waitForSelector('.CodeMirror-code');
31 // and make sure the kernel has started
31 // and make sure the kernel has started
32 this.waitFor( this.kernel_running );
32 this.waitFor( this.kernel_running );
33 };
33 };
34
34
35 // return whether or not the kernel is running
35 // return whether or not the kernel is running
36 casper.kernel_running = function kernel_running() {
36 casper.kernel_running = function kernel_running() {
37 return this.evaluate(function kernel_running() {
37 return this.evaluate(function kernel_running() {
38 return IPython.notebook.kernel.running;
38 return IPython.notebook.kernel.running;
39 });
39 });
40 };
40 };
41
41
42 // Shut down the current notebook's kernel.
42 // Shut down the current notebook's kernel.
43 casper.shutdown_current_kernel = function () {
43 casper.shutdown_current_kernel = function () {
44 this.thenEvaluate(function() {
44 this.thenEvaluate(function() {
45 IPython.notebook.kernel.kill();
45 IPython.notebook.kernel.kill();
46 });
46 });
47 };
47 };
48
48
49 // Delete created notebook.
49 // Delete created notebook.
50 casper.delete_current_notebook = function () {
50 casper.delete_current_notebook = function () {
51 this.thenEvaluate(function() {
51 this.thenEvaluate(function() {
52 var nbData = $('body').data();
52 var nbData = $('body').data();
53 var url = nbData.baseProjectUrl + 'notebooks/' + nbData.notebookId;
53 var url = nbData.baseProjectUrl + 'notebooks/' + nbData.notebookId;
54 $.ajax(url, {
54 $.ajax(url, {
55 type: 'DELETE',
55 type: 'DELETE',
56 });
56 });
57 });
57 });
58 };
58 };
59
59
60 // wait for output in a given cell
60 // wait for output in a given cell
61 casper.wait_for_output = function (cell_num) {
61 casper.wait_for_output = function (cell_num) {
62 this.then(function() {
62 this.then(function() {
63 this.waitFor(function (c) {
63 this.waitFor(function (c) {
64 return this.evaluate(function get_output(c) {
64 return this.evaluate(function get_output(c) {
65 var cell = IPython.notebook.get_cell(c);
65 var cell = IPython.notebook.get_cell(c);
66 return cell.output_area.outputs.length != 0;
66 return cell.output_area.outputs.length != 0;
67 },
67 },
68 // pass parameter from the test suite js to the browser code js
68 // pass parameter from the test suite js to the browser code js
69 {c : cell_num});
69 {c : cell_num});
70 },
70 },
71 function then() { },
71 function then() { },
72 function timeout() {
72 function timeout() {
73 this.echo("wait_for_output timedout!");
73 this.echo("wait_for_output timedout!");
74 });
74 });
75 });
75 });
76 };
76 };
77
77
78 // return the output of a given cell
78 // return the output of a given cell
79 casper.get_output_cell = function (cell_num) {
79 casper.get_output_cell = function (cell_num) {
80 var result = casper.evaluate(function (c) {
80 var result = casper.evaluate(function (c) {
81 var cell = IPython.notebook.get_cell(c);
81 var cell = IPython.notebook.get_cell(c);
82 return cell.output_area.outputs[0];
82 return cell.output_area.outputs[0];
83 },
83 },
84 {c : cell_num});
84 {c : cell_num});
85 return result;
85 return result;
86 };
86 };
87
87
88 // return the number of cells in the notebook
88 // return the number of cells in the notebook
89 casper.get_cells_length = function () {
89 casper.get_cells_length = function () {
90 var result = casper.evaluate(function () {
90 var result = casper.evaluate(function () {
91 return IPython.notebook.get_cells().length;
91 return IPython.notebook.get_cells().length;
92 })
92 })
93 return result;
93 return result;
94 };
94 };
95
95
96 // Set the text content of a cell.
97 casper.set_cell_text = function(index, text){
98 this.evaluate(function (index, text) {
99 var cell = IPython.notebook.get_cell(index);
100 cell.set_text(text);
101 }, index, text);
102 };
103
104 // Inserts a cell at the bottom of the notebook
105 // Returns the new cell's index.
106 casper.insert_cell_at_bottom = function(cell_type){
107 if (cell_type===undefined) {
108 cell_type = 'code';
109 }
110
111 return this.evaluate(function (cell_type) {
112 var cell = IPython.notebook.insert_cell_at_bottom(cell_type);
113 return IPython.notebook.find_cell_index(cell);
114 }, cell_type);
115 };
116
117 // Insert a cell at the bottom of the notebook and set the cells text.
118 // Returns the new cell's index.
119 casper.append_cell = function(text, cell_type) {
120 var index = this.insert_cell_at_bottom(cell_type);
121 if (text !== undefined) {
122 this.set_cell_text(index, text);
123 }
124 return index;
125 };
126
127 // Asynchronously executes a cell by index.
128 // Returns the cell's index.
129 casper.execute_cell = function(index){
130 var that = this;
131 this.then(function(){
132 that.evaluate(function (index) {
133 var cell = IPython.notebook.get_cell(index);
134 cell.execute();
135 }, index);
136 });
137 return index;
138 };
139
140 // Synchronously executes a cell by index.
141 // Optionally accepts a then_callback parameter. then_callback will get called
142 // when the cell has finished executing.
143 // Returns the cell's index.
144 casper.execute_cell_then = function(index, then_callback) {
145 var return_val = this.execute_cell(index);
146
147 this.wait_for_output(index);
148
149 var that = this;
150 this.then(function(){
151 if (then_callback!==undefined) {
152 then_callback.apply(that, [index]);
153 }
154 });
155
156 return return_val;
157 };
158
159 // Utility function that allows us to easily check if an element exists
160 // within a cell. Uses JQuery selector to look for the element.
161 casper.cell_element_exists = function(index, selector){
162 return casper.evaluate(function (index, selector) {
163 var $cell = IPython.notebook.get_cell(index).element;
164 return $cell.find(selector).length > 0;
165 }, index, selector);
166 };
167
168 // Utility function that allows us to execute a jQuery function on an
169 // element within a cell.
170 casper.cell_element_function = function(index, selector, function_name, function_args){
171 return casper.evaluate(function (index, selector, function_name, function_args) {
172 var $cell = IPython.notebook.get_cell(index).element;
173 var $el = $cell.find(selector);
174 return $el[function_name].apply($el, function_args);
175 }, index, selector, function_name, function_args);
176 };
177
96 // Wrap a notebook test to reduce boilerplate.
178 // Wrap a notebook test to reduce boilerplate.
97 casper.notebook_test = function(test) {
179 casper.notebook_test = function(test) {
98 this.open_new_notebook();
180 this.open_new_notebook();
99 this.then(test);
181 this.then(test);
100 //XXX: we get sporadic error messages when shutting down some of the tests.
182 //XXX: we get sporadic error messages when shutting down some of the tests.
101 // Since the entire server will go down at the end of running the test
183 // Since the entire server will go down at the end of running the test
102 // suite, it's ok for now to not try to shut anything down.
184 // suite, it's ok for now to not try to shut anything down.
103 this.shutdown_current_kernel();
185 this.shutdown_current_kernel();
104
186
105 //XXX: the implementation of delete_current_notebook is currently broken
187 //XXX: the implementation of delete_current_notebook is currently broken
106 // it's not a big deal, since the notebook directory will be deleted on
188 // it's not a big deal, since the notebook directory will be deleted on
107 // cleanup, but we should add tests for deleting the notebook separately
189 // cleanup, but we should add tests for deleting the notebook separately
108 //this.delete_current_notebook();
190 //this.delete_current_notebook();
109
191
110 // Run the browser automation.
192 // Run the browser automation.
111 this.run(function() {
193 this.run(function() {
112 this.test.done();
194 this.test.done();
113 });
195 });
114 };
196 };
115
197
116 casper.options.waitTimeout=10000
198 casper.options.waitTimeout=10000
117 casper.on('waitFor.timeout', function onWaitForTimeout(timeout) {
199 casper.on('waitFor.timeout', function onWaitForTimeout(timeout) {
118 this.echo("Timeout for " + casper.get_notebook_server());
200 this.echo("Timeout for " + casper.get_notebook_server());
119 this.echo("Is the notebook server running?");
201 this.echo("Is the notebook server running?");
120 });
202 });
121
203
122 // Pass `console.log` calls from page JS to casper.
204 // Pass `console.log` calls from page JS to casper.
123 casper.printLog = function () {
205 casper.printLog = function () {
124 this.on('remote.message', function(msg) {
206 this.on('remote.message', function(msg) {
125 this.echo('Remote message caught: ' + msg);
207 this.echo('Remote message caught: ' + msg);
126 });
208 });
127 };
209 };
General Comments 0
You need to be logged in to leave comments. Login now