##// END OF EJS Templates
Merge pull request #4952 from jdfreder/widget-tests...
Min RK -
r15050:3457a5d7 merge
parent child Browse files
Show More
@@ -1,69 +1,67 b''
1 1 // Test the widget framework.
2 2 casper.notebook_test(function () {
3 3 var index;
4 4
5 5 this.then(function () {
6 6
7 7 // Check if the WidgetManager class is defined.
8 8 this.test.assert(this.evaluate(function() {
9 9 return IPython.WidgetManager !== undefined;
10 10 }), 'WidgetManager class is defined');
11 11 });
12 12
13 13 index = this.append_cell(
14 14 'from IPython.html import widgets\n' +
15 15 'from IPython.display import display, clear_output\n' +
16 16 'print("Success")');
17 17 this.execute_cell_then(index);
18 18
19 this.wait(500); // Wait for require.js async callbacks to load dependencies.
20
21 19 this.then(function () {
22 20 // Check if the widget manager has been instantiated.
23 21 this.test.assert(this.evaluate(function() {
24 22 return IPython.notebook.kernel.widget_manager !== undefined;
25 23 }), 'Notebook widget manager instantiated');
26 24 });
27 25
26 var textbox = {};
28 27 throttle_index = this.append_cell(
29 28 'import time\n' +
30 29 'textbox = widgets.TextWidget()\n' +
31 30 'display(textbox)\n'+
32 31 'textbox.add_class("my-throttle-textbox")\n' +
33 32 'def handle_change(name, old, new):\n' +
34 33 ' print(len(new))\n' +
35 34 ' time.sleep(0.5)\n' +
36 35 'textbox.on_trait_change(handle_change, "value")\n' +
37 'print("Success")');
36 'print(textbox.model_id)');
38 37 this.execute_cell_then(throttle_index, function(index){
39 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
40 'Test throttling cell executed with correct output');
38 textbox.model_id = this.get_output_cell(index).text.trim();
41 39
42 40 this.test.assert(this.cell_element_exists(index,
43 41 '.widget-area .widget-subarea'),
44 42 'Widget subarea exists.');
45 43
46 44 this.test.assert(this.cell_element_exists(index,
47 45 '.my-throttle-textbox'), 'Textbox exists.');
48 46
49 47 // Send 20 characters
50 48 this.sendKeys('.my-throttle-textbox', '....................');
51 49 });
52 50
53 this.wait(2000); // Wait for clicks to execute in kernel
51 this.wait_for_widget(textbox);
54 52
55 53 this.then(function(){
56 54 var outputs = this.evaluate(function(i) {
57 55 return IPython.notebook.get_cell(i).output_area.outputs;
58 56 }, {i : throttle_index});
59 57
60 58 // Only 4 outputs should have printed, but because of timing, sometimes
61 59 // 5 outputs will print. All we need to do is verify num outputs <= 5
62 60 // because that is much less than 20.
63 61 this.test.assert(outputs.length <= 5, 'Messages throttled.');
64 62
65 63 // We also need to verify that the last state sent was correct.
66 64 var last_state = outputs[outputs.length-1].text;
67 65 this.test.assertEquals(last_state, "20\n", "Last state sent when throttling.");
68 66 });
69 67 });
@@ -1,43 +1,43 b''
1 1 // Test widget button class
2 2 casper.notebook_test(function () {
3 3 index = this.append_cell(
4 4 'from IPython.html import widgets\n' +
5 5 'from IPython.display import display, clear_output\n' +
6 6 'print("Success")');
7 7 this.execute_cell_then(index);
8 8
9 9 var button_index = this.append_cell(
10 10 'button = widgets.ButtonWidget(description="Title")\n' +
11 11 'display(button)\n'+
12 12 'print("Success")\n' +
13 13 'def handle_click(sender):\n' +
14 14 ' print("Clicked")\n' +
15 15 'button.on_click(handle_click)');
16 16 this.execute_cell_then(button_index, function(index){
17 17
18 18 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
19 19 'Create button cell executed with correct output.');
20 20
21 21 this.test.assert(this.cell_element_exists(index,
22 22 '.widget-area .widget-subarea'),
23 23 'Widget subarea exists.');
24 24
25 25 this.test.assert(this.cell_element_exists(index,
26 26 '.widget-area .widget-subarea button'),
27 27 'Widget button exists.');
28 28
29 29 this.test.assert(this.cell_element_function(index,
30 30 '.widget-area .widget-subarea button', 'html')=='Title',
31 31 'Set button description.');
32 32
33 33 this.cell_element_function(index,
34 34 '.widget-area .widget-subarea button', 'click');
35 35 });
36 36
37 this.wait(500); // Wait for click to execute in kernel and write output
37 this.wait_for_output(button_index, 1);
38 38
39 39 this.then(function () {
40 40 this.test.assertEquals(this.get_output_cell(button_index, 1).text, 'Clicked\n',
41 41 'Button click event fires.');
42 42 });
43 43 }); No newline at end of file
@@ -1,108 +1,100 b''
1 1 // Test widget float class
2 2 casper.notebook_test(function () {
3 3 index = this.append_cell(
4 4 'from IPython.html import widgets\n' +
5 5 'from IPython.display import display, clear_output\n' +
6 6 'print("Success")');
7 7 this.execute_cell_then(index);
8 8
9 var float_text_query_2 = '.widget-area .widget-subarea .widget-hbox-single .my-second-float-text';
10
11 var float_index = this.append_cell(
9 var float_text = {};
10 float_text.query = '.widget-area .widget-subarea .widget-hbox-single .my-second-float-text';
11 float_text.index = this.append_cell(
12 12 'float_widget = widgets.FloatTextWidget()\n' +
13 13 'display(float_widget)\n' +
14 14 'float_widget.add_class("my-second-float-text")\n' +
15 'print("Success")\n');
16 this.execute_cell_then(float_index, function(index){
17
18 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
19 'Create float cell executed with correct output.');
15 'print(float_widget.model_id)\n');
16 this.execute_cell_then(float_text.index, function(index){
17 float_text.model_id = this.get_output_cell(index).text.trim();
20 18
21 19 this.test.assert(this.cell_element_exists(index,
22 20 '.widget-area .widget-subarea'),
23 21 'Widget subarea exists.');
24 22
25 this.test.assert(this.cell_element_exists(index, float_text_query_2),
23 this.test.assert(this.cell_element_exists(index, float_text.query),
26 24 'Widget float textbox exists.');
27 25
28 this.cell_element_function(float_index, float_text_query_2, 'val', ['']);
29 this.sendKeys(float_text_query_2, '1.05');
26 this.cell_element_function(float_text.index, float_text.query, 'val', ['']);
27 this.sendKeys(float_text.query, '1.05');
30 28 });
31 29
32 this.wait(500); // Wait for change to execute in kernel
30 this.wait_for_widget(float_text);
33 31
34 32 index = this.append_cell('print(float_widget.value)\n');
35 33 this.execute_cell_then(index, function(index){
36 34 this.test.assertEquals(this.get_output_cell(index).text, '1.05\n',
37 35 'Float textbox value set.');
38 this.cell_element_function(float_index, float_text_query_2, 'val', ['']);
39 this.sendKeys(float_text_query_2, '123456789.0');
36 this.cell_element_function(float_text.index, float_text.query, 'val', ['']);
37 this.sendKeys(float_text.query, '123456789.0');
40 38 });
41 39
42 this.wait(500); // Wait for change to execute in kernel
40 this.wait_for_widget(float_text);
43 41
44 42 index = this.append_cell('print(float_widget.value)\n');
45 43 this.execute_cell_then(index, function(index){
46 44 this.test.assertEquals(this.get_output_cell(index).text, '123456789.0\n',
47 45 'Long float textbox value set (probably triggers throttling).');
48 this.cell_element_function(float_index, float_text_query_2, 'val', ['']);
49 this.sendKeys(float_text_query_2, '12hello');
46 this.cell_element_function(float_text.index, float_text.query, 'val', ['']);
47 this.sendKeys(float_text.query, '12hello');
50 48 });
51 49
52 this.wait(500); // Wait for change to execute in kernel
50 this.wait_for_widget(float_text);
53 51
54 52 index = this.append_cell('print(float_widget.value)\n');
55 53 this.execute_cell_then(index, function(index){
56 54 this.test.assertEquals(this.get_output_cell(index).text, '12.0\n',
57 55 'Invald float textbox value caught and filtered.');
58 56 });
59 57
60 index = this.append_cell(
61 'from IPython.html import widgets\n' +
62 'from IPython.display import display, clear_output\n' +
63 'print("Success")');
64 this.execute_cell_then(index);
65
66 var slider_query = '.widget-area .widget-subarea .widget-hbox-single .slider';
67 58 var float_text_query = '.widget-area .widget-subarea .widget-hbox-single .widget-numeric-text';
68
69 var floatrange_index = this.append_cell(
59 var slider = {};
60 slider.query = '.widget-area .widget-subarea .widget-hbox-single .slider';
61 slider.index = this.append_cell(
70 62 'floatrange = [widgets.BoundedFloatTextWidget(), \n' +
71 63 ' widgets.FloatSliderWidget()]\n' +
72 64 '[display(floatrange[i]) for i in range(2)]\n' +
73 65 'print("Success")\n');
74 this.execute_cell_then(floatrange_index, function(index){
66 this.execute_cell_then(slider.index, function(index){
75 67
76 68 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
77 69 'Create float range cell executed with correct output.');
78 70
79 71 this.test.assert(this.cell_element_exists(index,
80 72 '.widget-area .widget-subarea'),
81 73 'Widget subarea exists.');
82 74
83 this.test.assert(this.cell_element_exists(index, slider_query),
75 this.test.assert(this.cell_element_exists(index, slider.query),
84 76 'Widget slider exists.');
85 77
86 78 this.test.assert(this.cell_element_exists(index, float_text_query),
87 79 'Widget float textbox exists.');
88 80 });
89 81
90 82 index = this.append_cell(
91 83 'for widget in floatrange:\n' +
92 84 ' widget.max = 50.0\n' +
93 85 ' widget.min = -50.0\n' +
94 86 ' widget.value = 25.0\n' +
95 87 'print("Success")\n');
96 88 this.execute_cell_then(index, function(index){
97 89
98 90 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
99 91 'Float range properties cell executed with correct output.');
100 92
101 this.test.assert(this.cell_element_exists(floatrange_index, slider_query),
93 this.test.assert(this.cell_element_exists(slider.index, slider.query),
102 94 'Widget slider exists.');
103 95
104 this.test.assert(this.cell_element_function(floatrange_index, slider_query,
96 this.test.assert(this.cell_element_function(slider.index, slider.query,
105 97 'slider', ['value']) == 25.0,
106 98 'Slider set to Python value.');
107 99 });
108 100 }); No newline at end of file
@@ -1,151 +1,148 b''
1 1 // Test widget int class
2 2 casper.notebook_test(function () {
3 3 index = this.append_cell(
4 4 'from IPython.html import widgets\n' +
5 5 'from IPython.display import display, clear_output\n' +
6 6 'print("Success")');
7 7 this.execute_cell_then(index);
8 8
9 var int_text_query_2 = '.widget-area .widget-subarea .widget-hbox-single .my-second-int-text';
10
11 var int_index = this.append_cell(
9 var int_text = {}
10 int_text.query = '.widget-area .widget-subarea .widget-hbox-single .my-second-int-text';
11 int_text.index = this.append_cell(
12 12 'int_widget = widgets.IntTextWidget()\n' +
13 13 'display(int_widget)\n' +
14 14 'int_widget.add_class("my-second-int-text")\n' +
15 'print("Success")\n');
16 this.execute_cell_then(int_index, function(index){
17
18 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
19 'Create int cell executed with correct output.');
15 'print(int_widget.model_id)\n');
16 this.execute_cell_then(int_text.index, function(index){
17 int_text.model_id = this.get_output_cell(index).text.trim();
20 18
21 19 this.test.assert(this.cell_element_exists(index,
22 20 '.widget-area .widget-subarea'),
23 21 'Widget subarea exists.');
24 22
25 this.test.assert(this.cell_element_exists(index, int_text_query_2),
23 this.test.assert(this.cell_element_exists(index, int_text.query),
26 24 'Widget int textbox exists.');
27 25
28 this.cell_element_function(int_index, int_text_query_2, 'val', ['']);
29 this.sendKeys(int_text_query_2, '1.05');
26 this.cell_element_function(int_text.index, int_text.query, 'val', ['']);
27 this.sendKeys(int_text.query, '1.05');
30 28 });
31 29
32 this.wait(500); // Wait for change to execute in kernel
30 this.wait_for_widget(int_text);
33 31
34 32 index = this.append_cell('print(int_widget.value)\n');
35 33 this.execute_cell_then(index, function(index){
36 34 this.test.assertEquals(this.get_output_cell(index).text, '1\n',
37 35 'Int textbox value set.');
38 this.cell_element_function(int_index, int_text_query_2, 'val', ['']);
39 this.sendKeys(int_text_query_2, '123456789');
36 this.cell_element_function(int_text.index, int_text.query, 'val', ['']);
37 this.sendKeys(int_text.query, '123456789');
40 38 });
41 39
42 this.wait(500); // Wait for change to execute in kernel
40 this.wait_for_widget(int_text);
43 41
44 42 index = this.append_cell('print(int_widget.value)\n');
45 43 this.execute_cell_then(index, function(index){
46 44 this.test.assertEquals(this.get_output_cell(index).text, '123456789\n',
47 45 'Long int textbox value set (probably triggers throttling).');
48 this.cell_element_function(int_index, int_text_query_2, 'val', ['']);
49 this.sendKeys(int_text_query_2, '12hello');
46 this.cell_element_function(int_text.index, int_text.query, 'val', ['']);
47 this.sendKeys(int_text.query, '12hello');
50 48 });
51 49
52 this.wait(500); // Wait for change to execute in kernel
50 this.wait_for_widget(int_text);
53 51
54 52 index = this.append_cell('print(int_widget.value)\n');
55 53 this.execute_cell_then(index, function(index){
56 54 this.test.assertEquals(this.get_output_cell(index).text, '12\n',
57 55 'Invald int textbox value caught and filtered.');
58 56 });
59 57
60 58 index = this.append_cell(
61 59 'from IPython.html import widgets\n' +
62 60 'from IPython.display import display, clear_output\n' +
63 61 'print("Success")');
64 62 this.execute_cell_then(index);
65 63
66 var slider_query = '.widget-area .widget-subarea .widget-hbox-single .slider';
67 var int_text_query = '.widget-area .widget-subarea .widget-hbox-single .my-second-num-test-text';
68 64
69 var intrange_index = this.append_cell(
65 var slider_query = '.widget-area .widget-subarea .widget-hbox-single .slider';
66 var int_text2 = {};
67 int_text2.query = '.widget-area .widget-subarea .widget-hbox-single .my-second-num-test-text';
68 int_text2.index = this.append_cell(
70 69 'intrange = [widgets.BoundedIntTextWidget(),\n' +
71 70 ' widgets.IntSliderWidget()]\n' +
72 71 '[display(intrange[i]) for i in range(2)]\n' +
73 72 'intrange[0].add_class("my-second-num-test-text")\n' +
74 'print("Success")\n');
75 this.execute_cell_then(intrange_index, function(index){
76
77 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
78 'Create int range cell executed with correct output.');
73 'print(intrange[0].model_id)\n');
74 this.execute_cell_then(int_text2.index, function(index){
75 int_text2.model_id = this.get_output_cell(index).text.trim();
79 76
80 77 this.test.assert(this.cell_element_exists(index,
81 78 '.widget-area .widget-subarea'),
82 79 'Widget subarea exists.');
83 80
84 81 this.test.assert(this.cell_element_exists(index, slider_query),
85 82 'Widget slider exists.');
86 83
87 this.test.assert(this.cell_element_exists(index, int_text_query),
84 this.test.assert(this.cell_element_exists(index, int_text2.query),
88 85 'Widget int textbox exists.');
89 86 });
90 87
91 88 index = this.append_cell(
92 89 'for widget in intrange:\n' +
93 90 ' widget.max = 50\n' +
94 91 ' widget.min = -50\n' +
95 92 ' widget.value = 25\n' +
96 93 'print("Success")\n');
97 94 this.execute_cell_then(index, function(index){
98 95
99 96 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
100 97 'Int range properties cell executed with correct output.');
101 98
102 this.test.assert(this.cell_element_exists(intrange_index, slider_query),
99 this.test.assert(this.cell_element_exists(int_text2.index, slider_query),
103 100 'Widget slider exists.');
104 101
105 this.test.assert(this.cell_element_function(intrange_index, slider_query,
102 this.test.assert(this.cell_element_function(int_text2.index, slider_query,
106 103 'slider', ['value']) == 25,
107 104 'Slider set to Python value.');
108 105
109 this.test.assert(this.cell_element_function(intrange_index, int_text_query,
106 this.test.assert(this.cell_element_function(int_text2.index, int_text2.query,
110 107 'val') == 25, 'Int textbox set to Python value.');
111 108
112 109 // Clear the int textbox value and then set it to 1 by emulating
113 110 // keyboard presses.
114 this.cell_element_function(intrange_index, int_text_query, 'val', ['']);
115 this.sendKeys(int_text_query, '1');
111 this.cell_element_function(int_text2.index, int_text2.query, 'val', ['']);
112 this.sendKeys(int_text2.query, '1');
116 113 });
117 114
118 this.wait(500); // Wait for change to execute in kernel
115 this.wait_for_widget(int_text2);
119 116
120 117 index = this.append_cell('print(intrange[0].value)\n');
121 118 this.execute_cell_then(index, function(index){
122 119 this.test.assertEquals(this.get_output_cell(index).text, '1\n',
123 120 'Int textbox set int range value');
124 121
125 122 // Clear the int textbox value and then set it to 120 by emulating
126 123 // keyboard presses.
127 this.cell_element_function(intrange_index, int_text_query, 'val', ['']);
128 this.sendKeys(int_text_query, '120');
124 this.cell_element_function(int_text2.index, int_text2.query, 'val', ['']);
125 this.sendKeys(int_text2.query, '120');
129 126 });
130 127
131 this.wait(500); // Wait for change to execute in kernel
128 this.wait_for_widget(int_text2);
132 129
133 130 index = this.append_cell('print(intrange[0].value)\n');
134 131 this.execute_cell_then(index, function(index){
135 132 this.test.assertEquals(this.get_output_cell(index).text, '50\n',
136 133 'Int textbox value bound');
137 134
138 135 // Clear the int textbox value and then set it to 'hello world' by
139 136 // emulating keyboard presses. 'hello world' should get filtered...
140 this.cell_element_function(intrange_index, int_text_query, 'val', ['']);
141 this.sendKeys(int_text_query, 'hello world');
137 this.cell_element_function(int_text2.index, int_text2.query, 'val', ['']);
138 this.sendKeys(int_text2.query, 'hello world');
142 139 });
143 140
144 this.wait(500); // Wait for change to execute in kernel
141 this.wait_for_widget(int_text2);
145 142
146 143 index = this.append_cell('print(intrange[0].value)\n');
147 144 this.execute_cell_then(index, function(index){
148 145 this.test.assertEquals(this.get_output_cell(index).text, '50\n',
149 146 'Invalid int textbox characters ignored');
150 147 });
151 148 }); No newline at end of file
@@ -1,108 +1,108 b''
1 1 // Test multicontainer class
2 2 casper.notebook_test(function () {
3 3 index = this.append_cell(
4 4 'from IPython.html import widgets\n' +
5 5 'from IPython.display import display, clear_output\n' +
6 6 'print("Success")');
7 7 this.execute_cell_then(index);
8 8
9 9 // Test tab view
10 10 var multicontainer1_query = '.widget-area .widget-subarea div div.nav-tabs';
11 11 var multicontainer1_index = this.append_cell(
12 12 'multicontainer = widgets.TabWidget()\n' +
13 13 'page1 = widgets.TextWidget()\n' +
14 14 'page2 = widgets.TextWidget()\n' +
15 15 'page3 = widgets.TextWidget()\n' +
16 16 'multicontainer.children = [page1, page2, page3]\n' +
17 17 'display(multicontainer)\n' +
18 18 'multicontainer.selected_index = 0\n' +
19 19 'print("Success")\n');
20 20 this.execute_cell_then(multicontainer1_index, function(index){
21 21
22 22 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
23 23 'Create multicontainer cell executed with correct output. (1)');
24 24
25 25 this.test.assert(this.cell_element_exists(index,
26 26 '.widget-area .widget-subarea'),
27 27 'Widget subarea exists.');
28 28
29 29 this.test.assert(this.cell_element_exists(index, multicontainer1_query),
30 30 'Widget tab list exists.');
31 31
32 32 this.test.assert(this.cell_element_exists(index, multicontainer1_query),
33 33 'First widget tab list exists.');
34 34
35 35 // JQuery selector is 1 based
36 36 this.click(multicontainer1_query + ' li:nth-child(2) a');
37 37 });
38 38
39 this.wait(500); // Wait for change to execute in kernel
39 this.wait_for_idle();
40 40
41 41 index = this.append_cell(
42 42 'print(multicontainer.selected_index)\n' +
43 43 'multicontainer.selected_index = 2'); // 0 based
44 44 this.execute_cell_then(index, function(index){
45 45 this.test.assertEquals(this.get_output_cell(index).text, '1\n', // 0 based
46 46 'selected_index property updated with tab change.');
47 47
48 48 // JQuery selector is 1 based
49 49 this.test.assert(!this.cell_element_function(multicontainer1_index, multicontainer1_query + ' li:nth-child(1)', 'hasClass', ['active']),
50 50 "Tab 1 is not selected.");
51 51 this.test.assert(!this.cell_element_function(multicontainer1_index, multicontainer1_query + ' li:nth-child(2)', 'hasClass', ['active']),
52 52 "Tab 2 is not selected.");
53 53 this.test.assert(this.cell_element_function(multicontainer1_index, multicontainer1_query + ' li:nth-child(3)', 'hasClass', ['active']),
54 54 "Tab 3 is selected.");
55 55 });
56 56
57 57 index = this.append_cell('multicontainer.set_title(1, "hello")\nprint("Success")'); // 0 based
58 58 this.execute_cell_then(index, function(index){
59 59 this.test.assert(this.cell_element_function(multicontainer1_index, multicontainer1_query +
60 60 ' li:nth-child(2) a', 'html') == 'hello',
61 61 'Tab page title set (after display).');
62 62 });
63 63
64 64 // Test accordion view
65 65 var multicontainer2_query = '.widget-area .widget-subarea .accordion';
66 66 var multicontainer2_index = this.append_cell(
67 67 'multicontainer = widgets.AccordionWidget()\n' +
68 68 'page1 = widgets.TextWidget()\n' +
69 69 'page2 = widgets.TextWidget()\n' +
70 70 'page3 = widgets.TextWidget()\n' +
71 71 'multicontainer.children = [page1, page2, page3]\n' +
72 72 'multicontainer.set_title(2, "good")\n' +
73 73 'display(multicontainer)\n' +
74 74 'multicontainer.selected_index = 0\n' +
75 75 'print("Success")\n');
76 76 this.execute_cell_then(multicontainer2_index, function(index){
77 77
78 78 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
79 79 'Create multicontainer cell executed with correct output. (2)');
80 80
81 81 this.test.assert(this.cell_element_exists(index,
82 82 '.widget-area .widget-subarea'),
83 83 'Widget subarea exists.');
84 84
85 85 this.test.assert(this.cell_element_exists(index, multicontainer2_query),
86 86 'Widget accordion exists.');
87 87
88 88 this.test.assert(this.cell_element_exists(index, multicontainer2_query +
89 89 ' .accordion-group:nth-child(1) .accordion-body'),
90 90 'First accordion page exists.');
91 91
92 92 // JQuery selector is 1 based
93 93 this.test.assert(this.cell_element_function(index, multicontainer2_query +
94 94 ' .accordion-group:nth-child(3) .accordion-heading .accordion-toggle',
95 95 'html')=='good', 'Accordion page title set (before display).');
96 96
97 97 // JQuery selector is 1 based
98 98 this.click(multicontainer2_query + ' .accordion-group:nth-child(2) .accordion-heading .accordion-toggle');
99 99 });
100 100
101 this.wait(500); // Wait for change to execute in kernel
101 this.wait_for_idle();
102 102
103 103 index = this.append_cell('print(multicontainer.selected_index)'); // 0 based
104 104 this.execute_cell_then(index, function(index){
105 105 this.test.assertEquals(this.get_output_cell(index).text, '1\n', // 0 based
106 106 'selected_index property updated with tab change.');
107 107 });
108 108 }); No newline at end of file
@@ -1,133 +1,133 b''
1 1 // Test selection class
2 2 casper.notebook_test(function () {
3 3 index = this.append_cell(
4 4 'from IPython.html import widgets\n' +
5 5 'from IPython.display import display, clear_output\n' +
6 6 'print("Success")');
7 7 this.execute_cell_then(index);
8 8
9 9 var combo_selector = '.widget-area .widget-subarea .widget-hbox-single .btn-group .widget-combo-btn';
10 10 var multibtn_selector = '.widget-area .widget-subarea .widget-hbox-single .btn-group[data-toggle="buttons-radio"]';
11 11 var radio_selector = '.widget-area .widget-subarea .widget-hbox .vbox';
12 12 var list_selector = '.widget-area .widget-subarea .widget-hbox .widget-listbox';
13 13
14 14 var selection_index;
15 15 var selection_values = 'abcd';
16 16 var check_state = function(context, index, state){
17 17 if (0 <= index && index < selection_values.length) {
18 18 var multibtn_state = context.cell_element_function(selection_index, multibtn_selector + ' .btn:nth-child(' + (index + 1) + ')', 'hasClass', ['active']);
19 19 var radio_state = context.cell_element_function(selection_index, radio_selector + ' .radio:nth-child(' + (index + 1) + ') input', 'prop', ['checked']);
20 20 var list_val = context.cell_element_function(selection_index, list_selector, 'val');
21 21 var combo_val = context.cell_element_function(selection_index, combo_selector, 'html');
22 22
23 23 var val = selection_values.charAt(index);
24 24 var list_state = (val == list_val);
25 25 var combo_state = (val == combo_val);
26 26
27 27 return multibtn_state == state &&
28 28 radio_state == state &&
29 29 list_state == state &&
30 30 combo_state == state;
31 31 }
32 32 return true;
33 33 };
34 34
35 35 var verify_selection = function(context, index){
36 36 for (var i = 0; i < selection_values.length; i++) {
37 37 if (!check_state(context, i, i==index)) {
38 38 return false;
39 39 }
40 40 }
41 41 return true;
42 42 };
43 43
44 44 //values=["' + selection_values + '"[i] for i in range(4)]
45 45 selection_index = this.append_cell(
46 46 'values=["' + selection_values + '"[i] for i in range(4)]\n' +
47 47 'selection = [widgets.DropdownWidget(values=values),\n' +
48 48 ' widgets.ToggleButtonsWidget(values=values),\n' +
49 49 ' widgets.RadioButtonsWidget(values=values),\n' +
50 50 ' widgets.SelectWidget(values=values)]\n' +
51 51 '[display(selection[i]) for i in range(4)]\n' +
52 52 'for widget in selection:\n' +
53 53 ' def handle_change(name,old,new):\n' +
54 54 ' for other_widget in selection:\n' +
55 55 ' other_widget.value = new\n' +
56 56 ' widget.on_trait_change(handle_change, "value")\n' +
57 57 'print("Success")\n');
58 58 this.execute_cell_then(selection_index, function(index){
59 59 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
60 60 'Create selection cell executed with correct output.');
61 61
62 62 this.test.assert(this.cell_element_exists(index,
63 63 '.widget-area .widget-subarea'),
64 64 'Widget subarea exists.');
65 65
66 66 this.test.assert(this.cell_element_exists(index, combo_selector),
67 67 'Widget combobox exists.');
68 68
69 69 this.test.assert(this.cell_element_exists(index, multibtn_selector),
70 70 'Widget multibutton exists.');
71 71
72 72 this.test.assert(this.cell_element_exists(index, radio_selector),
73 73 'Widget radio buttons exists.');
74 74
75 75 this.test.assert(this.cell_element_exists(index, list_selector),
76 76 'Widget list exists.');
77 77
78 78 // Verify that no items are selected.
79 79 this.test.assert(verify_selection(this, -1), 'No items selected.');
80 80 });
81 81
82 82 index = this.append_cell(
83 83 'for widget in selection:\n' +
84 84 ' widget.value = "a"\n' +
85 85 'print("Success")\n');
86 86 this.execute_cell_then(index, function(index){
87 87 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
88 88 'Python select item executed with correct output.');
89 89
90 90 // Verify that the first item is selected.
91 91 this.test.assert(verify_selection(this, 0), 'Python selected');
92 92
93 93 // Verify that selecting a radio button updates all of the others.
94 94 this.cell_element_function(selection_index, radio_selector + ' .radio:nth-child(2) input', 'click');
95 95 });
96 this.wait(500);
96 this.wait_for_idle();
97 97 this.then(function () {
98 98 this.test.assert(verify_selection(this, 1), 'Radio button selection updated view states correctly.');
99 99
100 100 // Verify that selecting a list option updates all of the others.
101 101 this.cell_element_function(selection_index, list_selector + ' option:nth-child(3)', 'click');
102 102 });
103 this.wait(500);
103 this.wait_for_idle();
104 104 this.then(function () {
105 105 this.test.assert(verify_selection(this, 2), 'List selection updated view states correctly.');
106 106
107 107 // Verify that selecting a multibutton option updates all of the others.
108 108 this.cell_element_function(selection_index, multibtn_selector + ' .btn:nth-child(4)', 'click');
109 109 });
110 this.wait(500);
110 this.wait_for_idle();
111 111 this.then(function () {
112 112 this.test.assert(verify_selection(this, 3), 'Multibutton selection updated view states correctly.');
113 113
114 114 // Verify that selecting a combobox option updates all of the others.
115 115 this.cell_element_function(selection_index, '.widget-area .widget-subarea .widget-hbox-single .btn-group ul.dropdown-menu li:nth-child(3) a', 'click');
116 116 });
117 this.wait(500);
117 this.wait_for_idle();
118 118 this.then(function () {
119 119 this.test.assert(verify_selection(this, 2), 'Combobox selection updated view states correctly.');
120 120 });
121 121
122 this.wait(500); // Wait for change to execute in kernel
122 this.wait_for_idle();
123 123
124 124 index = this.append_cell(
125 125 'for widget in selection:\n' +
126 126 ' widget.values = list(widget.values) + ["z"]\n' +
127 127 'selection[0].value = "z"');
128 128 this.execute_cell_then(index, function(index){
129 129
130 130 // Verify that selecting a combobox option updates all of the others.
131 131 this.test.assert(verify_selection(this, 4), 'Item added to selection widget.');
132 132 });
133 133 }); No newline at end of file
@@ -1,52 +1,45 b''
1 1 // Test widget string class
2 2 casper.notebook_test(function () {
3 3 index = this.append_cell(
4 4 'from IPython.html import widgets\n' +
5 5 'from IPython.display import display, clear_output\n' +
6 6 'print("Success")');
7 7 this.execute_cell_then(index);
8 8
9 9 var string_index = this.append_cell(
10 10 'string_widget = [widgets.TextWidget(value = "xyz"),\n' +
11 11 ' widgets.TextareaWidget(value = "xyz"),\n' +
12 12 ' widgets.HTMLWidget(value = "xyz"),\n' +
13 13 ' widgets.LatexWidget(value = "$\\\\LaTeX{}$")]\n' +
14 14 '[display(widget) for widget in string_widget]\n'+
15 15 'print("Success")');
16 16 this.execute_cell_then(string_index, function(index){
17 17
18 18 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
19 19 'Create string widget cell executed with correct output.');
20 20
21 21 this.test.assert(this.cell_element_exists(index,
22 22 '.widget-area .widget-subarea'),
23 23 'Widget subarea exists.');
24 24
25 25 this.test.assert(this.cell_element_exists(index,
26 26 '.widget-area .widget-subarea .widget-hbox-single input[type=text]'),
27 27 'Textbox exists.');
28 28
29 29 this.test.assert(this.cell_element_exists(index,
30 30 '.widget-area .widget-subarea .widget-hbox textarea'),
31 31 'Textarea exists.');
32 32
33 33 this.test.assert(this.cell_element_function(index,
34 34 '.widget-area .widget-subarea .widget-hbox textarea', 'val')=='xyz',
35 35 'Python set textarea value.');
36 36
37 37 this.test.assert(this.cell_element_function(index,
38 38 '.widget-area .widget-subarea .widget-hbox-single input[type=text]', 'val')=='xyz',
39 39 'Python set textbox value.');
40 40
41 });
42
43 this.wait(500); // Wait for change to execute in kernel
44
45 index = this.append_cell('print(string_widget.value)');
46 this.execute_cell_then(index, function(index){
47
48 41 this.test.assert(this.cell_element_exists(string_index,
49 42 '.widget-area .widget-subarea div span.MathJax_Preview'),
50 43 'MathJax parsed the LaTeX successfully.');
51 44 });
52 45 }); No newline at end of file
@@ -1,243 +1,264 b''
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 this.open(this.popups[0].url);
22 22 });
23 23
24 24 // Make sure the kernel has started
25 25 this.waitFor( this.kernel_running );
26 26 // track the IPython busy/idle state
27 27 this.thenEvaluate(function () {
28 28 $([IPython.events]).on('status_idle.Kernel',function () {
29 29 IPython._status = 'idle';
30 30 });
31 31 $([IPython.events]).on('status_busy.Kernel',function () {
32 32 IPython._status = 'busy';
33 33 });
34 34 });
35 35 };
36 36
37 37 // Return whether or not the kernel is running.
38 38 casper.kernel_running = function kernel_running() {
39 39 return this.evaluate(function kernel_running() {
40 40 return IPython.notebook.kernel.running;
41 41 });
42 42 };
43 43
44 44 // Shut down the current notebook's kernel.
45 45 casper.shutdown_current_kernel = function () {
46 46 this.thenEvaluate(function() {
47 47 IPython.notebook.kernel.kill();
48 48 });
49 49 // We close the page right after this so we need to give it time to complete.
50 50 this.wait(1000);
51 51 };
52 52
53 53 // Delete created notebook.
54 54 casper.delete_current_notebook = function () {
55 55 // For some unknown reason, this doesn't work?!?
56 56 this.thenEvaluate(function() {
57 57 IPython.notebook.delete();
58 58 });
59 59 };
60 60
61 61 casper.wait_for_busy = function () {
62 62 this.waitFor(function () {
63 63 return this.evaluate(function () {
64 64 return IPython._status == 'busy';
65 65 });
66 66 });
67 67 };
68 68
69 69 casper.wait_for_idle = function () {
70 70 this.waitFor(function () {
71 71 return this.evaluate(function () {
72 72 return IPython._status == 'idle';
73 73 });
74 74 });
75 75 };
76 76
77 77 // wait for the nth output in a given cell
78 78 casper.wait_for_output = function (cell_num, out_num) {
79 79 this.wait_for_idle();
80 80 out_num = out_num || 0;
81 81 this.then(function() {
82 82 this.waitFor(function (c, o) {
83 83 return this.evaluate(function get_output(c, o) {
84 84 var cell = IPython.notebook.get_cell(c);
85 85 return cell.output_area.outputs.length > o;
86 86 },
87 87 // pass parameter from the test suite js to the browser code js
88 88 {c : cell_num, o : out_num});
89 89 });
90 90 },
91 91 function then() { },
92 92 function timeout() {
93 93 this.echo("wait_for_output timed out!");
94 94 });
95 95 };
96 96
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) {
105 this.waitFor(function () {
106 var pending = this.evaluate(function (m) {
107 return IPython.notebook.kernel.widget_manager.get_model(m).pending_msgs;
108 }, {m: widget_info.model_id});
109
110 if (pending == 0) {
111 return true;
112 } else {
113 return false;
114 }
115 });
116 }
117
97 118 // return an output of a given cell
98 119 casper.get_output_cell = function (cell_num, out_num) {
99 120 out_num = out_num || 0;
100 121 var result = casper.evaluate(function (c, o) {
101 122 var cell = IPython.notebook.get_cell(c);
102 123 return cell.output_area.outputs[o];
103 124 },
104 125 {c : cell_num, o : out_num});
105 126 if (!result) {
106 127 var num_outputs = casper.evaluate(function (c) {
107 128 var cell = IPython.notebook.get_cell(c);
108 129 return cell.output_area.outputs.length;
109 130 },
110 131 {c : cell_num});
111 132 this.test.assertTrue(false,
112 133 "Cell " + cell_num + " has no output #" + out_num + " (" + num_outputs + " total)"
113 134 );
114 135 } else {
115 136 return result;
116 137 }
117 138 };
118 139
119 140 // return the number of cells in the notebook
120 141 casper.get_cells_length = function () {
121 142 var result = casper.evaluate(function () {
122 143 return IPython.notebook.get_cells().length;
123 144 })
124 145 return result;
125 146 };
126 147
127 148 // Set the text content of a cell.
128 149 casper.set_cell_text = function(index, text){
129 150 this.evaluate(function (index, text) {
130 151 var cell = IPython.notebook.get_cell(index);
131 152 cell.set_text(text);
132 153 }, index, text);
133 154 };
134 155
135 156 // Inserts a cell at the bottom of the notebook
136 157 // Returns the new cell's index.
137 158 casper.insert_cell_at_bottom = function(cell_type){
138 159 cell_type = cell_type || 'code';
139 160
140 161 return this.evaluate(function (cell_type) {
141 162 var cell = IPython.notebook.insert_cell_at_bottom(cell_type);
142 163 return IPython.notebook.find_cell_index(cell);
143 164 }, cell_type);
144 165 };
145 166
146 167 // Insert a cell at the bottom of the notebook and set the cells text.
147 168 // Returns the new cell's index.
148 169 casper.append_cell = function(text, cell_type) {
149 170 var index = this.insert_cell_at_bottom(cell_type);
150 171 if (text !== undefined) {
151 172 this.set_cell_text(index, text);
152 173 }
153 174 return index;
154 175 };
155 176
156 177 // Asynchronously executes a cell by index.
157 178 // Returns the cell's index.
158 179 casper.execute_cell = function(index){
159 180 var that = this;
160 181 this.then(function(){
161 182 that.evaluate(function (index) {
162 183 var cell = IPython.notebook.get_cell(index);
163 184 cell.execute();
164 185 }, index);
165 186 });
166 187 return index;
167 188 };
168 189
169 190 // Synchronously executes a cell by index.
170 191 // Optionally accepts a then_callback parameter. then_callback will get called
171 192 // when the cell has finished executing.
172 193 // Returns the cell's index.
173 194 casper.execute_cell_then = function(index, then_callback) {
174 195 var return_val = this.execute_cell(index);
175 196
176 197 this.wait_for_idle();
177 198
178 199 var that = this;
179 200 this.then(function(){
180 201 if (then_callback!==undefined) {
181 202 then_callback.apply(that, [index]);
182 203 }
183 204 });
184 205
185 206 return return_val;
186 207 };
187 208
188 209 // Utility function that allows us to easily check if an element exists
189 210 // within a cell. Uses JQuery selector to look for the element.
190 211 casper.cell_element_exists = function(index, selector){
191 212 return casper.evaluate(function (index, selector) {
192 213 var $cell = IPython.notebook.get_cell(index).element;
193 214 return $cell.find(selector).length > 0;
194 215 }, index, selector);
195 216 };
196 217
197 218 // Utility function that allows us to execute a jQuery function on an
198 219 // element within a cell.
199 220 casper.cell_element_function = function(index, selector, function_name, function_args){
200 221 return casper.evaluate(function (index, selector, function_name, function_args) {
201 222 var $cell = IPython.notebook.get_cell(index).element;
202 223 var $el = $cell.find(selector);
203 224 return $el[function_name].apply($el, function_args);
204 225 }, index, selector, function_name, function_args);
205 226 };
206 227
207 228 // Wrap a notebook test to reduce boilerplate.
208 229 casper.notebook_test = function(test) {
209 230 this.open_new_notebook();
210 231 this.then(test);
211 232
212 233 // Kill the kernel and delete the notebook.
213 234 this.shutdown_current_kernel();
214 235 // This is still broken but shouldn't be a problem for now.
215 236 // this.delete_current_notebook();
216 237
217 238 // This is required to clean up the page we just finished with. If we don't call this
218 239 // casperjs will leak file descriptors of all the open WebSockets in that page. We
219 240 // have to set this.page=null so that next time casper.start runs, it will create a
220 241 // new page from scratch.
221 242 this.then(function () {
222 243 this.page.close();
223 244 this.page = null;
224 245 });
225 246
226 247 // Run the browser automation.
227 248 this.run(function() {
228 249 this.test.done();
229 250 });
230 251 };
231 252
232 253 casper.options.waitTimeout=10000
233 254 casper.on('waitFor.timeout', function onWaitForTimeout(timeout) {
234 255 this.echo("Timeout for " + casper.get_notebook_server());
235 256 this.echo("Is the notebook server running?");
236 257 });
237 258
238 259 // Pass `console.log` calls from page JS to casper.
239 260 casper.printLog = function () {
240 261 this.on('remote.message', function(msg) {
241 262 this.echo('Remote message caught: ' + msg);
242 263 });
243 264 };
General Comments 0
You need to be logged in to leave comments. Login now