From 789d50e0e41bf7c0df686129089c3f45eb9fc6bd Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Wed, 21 Feb 2024 20:12:31 -0800 Subject: [PATCH] test_input: replace sleep with `_poll_for` (#2677) Reduce test flakiness and improve runtime in optimal case --- integration/test_input.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/integration/test_input.py b/integration/test_input.py index dc1e7475a..fed8a29c0 100644 --- a/integration/test_input.py +++ b/integration/test_input.py @@ -1,5 +1,4 @@ """Integration tests for text input and related components.""" -import time from typing import Generator import pytest @@ -97,10 +96,11 @@ async def test_fully_controlled_input(fully_controlled_input: AppHarness): # move cursor to home, then to the right and type characters debounce_input.send_keys(Keys.HOME, Keys.ARROW_RIGHT) debounce_input.send_keys("foo") - time.sleep(0.5) + assert AppHarness._poll_for( + lambda: fully_controlled_input.poll_for_value(value_input) == "ifoonitial" + ) assert debounce_input.get_attribute("value") == "ifoonitial" assert await get_state_text() == "ifoonitial" - assert fully_controlled_input.poll_for_value(value_input) == "ifoonitial" assert fully_controlled_input.poll_for_value(plain_value_input) == "ifoonitial" # clear the input on the backend @@ -116,10 +116,12 @@ async def test_fully_controlled_input(fully_controlled_input: AppHarness): # type more characters debounce_input.send_keys("getting testing done") - time.sleep(0.5) + assert AppHarness._poll_for( + lambda: fully_controlled_input.poll_for_value(value_input) + == "getting testing done" + ) assert debounce_input.get_attribute("value") == "getting testing done" assert await get_state_text() == "getting testing done" - assert fully_controlled_input.poll_for_value(value_input) == "getting testing done" assert ( fully_controlled_input.poll_for_value(plain_value_input) == "getting testing done" @@ -127,19 +129,20 @@ async def test_fully_controlled_input(fully_controlled_input: AppHarness): # type into the on_change input on_change_input.send_keys("overwrite the state") - time.sleep(0.5) + assert AppHarness._poll_for( + lambda: fully_controlled_input.poll_for_value(value_input) + == "overwrite the state" + ) assert debounce_input.get_attribute("value") == "overwrite the state" assert on_change_input.get_attribute("value") == "overwrite the state" assert await get_state_text() == "overwrite the state" - assert fully_controlled_input.poll_for_value(value_input) == "overwrite the state" assert ( fully_controlled_input.poll_for_value(plain_value_input) == "overwrite the state" ) clear_button.click() - time.sleep(0.5) - assert on_change_input.get_attribute("value") == "" + assert AppHarness._poll_for(lambda: on_change_input.get_attribute("value") == "") # potential bug: clearing the on_change field doesn't itself trigger on_change # assert backend_state.text == "" # assert debounce_input.get_attribute("value") == ""