Skip to content

Commit 10679cb

Browse files
committed
fix test_html
1 parent 6477e66 commit 10679cb

File tree

6 files changed

+81
-57
lines changed

6 files changed

+81
-57
lines changed

re.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def test_(.*?)\(.*driver.*\):
2+
async def test_$1(display: DisplayFixture):
3+
4+
display\((.*?)\)
5+
page = await display.show($1)
6+
7+
driver\.find_element\("id", "(.*?)"\)
8+
await display.wait_for_selector("#$1")
9+
10+
driver_wait\.until\(lambda .*: (\w*)\.get_attribute\("(.*?)"\) == (.*)\)
11+
assert (await $1.evaluate("node => node['$2']")) == $3
12+
await poll($1.evaluate, "node => node['$2']").until_equals($3)

src/idom/testing.py

+23-15
Original file line numberDiff line numberDiff line change
@@ -82,33 +82,41 @@ def __init__(
8282

8383
async def until(
8484
condition: Callable[[_R], bool], timeout: float = _DEFAULT_TIMEOUT
85-
) -> _R:
85+
) -> None:
8686
started_at = time.time()
87-
while not condition(await function(*args, **kwargs)):
88-
if (time.time() - started_at) > timeout:
89-
raise TimeoutError()
87+
while True:
88+
result = await function(*args, **kwargs)
89+
if condition(result):
90+
break
91+
elif (time.time() - started_at) > timeout:
92+
raise TimeoutError(
93+
f"Condition not met within {timeout} "
94+
f"seconds - last value was {result!r}"
95+
)
9096

9197
else:
9298

9399
def until(
94100
condition: Callable[[_R], bool] | Any, timeout: float = _DEFAULT_TIMEOUT
95-
) -> _R:
101+
) -> None:
96102
started_at = time.time()
97-
while not condition(function(*args, **kwargs)):
98-
if (time.time() - started_at) > timeout:
99-
raise TimeoutError()
103+
while True:
104+
result = function(*args, **kwargs)
105+
if condition(result):
106+
break
107+
elif (time.time() - started_at) > timeout:
108+
raise TimeoutError(
109+
f"Condition not met within {timeout} "
110+
f"seconds - last value was {result!r}"
111+
)
100112

101113
self.until: Callable[[Callable[[_R], bool]], Any] = until
102114
"""Check that the coroutines result meets a condition within the timeout"""
103115

104-
def eq(self, right: Any, timeout: float = _DEFAULT_TIMEOUT) -> Any:
116+
def until_equals(self, right: Any, timeout: float = _DEFAULT_TIMEOUT) -> Any:
105117
"""Wait until the result is equal to the given value"""
106118
return self.until(lambda left: left == right, timeout)
107119

108-
def ne(self, right: Any, timeout: float = _DEFAULT_TIMEOUT) -> Any:
109-
"""Wait until the result is not equal to the given value"""
110-
return self.until(lambda left: left != right, timeout)
111-
112120

113121
class DisplayFixture:
114122
"""A fixture for running web-based tests using ``playwright``"""
@@ -126,7 +134,7 @@ def __init__(
126134
if isinstance(driver, Page):
127135
self._page = driver
128136
else:
129-
self._browser = browser
137+
self._browser = driver
130138
self._next_view_id = 0
131139

132140
async def show(
@@ -139,7 +147,7 @@ async def show(
139147
self.server.mount(lambda: html.div({"id": view_id}, component()))
140148

141149
await self._page.goto(self.server.url(query=query))
142-
await self._page.wait_for_selector(f"#{view_id}")
150+
await self._page.wait_for_selector(f"#{view_id}", state="attached")
143151

144152
return self._page
145153

tests/conftest.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ async def display(server, page):
2525
yield display
2626

2727

28-
@pytest.fixture(scope="session", params=list(all_implementations()))
29-
async def server(request):
30-
async with ServerFixture(implementation=request.param) as server:
28+
@pytest.fixture(scope="session")
29+
async def server():
30+
async with ServerFixture() as server:
3131
yield server
3232

3333

tests/test_html.py

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import pytest
22

33
from idom import component, config, html, use_state
4+
from idom.testing import DisplayFixture, poll
45
from idom.utils import Ref
56

67

7-
def use_toggle():
8-
state, set_state = use_state(True)
8+
def use_toggle(initial=True):
9+
state, set_state = use_state(initial)
910
return state, lambda: set_state(not state)
1011

1112

@@ -14,20 +15,15 @@ def use_counter(initial_value):
1415
return state, lambda: set_state(state + 1)
1516

1617

17-
def test_script_mount_unmount(driver, driver_wait, display):
18+
async def test_script_mount_unmount(display: DisplayFixture):
1819
toggle_is_mounted = Ref()
1920

2021
@component
2122
def Root():
2223
is_mounted, toggle_is_mounted.current = use_toggle()
23-
if is_mounted:
24-
el = HasScript()
25-
else:
26-
el = html.div()
27-
2824
return html.div(
2925
html.div({"id": "mount-state", "data-value": False}),
30-
el,
26+
HasScript() if is_mounted else html.div(),
3127
)
3228

3329
@component
@@ -43,22 +39,23 @@ def HasScript():
4339
}"""
4440
)
4541

46-
display(Root)
42+
page = await display.show(Root)
4743

48-
mount_state = driver.find_element("id", "mount-state")
44+
mount_state = await page.wait_for_selector("#mount-state", state="attached")
45+
poll_mount_state = poll(mount_state.get_attribute, "data-value")
4946

50-
driver_wait.until(lambda d: mount_state.get_attribute("data-value") == "true")
47+
await poll_mount_state.until_equals("true")
5148

5249
toggle_is_mounted.current()
5350

54-
driver_wait.until(lambda d: mount_state.get_attribute("data-value") == "false")
51+
await poll_mount_state.until_equals("false")
5552

5653
toggle_is_mounted.current()
5754

58-
driver_wait.until(lambda d: mount_state.get_attribute("data-value") == "true")
55+
await poll_mount_state.until_equals("true")
5956

6057

61-
def test_script_re_run_on_content_change(driver, driver_wait, display):
58+
async def test_script_re_run_on_content_change(display: DisplayFixture):
6259
incr_count = Ref()
6360

6461
@component
@@ -77,26 +74,29 @@ def HasScript():
7774
),
7875
)
7976

80-
display(HasScript)
77+
page = await display.show(HasScript)
8178

82-
mount_count = driver.find_element("id", "mount-count")
83-
unmount_count = driver.find_element("id", "unmount-count")
79+
mount_count = await page.wait_for_selector("#mount-count", state="attached")
80+
poll_mount_count = poll(mount_count.get_attribute, "data-value")
8481

85-
driver_wait.until(lambda d: mount_count.get_attribute("data-value") == "1")
86-
driver_wait.until(lambda d: unmount_count.get_attribute("data-value") == "0")
82+
unmount_count = await page.wait_for_selector("#unmount-count", state="attached")
83+
poll_unmount_count = poll(unmount_count.get_attribute, "data-value")
84+
85+
await poll_mount_count.until_equals("1")
86+
await poll_unmount_count.until_equals("0")
8787

8888
incr_count.current()
8989

90-
driver_wait.until(lambda d: mount_count.get_attribute("data-value") == "2")
91-
driver_wait.until(lambda d: unmount_count.get_attribute("data-value") == "1")
90+
await poll_mount_count.until_equals("2")
91+
await poll_unmount_count.until_equals("1")
9292

9393
incr_count.current()
9494

95-
driver_wait.until(lambda d: mount_count.get_attribute("data-value") == "3")
96-
driver_wait.until(lambda d: unmount_count.get_attribute("data-value") == "2")
95+
await poll_mount_count.until_equals("3")
96+
await poll_unmount_count.until_equals("2")
9797

9898

99-
def test_script_from_src(driver, driver_wait, display):
99+
async def test_script_from_src(display: DisplayFixture):
100100
incr_src_id = Ref()
101101
file_name_template = "__some_js_script_{src_id}__.js"
102102

@@ -114,7 +114,7 @@ def HasScript():
114114
),
115115
)
116116

117-
display(HasScript)
117+
page = await display.show(HasScript)
118118

119119
for i in range(1, 4):
120120
script_file = config.IDOM_WEB_MODULES_DIR.current / file_name_template.format(
@@ -129,9 +129,9 @@ def HasScript():
129129

130130
incr_src_id.current()
131131

132-
run_count = driver.find_element("id", "run-count")
133-
134-
driver_wait.until(lambda d: (run_count.get_attribute("data-value") == "1"))
132+
run_count = await page.wait_for_selector("#run-count", state="attached")
133+
poll_run_count = poll(run_count.get_attribute, "data-value")
134+
await poll_run_count.until_equals("1")
135135

136136

137137
def test_script_may_only_have_one_child():

tests/test_widgets.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ def SomeComponent():
127127

128128
poll_value = poll(lambda: value.current)
129129

130-
poll_value.eq("hello")
130+
poll_value.until_equals("hello")
131131

132132
await input_2.focus()
133133
await input_2.type(" world", delay=20)
134134

135-
poll_value.eq("hello world")
135+
poll_value.until_equals("hello world")
136136

137137

138138
async def test_use_linked_inputs_on_change_with_cast(display: DisplayFixture):
@@ -154,12 +154,12 @@ def SomeComponent():
154154

155155
poll_value = poll(lambda: value.current)
156156

157-
poll_value.eq(1)
157+
poll_value.until_equals(1)
158158

159159
await input_2.focus()
160160
await input_2.type("2")
161161

162-
poll_value.eq(12)
162+
poll_value.until_equals(12)
163163

164164

165165
async def test_use_linked_inputs_ignore_empty(display: DisplayFixture):
@@ -183,13 +183,13 @@ def SomeComponent():
183183

184184
poll_value = poll(lambda: value.current)
185185

186-
poll_value.eq("1")
186+
poll_value.until_equals("1")
187187

188188
await input_2.focus()
189189
await input_2.press("Backspace")
190190

191-
poll_value.eq("1")
191+
poll_value.until_equals("1")
192192

193193
await input_1.type("2")
194194

195-
poll_value.eq("2")
195+
poll_value.until_equals("2")

tests/tooling/loop.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import asyncio
2+
from asyncio import wait_for
23
from contextlib import contextmanager
34
from typing import Iterator
45

56

7+
TIMEOUT = 3
8+
9+
610
@contextmanager
711
def open_event_loop() -> Iterator[asyncio.AbstractEventLoop]:
812
loop = asyncio.new_event_loop()
@@ -13,8 +17,8 @@ def open_event_loop() -> Iterator[asyncio.AbstractEventLoop]:
1317
finally:
1418
try:
1519
_cancel_all_tasks(loop)
16-
loop.run_until_complete(loop.shutdown_asyncgens())
17-
loop.run_until_complete(loop.shutdown_default_executor())
20+
loop.run_until_complete(wait_for(loop.shutdown_asyncgens(), TIMEOUT))
21+
loop.run_until_complete(wait_for(loop.shutdown_default_executor(), TIMEOUT))
1822
finally:
1923
asyncio.set_event_loop(None)
2024
loop.close()
@@ -29,7 +33,7 @@ def _cancel_all_tasks(loop: asyncio.AbstractEventLoop) -> None:
2933
task.cancel()
3034

3135
loop.run_until_complete(
32-
asyncio.gather(*to_cancel, loop=loop, return_exceptions=True)
36+
wait_for(asyncio.gather(*to_cancel, loop=loop, return_exceptions=True), TIMEOUT)
3337
)
3438

3539
for task in to_cancel:

0 commit comments

Comments
 (0)