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