Skip to content

Commit d84faae

Browse files
authored
Convert notifications JS test to selenium (#5455)
* Add selenium test helper to wait for JS expressions * Convert notifications JS test to selenium (#3335)
1 parent 4318927 commit d84faae

File tree

3 files changed

+107
-116
lines changed

3 files changed

+107
-116
lines changed

notebook/tests/notebook/notifications.js

Lines changed: 0 additions & 116 deletions
This file was deleted.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""
2+
Test the notification area and widgets
3+
"""
4+
import pytest
5+
6+
from .utils import wait_for_selector, wait_for_script_to_return_true
7+
8+
9+
def get_widget(notebook, name):
10+
return notebook.browser.execute_script(
11+
f"return IPython.notification_area.get_widget('{name}') !== undefined"
12+
)
13+
14+
15+
def widget(notebook, name):
16+
return notebook.browser.execute_script(
17+
f"return IPython.notification_area.widget('{name}') !== undefined"
18+
)
19+
20+
21+
def new_notification_widget(notebook, name):
22+
return notebook.browser.execute_script(
23+
f"return IPython.notification_area.new_notification_widget('{name}') !== undefined"
24+
)
25+
26+
27+
def widget_has_class(notebook, name, class_name):
28+
return notebook.browser.execute_script(
29+
f"""
30+
var w = IPython.notification_area.get_widget('{name}');
31+
return w.element.hasClass('{class_name}');
32+
"""
33+
)
34+
35+
36+
def widget_message(notebook, name):
37+
return notebook.browser.execute_script(
38+
f"""
39+
var w = IPython.notification_area.get_widget('{name}');
40+
return w.get_message();
41+
"""
42+
)
43+
44+
45+
def test_notification(notebook):
46+
# check that existing widgets are there
47+
assert get_widget(notebook, "kernel") and widget(notebook, "kernel"),\
48+
"The kernel notification widget exists"
49+
assert get_widget(notebook, "notebook") and widget(notebook, "notebook"),\
50+
"The notebook notification widget exists"
51+
52+
# try getting a non-existent widget
53+
with pytest.raises(Exception):
54+
get_widget(notebook, "foo")
55+
56+
# try creating a non-existent widget
57+
assert widget(notebook, "bar"), "widget: new widget is created"
58+
59+
# try creating a widget that already exists
60+
with pytest.raises(Exception):
61+
new_notification_widget(notebook, "kernel")
62+
63+
# test creating 'info', 'warning' and 'danger' messages
64+
for level in ("info", "warning", "danger"):
65+
notebook.browser.execute_script(f"""
66+
var tnw = IPython.notification_area.widget('test');
67+
tnw.{level}('test {level}');
68+
""")
69+
wait_for_selector(notebook.browser, "#notification_test", visible=True)
70+
71+
assert widget_has_class(notebook, "test", level), f"{level}: class is correct"
72+
assert widget_message(notebook, "test") == f"test {level}", f"{level}: message is correct"
73+
74+
# test message timeout
75+
notebook.browser.execute_script("""
76+
var tnw = IPython.notification_area.widget('test');
77+
tnw.set_message('test timeout', 1000);
78+
""")
79+
wait_for_selector(notebook.browser, "#notification_test", visible=True)
80+
81+
assert widget_message(notebook, "test") == "test timeout", "timeout: message is correct"
82+
wait_for_selector(notebook.browser, "#notification_test", obscures=True)
83+
assert widget_message(notebook, "test") == "", "timeout: message was cleared"
84+
85+
# test click callback
86+
notebook.browser.execute_script("""
87+
var tnw = IPython.notification_area.widget('test');
88+
tnw._clicked = false;
89+
tnw.set_message('test click', undefined, function () {
90+
tnw._clicked = true;
91+
return true;
92+
});
93+
""")
94+
wait_for_selector(notebook.browser, "#notification_test", visible=True)
95+
96+
assert widget_message(notebook, "test") == "test click", "callback: message is correct"
97+
98+
notebook.browser.find_element_by_id("notification_test").click()
99+
wait_for_script_to_return_true(notebook.browser,
100+
'return IPython.notification_area.widget("test")._clicked;')
101+
wait_for_selector(notebook.browser, "#notification_test", obscures=True)
102+
103+
assert widget_message(notebook, "test") == "", "callback: message was cleared"

notebook/tests/selenium/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def wait_for_xpath(driver, xpath, timeout=10, visible=False, single=False, wait_
3232
return _wait_for(driver, By.XPATH, xpath, timeout, visible, single, obscures)
3333

3434

35+
def wait_for_script_to_return_true(driver, script, timeout=10):
36+
WebDriverWait(driver, timeout).until(lambda d: d.execute_script(script))
37+
38+
3539
def _wait_for(driver, locator_type, locator, timeout=10, visible=False, single=False, obscures=False):
3640
"""Waits `timeout` seconds for the specified condition to be met. Condition is
3741
met if any matching element is found. Returns located element(s) when found.

0 commit comments

Comments
 (0)