|
1 | | -import asyncio |
2 | | -import contextlib |
3 | | -import time |
4 | | - |
5 | 1 | import flet.testing as ftt |
6 | 2 |
|
7 | 3 |
|
8 | | -async def _find_text_when_ready(tester, text: str, timeout: float = 60.0): |
9 | | - """ |
10 | | - Pump-and-retry (up to `timeout` seconds) until a control with `text` appears. |
11 | | -
|
12 | | - On a device the app runs embedded Python over dart_bridge; its cold start |
13 | | - (interpreter init + `import flet` + running `main()`) can take tens of |
14 | | - seconds on a slow CI emulator, so the first python-driven frame may land |
15 | | - well after the device driver's fixed warmup. `pump_and_settle` only settles |
16 | | - Flutter frames — it can't know a python -> dart round-trip is still in |
17 | | - flight — so poll rather than assert on the first frame. |
18 | | - """ |
19 | | - deadline = time.monotonic() + timeout |
20 | | - while True: |
21 | | - finder = await tester.find_by_text(text) |
22 | | - if finder.count >= 1 or time.monotonic() >= deadline: |
23 | | - return finder |
24 | | - await asyncio.sleep(0.25) |
25 | | - with contextlib.suppress(TimeoutError): |
26 | | - await tester.pump_and_settle() |
27 | | - |
28 | | - |
29 | 4 | async def test_counter(flet_app: ftt.FletTestApp): |
30 | 5 | tester = flet_app.tester |
31 | 6 |
|
32 | | - # Initial state (wait for the app's first render). |
33 | | - assert (await _find_text_when_ready(tester, "0")).count == 1 |
| 7 | + await tester.pump_and_settle() |
| 8 | + |
| 9 | + # Initial state |
| 10 | + assert (await tester.find_by_text("0")).count == 1 |
34 | 11 |
|
35 | 12 | # Increment once |
36 | 13 | await tester.tap(await tester.find_by_key("increment")) |
37 | | - assert (await _find_text_when_ready(tester, "1")).count == 1 |
| 14 | + await tester.pump_and_settle() |
| 15 | + assert (await tester.find_by_text("1")).count == 1 |
38 | 16 |
|
39 | 17 | # Decrement twice -> -1 |
40 | 18 | await tester.tap(await tester.find_by_key("decrement")) |
41 | 19 | await tester.tap(await tester.find_by_key("decrement")) |
42 | | - assert (await _find_text_when_ready(tester, "-1")).count == 1 |
| 20 | + await tester.pump_and_settle() |
| 21 | + assert (await tester.find_by_text("-1")).count == 1 |
0 commit comments