From 3457a5d79e9bc3a202a12281eef59d55a55c8f26 2014-02-04 20:14:31 From: Min RK Date: 2014-02-04 20:14:31 Subject: [PATCH] Merge pull request #4952 from jdfreder/widget-tests Widget test inconsistencies add wait_for_widget --- diff --git a/IPython/html/tests/casperjs/test_cases/widgets.js b/IPython/html/tests/casperjs/test_cases/widgets.js index 115e467..9943dd4 100644 --- a/IPython/html/tests/casperjs/test_cases/widgets.js +++ b/IPython/html/tests/casperjs/test_cases/widgets.js @@ -16,8 +16,6 @@ casper.notebook_test(function () { 'print("Success")'); this.execute_cell_then(index); - this.wait(500); // Wait for require.js async callbacks to load dependencies. - this.then(function () { // Check if the widget manager has been instantiated. this.test.assert(this.evaluate(function() { @@ -25,19 +23,19 @@ casper.notebook_test(function () { }), 'Notebook widget manager instantiated'); }); + var textbox = {}; throttle_index = this.append_cell( 'import time\n' + 'textbox = widgets.TextWidget()\n' + - 'display(textbox)\n'+ + 'display(textbox)\n' + 'textbox.add_class("my-throttle-textbox")\n' + 'def handle_change(name, old, new):\n' + ' print(len(new))\n' + ' time.sleep(0.5)\n' + 'textbox.on_trait_change(handle_change, "value")\n' + - 'print("Success")'); + 'print(textbox.model_id)'); this.execute_cell_then(throttle_index, function(index){ - this.test.assertEquals(this.get_output_cell(index).text, 'Success\n', - 'Test throttling cell executed with correct output'); + textbox.model_id = this.get_output_cell(index).text.trim(); this.test.assert(this.cell_element_exists(index, '.widget-area .widget-subarea'), @@ -50,9 +48,9 @@ casper.notebook_test(function () { this.sendKeys('.my-throttle-textbox', '....................'); }); - this.wait(2000); // Wait for clicks to execute in kernel + this.wait_for_widget(textbox); - this.then(function(){ + this.then(function () { var outputs = this.evaluate(function(i) { return IPython.notebook.get_cell(i).output_area.outputs; }, {i : throttle_index}); diff --git a/IPython/html/tests/casperjs/test_cases/widgets_button.js b/IPython/html/tests/casperjs/test_cases/widgets_button.js index aca1b90..75b35e3 100644 --- a/IPython/html/tests/casperjs/test_cases/widgets_button.js +++ b/IPython/html/tests/casperjs/test_cases/widgets_button.js @@ -34,7 +34,7 @@ casper.notebook_test(function () { '.widget-area .widget-subarea button', 'click'); }); - this.wait(500); // Wait for click to execute in kernel and write output + this.wait_for_output(button_index, 1); this.then(function () { this.test.assertEquals(this.get_output_cell(button_index, 1).text, 'Clicked\n', diff --git a/IPython/html/tests/casperjs/test_cases/widgets_float.js b/IPython/html/tests/casperjs/test_cases/widgets_float.js index 3241342..5b6f85d 100644 --- a/IPython/html/tests/casperjs/test_cases/widgets_float.js +++ b/IPython/html/tests/casperjs/test_cases/widgets_float.js @@ -6,72 +6,64 @@ casper.notebook_test(function () { 'print("Success")'); this.execute_cell_then(index); - var float_text_query_2 = '.widget-area .widget-subarea .widget-hbox-single .my-second-float-text'; - - var float_index = this.append_cell( + var float_text = {}; + float_text.query = '.widget-area .widget-subarea .widget-hbox-single .my-second-float-text'; + float_text.index = this.append_cell( 'float_widget = widgets.FloatTextWidget()\n' + 'display(float_widget)\n' + 'float_widget.add_class("my-second-float-text")\n' + - 'print("Success")\n'); - this.execute_cell_then(float_index, function(index){ - - this.test.assertEquals(this.get_output_cell(index).text, 'Success\n', - 'Create float cell executed with correct output.'); - + 'print(float_widget.model_id)\n'); + this.execute_cell_then(float_text.index, function(index){ + float_text.model_id = this.get_output_cell(index).text.trim(); + this.test.assert(this.cell_element_exists(index, '.widget-area .widget-subarea'), 'Widget subarea exists.'); - this.test.assert(this.cell_element_exists(index, float_text_query_2), + this.test.assert(this.cell_element_exists(index, float_text.query), 'Widget float textbox exists.'); - this.cell_element_function(float_index, float_text_query_2, 'val', ['']); - this.sendKeys(float_text_query_2, '1.05'); + this.cell_element_function(float_text.index, float_text.query, 'val', ['']); + this.sendKeys(float_text.query, '1.05'); }); - this.wait(500); // Wait for change to execute in kernel + this.wait_for_widget(float_text); index = this.append_cell('print(float_widget.value)\n'); this.execute_cell_then(index, function(index){ this.test.assertEquals(this.get_output_cell(index).text, '1.05\n', 'Float textbox value set.'); - this.cell_element_function(float_index, float_text_query_2, 'val', ['']); - this.sendKeys(float_text_query_2, '123456789.0'); + this.cell_element_function(float_text.index, float_text.query, 'val', ['']); + this.sendKeys(float_text.query, '123456789.0'); }); - this.wait(500); // Wait for change to execute in kernel - + this.wait_for_widget(float_text); + index = this.append_cell('print(float_widget.value)\n'); this.execute_cell_then(index, function(index){ this.test.assertEquals(this.get_output_cell(index).text, '123456789.0\n', 'Long float textbox value set (probably triggers throttling).'); - this.cell_element_function(float_index, float_text_query_2, 'val', ['']); - this.sendKeys(float_text_query_2, '12hello'); + this.cell_element_function(float_text.index, float_text.query, 'val', ['']); + this.sendKeys(float_text.query, '12hello'); }); - this.wait(500); // Wait for change to execute in kernel + this.wait_for_widget(float_text); index = this.append_cell('print(float_widget.value)\n'); this.execute_cell_then(index, function(index){ this.test.assertEquals(this.get_output_cell(index).text, '12.0\n', 'Invald float textbox value caught and filtered.'); }); - - index = this.append_cell( - 'from IPython.html import widgets\n' + - 'from IPython.display import display, clear_output\n' + - 'print("Success")'); - this.execute_cell_then(index); - var slider_query = '.widget-area .widget-subarea .widget-hbox-single .slider'; var float_text_query = '.widget-area .widget-subarea .widget-hbox-single .widget-numeric-text'; - - var floatrange_index = this.append_cell( + var slider = {}; + slider.query = '.widget-area .widget-subarea .widget-hbox-single .slider'; + slider.index = this.append_cell( 'floatrange = [widgets.BoundedFloatTextWidget(), \n' + ' widgets.FloatSliderWidget()]\n' + '[display(floatrange[i]) for i in range(2)]\n' + 'print("Success")\n'); - this.execute_cell_then(floatrange_index, function(index){ + this.execute_cell_then(slider.index, function(index){ this.test.assertEquals(this.get_output_cell(index).text, 'Success\n', 'Create float range cell executed with correct output.'); @@ -80,7 +72,7 @@ casper.notebook_test(function () { '.widget-area .widget-subarea'), 'Widget subarea exists.'); - this.test.assert(this.cell_element_exists(index, slider_query), + this.test.assert(this.cell_element_exists(index, slider.query), 'Widget slider exists.'); this.test.assert(this.cell_element_exists(index, float_text_query), @@ -98,10 +90,10 @@ casper.notebook_test(function () { this.test.assertEquals(this.get_output_cell(index).text, 'Success\n', 'Float range properties cell executed with correct output.'); - this.test.assert(this.cell_element_exists(floatrange_index, slider_query), + this.test.assert(this.cell_element_exists(slider.index, slider.query), 'Widget slider exists.'); - this.test.assert(this.cell_element_function(floatrange_index, slider_query, + this.test.assert(this.cell_element_function(slider.index, slider.query, 'slider', ['value']) == 25.0, 'Slider set to Python value.'); }); diff --git a/IPython/html/tests/casperjs/test_cases/widgets_int.js b/IPython/html/tests/casperjs/test_cases/widgets_int.js index a2765a0..3639f2d 100644 --- a/IPython/html/tests/casperjs/test_cases/widgets_int.js +++ b/IPython/html/tests/casperjs/test_cases/widgets_int.js @@ -6,50 +6,48 @@ casper.notebook_test(function () { 'print("Success")'); this.execute_cell_then(index); - var int_text_query_2 = '.widget-area .widget-subarea .widget-hbox-single .my-second-int-text'; - - var int_index = this.append_cell( + var int_text = {} + int_text.query = '.widget-area .widget-subarea .widget-hbox-single .my-second-int-text'; + int_text.index = this.append_cell( 'int_widget = widgets.IntTextWidget()\n' + 'display(int_widget)\n' + 'int_widget.add_class("my-second-int-text")\n' + - 'print("Success")\n'); - this.execute_cell_then(int_index, function(index){ - - this.test.assertEquals(this.get_output_cell(index).text, 'Success\n', - 'Create int cell executed with correct output.'); - + 'print(int_widget.model_id)\n'); + this.execute_cell_then(int_text.index, function(index){ + int_text.model_id = this.get_output_cell(index).text.trim(); + this.test.assert(this.cell_element_exists(index, '.widget-area .widget-subarea'), 'Widget subarea exists.'); - this.test.assert(this.cell_element_exists(index, int_text_query_2), + this.test.assert(this.cell_element_exists(index, int_text.query), 'Widget int textbox exists.'); - this.cell_element_function(int_index, int_text_query_2, 'val', ['']); - this.sendKeys(int_text_query_2, '1.05'); + this.cell_element_function(int_text.index, int_text.query, 'val', ['']); + this.sendKeys(int_text.query, '1.05'); }); - this.wait(500); // Wait for change to execute in kernel + this.wait_for_widget(int_text); index = this.append_cell('print(int_widget.value)\n'); this.execute_cell_then(index, function(index){ this.test.assertEquals(this.get_output_cell(index).text, '1\n', 'Int textbox value set.'); - this.cell_element_function(int_index, int_text_query_2, 'val', ['']); - this.sendKeys(int_text_query_2, '123456789'); + this.cell_element_function(int_text.index, int_text.query, 'val', ['']); + this.sendKeys(int_text.query, '123456789'); }); - this.wait(500); // Wait for change to execute in kernel + this.wait_for_widget(int_text); index = this.append_cell('print(int_widget.value)\n'); this.execute_cell_then(index, function(index){ this.test.assertEquals(this.get_output_cell(index).text, '123456789\n', 'Long int textbox value set (probably triggers throttling).'); - this.cell_element_function(int_index, int_text_query_2, 'val', ['']); - this.sendKeys(int_text_query_2, '12hello'); + this.cell_element_function(int_text.index, int_text.query, 'val', ['']); + this.sendKeys(int_text.query, '12hello'); }); - this.wait(500); // Wait for change to execute in kernel + this.wait_for_widget(int_text); index = this.append_cell('print(int_widget.value)\n'); this.execute_cell_then(index, function(index){ @@ -63,19 +61,18 @@ casper.notebook_test(function () { 'print("Success")'); this.execute_cell_then(index); - var slider_query = '.widget-area .widget-subarea .widget-hbox-single .slider'; - var int_text_query = '.widget-area .widget-subarea .widget-hbox-single .my-second-num-test-text'; - var intrange_index = this.append_cell( + var slider_query = '.widget-area .widget-subarea .widget-hbox-single .slider'; + var int_text2 = {}; + int_text2.query = '.widget-area .widget-subarea .widget-hbox-single .my-second-num-test-text'; + int_text2.index = this.append_cell( 'intrange = [widgets.BoundedIntTextWidget(),\n' + ' widgets.IntSliderWidget()]\n' + '[display(intrange[i]) for i in range(2)]\n' + 'intrange[0].add_class("my-second-num-test-text")\n' + - 'print("Success")\n'); - this.execute_cell_then(intrange_index, function(index){ - - this.test.assertEquals(this.get_output_cell(index).text, 'Success\n', - 'Create int range cell executed with correct output.'); + 'print(intrange[0].model_id)\n'); + this.execute_cell_then(int_text2.index, function(index){ + int_text2.model_id = this.get_output_cell(index).text.trim(); this.test.assert(this.cell_element_exists(index, '.widget-area .widget-subarea'), @@ -84,7 +81,7 @@ casper.notebook_test(function () { this.test.assert(this.cell_element_exists(index, slider_query), 'Widget slider exists.'); - this.test.assert(this.cell_element_exists(index, int_text_query), + this.test.assert(this.cell_element_exists(index, int_text2.query), 'Widget int textbox exists.'); }); @@ -99,23 +96,23 @@ casper.notebook_test(function () { this.test.assertEquals(this.get_output_cell(index).text, 'Success\n', 'Int range properties cell executed with correct output.'); - this.test.assert(this.cell_element_exists(intrange_index, slider_query), + this.test.assert(this.cell_element_exists(int_text2.index, slider_query), 'Widget slider exists.'); - this.test.assert(this.cell_element_function(intrange_index, slider_query, + this.test.assert(this.cell_element_function(int_text2.index, slider_query, 'slider', ['value']) == 25, 'Slider set to Python value.'); - this.test.assert(this.cell_element_function(intrange_index, int_text_query, + this.test.assert(this.cell_element_function(int_text2.index, int_text2.query, 'val') == 25, 'Int textbox set to Python value.'); // Clear the int textbox value and then set it to 1 by emulating // keyboard presses. - this.cell_element_function(intrange_index, int_text_query, 'val', ['']); - this.sendKeys(int_text_query, '1'); + this.cell_element_function(int_text2.index, int_text2.query, 'val', ['']); + this.sendKeys(int_text2.query, '1'); }); - this.wait(500); // Wait for change to execute in kernel + this.wait_for_widget(int_text2); index = this.append_cell('print(intrange[0].value)\n'); this.execute_cell_then(index, function(index){ @@ -124,11 +121,11 @@ casper.notebook_test(function () { // Clear the int textbox value and then set it to 120 by emulating // keyboard presses. - this.cell_element_function(intrange_index, int_text_query, 'val', ['']); - this.sendKeys(int_text_query, '120'); + this.cell_element_function(int_text2.index, int_text2.query, 'val', ['']); + this.sendKeys(int_text2.query, '120'); }); - this.wait(500); // Wait for change to execute in kernel + this.wait_for_widget(int_text2); index = this.append_cell('print(intrange[0].value)\n'); this.execute_cell_then(index, function(index){ @@ -137,11 +134,11 @@ casper.notebook_test(function () { // Clear the int textbox value and then set it to 'hello world' by // emulating keyboard presses. 'hello world' should get filtered... - this.cell_element_function(intrange_index, int_text_query, 'val', ['']); - this.sendKeys(int_text_query, 'hello world'); + this.cell_element_function(int_text2.index, int_text2.query, 'val', ['']); + this.sendKeys(int_text2.query, 'hello world'); }); - this.wait(500); // Wait for change to execute in kernel + this.wait_for_widget(int_text2); index = this.append_cell('print(intrange[0].value)\n'); this.execute_cell_then(index, function(index){ diff --git a/IPython/html/tests/casperjs/test_cases/widgets_multicontainer.js b/IPython/html/tests/casperjs/test_cases/widgets_multicontainer.js index 617bfec..2004306 100644 --- a/IPython/html/tests/casperjs/test_cases/widgets_multicontainer.js +++ b/IPython/html/tests/casperjs/test_cases/widgets_multicontainer.js @@ -36,7 +36,7 @@ casper.notebook_test(function () { this.click(multicontainer1_query + ' li:nth-child(2) a'); }); - this.wait(500); // Wait for change to execute in kernel + this.wait_for_idle(); index = this.append_cell( 'print(multicontainer.selected_index)\n' + @@ -98,7 +98,7 @@ casper.notebook_test(function () { this.click(multicontainer2_query + ' .accordion-group:nth-child(2) .accordion-heading .accordion-toggle'); }); - this.wait(500); // Wait for change to execute in kernel + this.wait_for_idle(); index = this.append_cell('print(multicontainer.selected_index)'); // 0 based this.execute_cell_then(index, function(index){ diff --git a/IPython/html/tests/casperjs/test_cases/widgets_selection.js b/IPython/html/tests/casperjs/test_cases/widgets_selection.js index abf68a7..5cbb3e3 100644 --- a/IPython/html/tests/casperjs/test_cases/widgets_selection.js +++ b/IPython/html/tests/casperjs/test_cases/widgets_selection.js @@ -93,33 +93,33 @@ casper.notebook_test(function () { // Verify that selecting a radio button updates all of the others. this.cell_element_function(selection_index, radio_selector + ' .radio:nth-child(2) input', 'click'); }); - this.wait(500); + this.wait_for_idle(); this.then(function () { this.test.assert(verify_selection(this, 1), 'Radio button selection updated view states correctly.'); // Verify that selecting a list option updates all of the others. this.cell_element_function(selection_index, list_selector + ' option:nth-child(3)', 'click'); }); - this.wait(500); + this.wait_for_idle(); this.then(function () { this.test.assert(verify_selection(this, 2), 'List selection updated view states correctly.'); // Verify that selecting a multibutton option updates all of the others. this.cell_element_function(selection_index, multibtn_selector + ' .btn:nth-child(4)', 'click'); }); - this.wait(500); + this.wait_for_idle(); this.then(function () { this.test.assert(verify_selection(this, 3), 'Multibutton selection updated view states correctly.'); // Verify that selecting a combobox option updates all of the others. this.cell_element_function(selection_index, '.widget-area .widget-subarea .widget-hbox-single .btn-group ul.dropdown-menu li:nth-child(3) a', 'click'); }); - this.wait(500); + this.wait_for_idle(); this.then(function () { this.test.assert(verify_selection(this, 2), 'Combobox selection updated view states correctly.'); }); - this.wait(500); // Wait for change to execute in kernel + this.wait_for_idle(); index = this.append_cell( 'for widget in selection:\n' + diff --git a/IPython/html/tests/casperjs/test_cases/widgets_string.js b/IPython/html/tests/casperjs/test_cases/widgets_string.js index a59c0a3..d5ffa07 100644 --- a/IPython/html/tests/casperjs/test_cases/widgets_string.js +++ b/IPython/html/tests/casperjs/test_cases/widgets_string.js @@ -38,13 +38,6 @@ casper.notebook_test(function () { '.widget-area .widget-subarea .widget-hbox-single input[type=text]', 'val')=='xyz', 'Python set textbox value.'); - }); - - this.wait(500); // Wait for change to execute in kernel - - index = this.append_cell('print(string_widget.value)'); - this.execute_cell_then(index, function(index){ - this.test.assert(this.cell_element_exists(string_index, '.widget-area .widget-subarea div span.MathJax_Preview'), 'MathJax parsed the LaTeX successfully.'); diff --git a/IPython/html/tests/casperjs/util.js b/IPython/html/tests/casperjs/util.js index 8d2423b..44db968 100644 --- a/IPython/html/tests/casperjs/util.js +++ b/IPython/html/tests/casperjs/util.js @@ -94,6 +94,27 @@ casper.wait_for_output = function (cell_num, out_num) { }); }; +// 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. +casper.wait_for_widget = function (widget_info) { + 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}); + + if (pending == 0) { + return true; + } else { + return false; + } + }); +} + // return an output of a given cell casper.get_output_cell = function (cell_num, out_num) { out_num = out_num || 0;