Skip to content

Commit

Permalink
Rewrote wait_for: it was flaky and redundant.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodja committed Jan 31, 2023
1 parent 04b77c2 commit a562d47
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
10 changes: 0 additions & 10 deletions tests/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,6 @@ def get_attributes(self, tag: str, attribute: str) -> List[str]:
def wait(self, t: float) -> None:
time.sleep(t)

def wait_for(self, text: str, *, timeout: float = 1.0) -> None:
deadline = time.time() + timeout
while time.time() < deadline:
try:
self.find(text)
return
except Exception:
self.wait(0.1)
raise TimeoutError()

def shot(self, name: str) -> None:
os.makedirs(self.SCREENSHOT_DIR, exist_ok=True)
filename = f'{self.SCREENSHOT_DIR}/{name}.png'
Expand Down
25 changes: 15 additions & 10 deletions tests/test_auto_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ async def add_b():
ui.timer(1.1, add_b, once=True)

screen.open('/')
screen.wait_for('A')
screen.wait_for('B')
screen.should_contain('A')
screen.should_contain('B')
cA = screen.selenium.find_element(By.ID, cardA.id)
cA.find_element(By.XPATH, './/*[contains(text(), "A")]')
cB = screen.selenium.find_element(By.ID, cardB.id)
Expand All @@ -65,11 +65,14 @@ async def page(client: Client):
screen.should_contain('before connected')
screen.should_contain('after connected')
screen.should_not_contain('one')
screen.wait_for('one')
screen.wait(0.5)
screen.should_contain('one')
screen.should_not_contain('two')
screen.wait_for('two')
screen.wait(0.5)
screen.should_contain('two')
screen.should_not_contain('three')
screen.wait_for('three')
screen.wait(0.5)
screen.should_contain('three')


def test_autoupdate_on_async_event_handler(screen: Screen):
Expand All @@ -85,7 +88,7 @@ async def open():
screen.click('Dialog')
screen.should_contain('This should be visible')
screen.should_not_contain('New text after 1 second')
screen.wait_for('New text after 1 second')
screen.should_contain('New text after 1 second')


def test_autoupdate_on_async_timer_callback(screen: Screen):
Expand All @@ -99,9 +102,11 @@ async def update():
screen.open('/')
screen.should_contain('0')
screen.should_not_contain('1')
screen.wait_for('1', timeout=3.0)
screen.wait(2.0)
screen.should_contain('1')
screen.should_not_contain('2')
screen.wait_for('2', timeout=3.0)
screen.wait(2.0)
screen.should_contain('2')


def test_adding_elements_from_different_tasks(screen: Screen):
Expand All @@ -121,8 +126,8 @@ async def add_label2() -> None:
screen.open('/')
background_tasks.create(add_label1())
background_tasks.create(add_label2())
screen.wait_for('1')
screen.wait_for('2')
screen.should_contain('1')
screen.should_contain('2')
c1 = screen.selenium.find_element(By.ID, card1.id)
c1.find_element(By.XPATH, './/*[contains(text(), "1")]')
c2 = screen.selenium.find_element(By.ID, card2.id)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ async def update():

screen.open('/')
screen.click('update')
screen.wait_for('1')
screen.should_contain('1')
screen.should_not_contain('2')
screen.wait_for('2')
screen.should_contain('2')


def test_event_modifiers(screen: Screen):
Expand Down

0 comments on commit a562d47

Please sign in to comment.