##// END OF EJS Templates
Remove sleep from the following,...
Jonathan Frederic -
Show More
@@ -1,72 +1,72
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.then(function () {
19 this.then(function () {
20 // Check if the widget manager has been instantiated.
20 // Check if the widget manager has been instantiated.
21 this.test.assert(this.evaluate(function() {
21 this.test.assert(this.evaluate(function() {
22 return IPython.notebook.kernel.widget_manager !== undefined;
22 return IPython.notebook.kernel.widget_manager !== undefined;
23 }), 'Notebook widget manager instantiated');
23 }), 'Notebook widget manager instantiated');
24 });
24 });
25
25
26 throttle_index = this.append_cell(
26 throttle_index = this.append_cell(
27 'import time\n' +
27 'import time\n' +
28 'textbox = widgets.TextWidget()\n' +
28 'textbox = widgets.TextWidget()\n' +
29 'display(textbox)\n' +
29 'display(textbox)\n' +
30 'textbox.add_class("my-throttle-textbox")\n' +
30 'textbox.add_class("my-throttle-textbox")\n' +
31 'def handle_change(name, old, new):\n' +
31 'def handle_change(name, old, new):\n' +
32 ' print(len(new))\n' +
32 ' print(len(new))\n' +
33 ' time.sleep(0.5)\n' +
33 ' time.sleep(0.5)\n' +
34 'textbox.on_trait_change(handle_change, "value")\n' +
34 'textbox.on_trait_change(handle_change, "value")\n' +
35 'print("Success")');
35 'print("Success")');
36 this.execute_cell_then(throttle_index, function(index){
36 this.execute_cell_then(throttle_index, function(index){
37 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
37 this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
38 'Test throttling cell executed with correct output');
38 'Test throttling cell executed with correct output');
39
39
40 this.test.assert(this.cell_element_exists(index,
40 this.test.assert(this.cell_element_exists(index,
41 '.widget-area .widget-subarea'),
41 '.widget-area .widget-subarea'),
42 'Widget subarea exists.');
42 'Widget subarea exists.');
43
43
44 this.test.assert(this.cell_element_exists(index,
44 this.test.assert(this.cell_element_exists(index,
45 '.my-throttle-textbox'), 'Textbox exists.');
45 '.my-throttle-textbox'), 'Textbox exists.');
46
46
47 // Send 20 characters
47 // Send 20 characters
48 this.sendKeys('.my-throttle-textbox', '...................A');
48 this.sendKeys('.my-throttle-textbox', '....................');
49 });
49 });
50
50
51 this.waitFor(function check() {
51 this.waitFor(function check() {
52 var outputs = this.evaluate(function(i) {
52 var outputs = this.evaluate(function(i) {
53 return IPython.notebook.get_cell(i).output_area.outputs;
53 return IPython.notebook.get_cell(i).output_area.outputs;
54 }, {i : throttle_index});
54 }, {i : throttle_index});
55 var output = outputs[outputs.length-1].text;
55 var output = outputs[outputs.length-1].text.trim();
56 return (output[output.length-1] == 'A');
56 return (output == '20');
57
57
58 }, function then() {
58 }, function then() {
59 var outputs = this.evaluate(function(i) {
59 var outputs = this.evaluate(function(i) {
60 return IPython.notebook.get_cell(i).output_area.outputs;
60 return IPython.notebook.get_cell(i).output_area.outputs;
61 }, {i : throttle_index});
61 }, {i : throttle_index});
62
62
63 // Only 4 outputs should have printed, but because of timing, sometimes
63 // Only 4 outputs should have printed, but because of timing, sometimes
64 // 5 outputs will print. All we need to do is verify num outputs <= 5
64 // 5 outputs will print. All we need to do is verify num outputs <= 5
65 // because that is much less than 20.
65 // because that is much less than 20.
66 this.test.assert(outputs.length <= 5, 'Messages throttled.');
66 this.test.assert(outputs.length <= 5, 'Messages throttled.');
67
67
68 // We also need to verify that the last state sent was correct.
68 // We also need to verify that the last state sent was correct.
69 var last_state = outputs[outputs.length-1].text;
69 var last_state = outputs[outputs.length-1].text;
70 this.test.assertEquals(last_state, "20\n", "Last state sent when throttling.");
70 this.test.assertEquals(last_state, "20\n", "Last state sent when throttling.");
71 });
71 });
72 });
72 });
@@ -1,44 +1,43
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.waitFor(function check() {
37 this.wait_for_output(button_index, 1);
38 return (this.get_output_cell(button_index, 1).text == 'Clicked\n');
38
39 }, function then() {
39 this.then(function () {
40 this.test.assert(true, 'Button click event fires.');
40 this.test.assertEquals(this.get_output_cell(button_index, 1).text, 'Clicked\n',
41 }), function timeout() {
41 'Button click event fires.');
42 this.test.assert(false, 'Button click event fires.');
43 });
42 });
44 }); No newline at end of file
43 });
@@ -1,108 +1,100
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
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
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
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,240 +1,261
1 //
1 //
2 // Utility functions for the HTML notebook's CasperJS tests.
2 // Utility functions for the HTML notebook's CasperJS tests.
3 //
3 //
4
4
5 // Get the URL of a notebook server on which to run tests.
5 // Get the URL of a notebook server on which to run tests.
6 casper.get_notebook_server = function () {
6 casper.get_notebook_server = function () {
7 port = casper.cli.get("port")
7 port = casper.cli.get("port")
8 port = (typeof port === 'undefined') ? '8888' : port;
8 port = (typeof port === 'undefined') ? '8888' : port;
9 return 'http://127.0.0.1:' + port
9 return 'http://127.0.0.1:' + port
10 };
10 };
11
11
12 // Create and open a new notebook.
12 // Create and open a new notebook.
13 casper.open_new_notebook = function () {
13 casper.open_new_notebook = function () {
14 var baseUrl = this.get_notebook_server();
14 var baseUrl = this.get_notebook_server();
15 this.start(baseUrl);
15 this.start(baseUrl);
16 this.thenClick('button#new_notebook');
16 this.thenClick('button#new_notebook');
17 this.waitForPopup('');
17 this.waitForPopup('');
18
18
19 this.withPopup('', function () {this.waitForSelector('.CodeMirror-code');});
19 this.withPopup('', function () {this.waitForSelector('.CodeMirror-code');});
20 this.then(function () {
20 this.then(function () {
21 // XXX: Kind of odd, the next line works for one test, but not when
21 // XXX: Kind of odd, the next line works for one test, but not when
22 // running multiple tests back-to-back, so we will just point the main
22 // running multiple tests back-to-back, so we will just point the main
23 // casper browser to the same URL as the popup we just grabbed.
23 // casper browser to the same URL as the popup we just grabbed.
24
24
25 //this.page = this.popups[0];
25 //this.page = this.popups[0];
26 this.open(this.popups[0].url);
26 this.open(this.popups[0].url);
27 });
27 });
28
28
29 // initially, the cells aren't created, so wait for them to appear
29 // initially, the cells aren't created, so wait for them to appear
30 this.waitForSelector('.CodeMirror-code');
30 this.waitForSelector('.CodeMirror-code');
31 // and make sure the kernel has started
31 // and make sure the kernel has started
32 this.waitFor( this.kernel_running );
32 this.waitFor( this.kernel_running );
33 // track the IPython busy/idle state
33 // track the IPython busy/idle state
34 this.thenEvaluate(function () {
34 this.thenEvaluate(function () {
35 $([IPython.events]).on('status_idle.Kernel',function () {
35 $([IPython.events]).on('status_idle.Kernel',function () {
36 IPython._status = 'idle';
36 IPython._status = 'idle';
37 });
37 });
38 $([IPython.events]).on('status_busy.Kernel',function () {
38 $([IPython.events]).on('status_busy.Kernel',function () {
39 IPython._status = 'busy';
39 IPython._status = 'busy';
40 });
40 });
41 });
41 });
42 };
42 };
43
43
44 // return whether or not the kernel is running
44 // return whether or not the kernel is running
45 casper.kernel_running = function kernel_running() {
45 casper.kernel_running = function kernel_running() {
46 return this.evaluate(function kernel_running() {
46 return this.evaluate(function kernel_running() {
47 return IPython.notebook.kernel.running;
47 return IPython.notebook.kernel.running;
48 });
48 });
49 };
49 };
50
50
51 // Shut down the current notebook's kernel.
51 // Shut down the current notebook's kernel.
52 casper.shutdown_current_kernel = function () {
52 casper.shutdown_current_kernel = function () {
53 this.thenEvaluate(function() {
53 this.thenEvaluate(function() {
54 IPython.notebook.kernel.kill();
54 IPython.notebook.kernel.kill();
55 });
55 });
56 };
56 };
57
57
58 // Delete created notebook.
58 // Delete created notebook.
59 casper.delete_current_notebook = function () {
59 casper.delete_current_notebook = function () {
60 this.thenEvaluate(function() {
60 this.thenEvaluate(function() {
61 var nbData = $('body').data();
61 var nbData = $('body').data();
62 var url = nbData.baseProjectUrl + 'notebooks/' + nbData.notebookId;
62 var url = nbData.baseProjectUrl + 'notebooks/' + nbData.notebookId;
63 $.ajax(url, {
63 $.ajax(url, {
64 type: 'DELETE',
64 type: 'DELETE',
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 if (cell_type===undefined) {
159 if (cell_type===undefined) {
139 cell_type = 'code';
160 cell_type = 'code';
140 }
161 }
141
162
142 return this.evaluate(function (cell_type) {
163 return this.evaluate(function (cell_type) {
143 var cell = IPython.notebook.insert_cell_at_bottom(cell_type);
164 var cell = IPython.notebook.insert_cell_at_bottom(cell_type);
144 return IPython.notebook.find_cell_index(cell);
165 return IPython.notebook.find_cell_index(cell);
145 }, cell_type);
166 }, cell_type);
146 };
167 };
147
168
148 // Insert a cell at the bottom of the notebook and set the cells text.
169 // Insert a cell at the bottom of the notebook and set the cells text.
149 // Returns the new cell's index.
170 // Returns the new cell's index.
150 casper.append_cell = function(text, cell_type) {
171 casper.append_cell = function(text, cell_type) {
151 var index = this.insert_cell_at_bottom(cell_type);
172 var index = this.insert_cell_at_bottom(cell_type);
152 if (text !== undefined) {
173 if (text !== undefined) {
153 this.set_cell_text(index, text);
174 this.set_cell_text(index, text);
154 }
175 }
155 return index;
176 return index;
156 };
177 };
157
178
158 // Asynchronously executes a cell by index.
179 // Asynchronously executes a cell by index.
159 // Returns the cell's index.
180 // Returns the cell's index.
160 casper.execute_cell = function(index){
181 casper.execute_cell = function(index){
161 var that = this;
182 var that = this;
162 this.then(function(){
183 this.then(function(){
163 that.evaluate(function (index) {
184 that.evaluate(function (index) {
164 var cell = IPython.notebook.get_cell(index);
185 var cell = IPython.notebook.get_cell(index);
165 cell.execute();
186 cell.execute();
166 }, index);
187 }, index);
167 });
188 });
168 return index;
189 return index;
169 };
190 };
170
191
171 // Synchronously executes a cell by index.
192 // Synchronously executes a cell by index.
172 // Optionally accepts a then_callback parameter. then_callback will get called
193 // Optionally accepts a then_callback parameter. then_callback will get called
173 // when the cell has finished executing.
194 // when the cell has finished executing.
174 // Returns the cell's index.
195 // Returns the cell's index.
175 casper.execute_cell_then = function(index, then_callback) {
196 casper.execute_cell_then = function(index, then_callback) {
176 var return_val = this.execute_cell(index);
197 var return_val = this.execute_cell(index);
177
198
178 this.wait_for_idle();
199 this.wait_for_idle();
179
200
180 var that = this;
201 var that = this;
181 this.then(function(){
202 this.then(function(){
182 if (then_callback!==undefined) {
203 if (then_callback!==undefined) {
183 then_callback.apply(that, [index]);
204 then_callback.apply(that, [index]);
184 }
205 }
185 });
206 });
186
207
187 return return_val;
208 return return_val;
188 };
209 };
189
210
190 // Utility function that allows us to easily check if an element exists
211 // Utility function that allows us to easily check if an element exists
191 // within a cell. Uses JQuery selector to look for the element.
212 // within a cell. Uses JQuery selector to look for the element.
192 casper.cell_element_exists = function(index, selector){
213 casper.cell_element_exists = function(index, selector){
193 return casper.evaluate(function (index, selector) {
214 return casper.evaluate(function (index, selector) {
194 var $cell = IPython.notebook.get_cell(index).element;
215 var $cell = IPython.notebook.get_cell(index).element;
195 return $cell.find(selector).length > 0;
216 return $cell.find(selector).length > 0;
196 }, index, selector);
217 }, index, selector);
197 };
218 };
198
219
199 // Utility function that allows us to execute a jQuery function on an
220 // Utility function that allows us to execute a jQuery function on an
200 // element within a cell.
221 // element within a cell.
201 casper.cell_element_function = function(index, selector, function_name, function_args){
222 casper.cell_element_function = function(index, selector, function_name, function_args){
202 return casper.evaluate(function (index, selector, function_name, function_args) {
223 return casper.evaluate(function (index, selector, function_name, function_args) {
203 var $cell = IPython.notebook.get_cell(index).element;
224 var $cell = IPython.notebook.get_cell(index).element;
204 var $el = $cell.find(selector);
225 var $el = $cell.find(selector);
205 return $el[function_name].apply($el, function_args);
226 return $el[function_name].apply($el, function_args);
206 }, index, selector, function_name, function_args);
227 }, index, selector, function_name, function_args);
207 };
228 };
208
229
209 // Wrap a notebook test to reduce boilerplate.
230 // Wrap a notebook test to reduce boilerplate.
210 casper.notebook_test = function(test) {
231 casper.notebook_test = function(test) {
211 this.open_new_notebook();
232 this.open_new_notebook();
212 this.then(test);
233 this.then(test);
213 //XXX: we get sporadic error messages when shutting down some of the tests.
234 //XXX: we get sporadic error messages when shutting down some of the tests.
214 // Since the entire server will go down at the end of running the test
235 // Since the entire server will go down at the end of running the test
215 // suite, it's ok for now to not try to shut anything down.
236 // suite, it's ok for now to not try to shut anything down.
216 this.shutdown_current_kernel();
237 this.shutdown_current_kernel();
217
238
218 //XXX: the implementation of delete_current_notebook is currently broken
239 //XXX: the implementation of delete_current_notebook is currently broken
219 // it's not a big deal, since the notebook directory will be deleted on
240 // it's not a big deal, since the notebook directory will be deleted on
220 // cleanup, but we should add tests for deleting the notebook separately
241 // cleanup, but we should add tests for deleting the notebook separately
221 //this.delete_current_notebook();
242 //this.delete_current_notebook();
222
243
223 // Run the browser automation.
244 // Run the browser automation.
224 this.run(function() {
245 this.run(function() {
225 this.test.done();
246 this.test.done();
226 });
247 });
227 };
248 };
228
249
229 casper.options.waitTimeout=10000
250 casper.options.waitTimeout=10000
230 casper.on('waitFor.timeout', function onWaitForTimeout(timeout) {
251 casper.on('waitFor.timeout', function onWaitForTimeout(timeout) {
231 this.echo("Timeout for " + casper.get_notebook_server());
252 this.echo("Timeout for " + casper.get_notebook_server());
232 this.echo("Is the notebook server running?");
253 this.echo("Is the notebook server running?");
233 });
254 });
234
255
235 // Pass `console.log` calls from page JS to casper.
256 // Pass `console.log` calls from page JS to casper.
236 casper.printLog = function () {
257 casper.printLog = function () {
237 this.on('remote.message', function(msg) {
258 this.on('remote.message', function(msg) {
238 this.echo('Remote message caught: ' + msg);
259 this.echo('Remote message caught: ' + msg);
239 });
260 });
240 };
261 };
General Comments 0
You need to be logged in to leave comments. Login now