From 989dfa193d1198acf30f7770ad7ad986b0e265a1 Mon Sep 17 00:00:00 2001 From: Falko Schindler Date: Fri, 2 Aug 2024 11:47:53 +0200 Subject: [PATCH] remove current_user and activate()/deactivate() --- examples/chat_app/test_chat_app.py | 1 - nicegui/testing/user.py | 60 +++++------------------------- 2 files changed, 9 insertions(+), 52 deletions(-) diff --git a/examples/chat_app/test_chat_app.py b/examples/chat_app/test_chat_app.py index 7a0f21fff..2297e3d87 100644 --- a/examples/chat_app/test_chat_app.py +++ b/examples/chat_app/test_chat_app.py @@ -36,6 +36,5 @@ async def test_sending_messages(create_user: Callable[[], User]) -> None: userB.find(ui.input).type('Hello from screen B!').trigger('keydown.enter') await userB.should_see('message') - userA.activate() await userA.should_see('Hello from screen A!') await userA.should_see('Hello from screen B!') diff --git a/nicegui/testing/user.py b/nicegui/testing/user.py index 9f4f6d5dd..1ff17a649 100644 --- a/nicegui/testing/user.py +++ b/nicegui/testing/user.py @@ -7,11 +7,10 @@ import httpx import socketio -from typing_extensions import Self from nicegui import Client, ElementFilter, ui from nicegui.element import Element -from nicegui.nicegui import Slot, _on_handshake +from nicegui.nicegui import _on_handshake from .user_interaction import UserInteraction from .user_navigate import UserNavigate @@ -23,7 +22,6 @@ class User: - current_user: Optional[User] = None def __init__(self, client: httpx.AsyncClient) -> None: self.http_client = client @@ -49,23 +47,6 @@ async def open(self, path: str, *, clear_forward_history: bool = True) -> None: self.back_history.append(path) if clear_forward_history: self.forward_history.clear() - self.activate() - - def activate(self) -> Self: - """Activate the user for interaction.""" - if self.current_user: - self.current_user.deactivate() - self.current_user = self - assert self.client - ui.navigate = self.navigate - self.client.__enter__() # pylint: disable=unnecessary-dunder-call - return self - - def deactivate(self, *_) -> None: - """Deactivate the user.""" - assert self.client - self.client.__exit__() - self.current_user = None @overload async def should_see(self, @@ -93,7 +74,14 @@ async def should_see(self, content: Union[str, list[str], None] = None, retries: int = 3, ) -> None: - """Assert that the page contains an input with the given value.""" + """Assert that the page contains an element fulfilling certain filter rules. + + Note that there is no scrolling in the user simulation -- the entire page is always *visible*. + Due to asynchronous execution, sometimes the expected elements only appear after a short delay. + + By default `should_see` makes three attempts to find the element before failing. + This can be adjusted with the `retries` parameter. + """ assert self.client for _ in range(retries): with self.client: @@ -217,33 +205,3 @@ def _build_error_message(self, return f'element of type {kind.__name__} with {marker=} and {content=} on the page:\n{self.current_layout}' else: return f'element with {marker=} and {content=} on the page:\n{self.current_layout}' - - -original_get_slot_stack = Slot.get_stack -original_prune_slot_stack = Slot.prune_stack - - -def get_stack(_=None) -> List[Slot]: - """Return the slot stack of the current client.""" - if User.current_user is None: - return original_get_slot_stack() - cls = Slot - client_id = id(User.current_user) - if client_id not in cls.stacks: - cls.stacks[client_id] = [] - return cls.stacks[client_id] - - -def prune_stack(cls) -> None: - """Remove the current slot stack if it is empty.""" - if User.current_user is None: - original_prune_slot_stack() - return - cls = Slot - client_id = id(User.current_user) - if not cls.stacks[client_id]: - del cls.stacks[client_id] - - -Slot.get_stack = get_stack # type: ignore -Slot.prune_stack = prune_stack # type: ignore