From 3c554a6725a047bb22d7d4d8fe86f7e2f3c49ae0 Mon Sep 17 00:00:00 2001 From: Ken Kinder Date: Sun, 28 Jul 2024 10:43:43 +0100 Subject: [PATCH] Expand tests coverage All 10 basic tutorial pages are now tested in playwright --- examples/tutorial/06_components/components.py | 12 +-- tests/integration/test_examples.py | 96 ++++++++++++++++++- 2 files changed, 98 insertions(+), 10 deletions(-) diff --git a/examples/tutorial/06_components/components.py b/examples/tutorial/06_components/components.py index 5718bea..4959183 100644 --- a/examples/tutorial/06_components/components.py +++ b/examples/tutorial/06_components/components.py @@ -6,7 +6,7 @@ # Using the @t.component decorator registers the component for use elsewhere @t.component() class Card(Component): - props = ["type"] + props = ["type", "button_text"] default_classes = ["card"] def populate(self): @@ -14,7 +14,7 @@ def populate(self): self.insert_slot("card-header") with t.p(): self.insert_slot() # If you don't pass a name, it defaults to the main slot - t.button("Understood", on_click=self.on_button_click) + t.button(self.button_text, on_click=self.on_button_click) def on_button_click(self, event): self.trigger_event("my-custom-event", detail={"type": self.type}) @@ -28,26 +28,26 @@ def initial(self): def populate(self): t.h1("Components are useful") - with t.card(type="success", on_my_custom_event=self.handle_custom_event) as card: + with t.card(type="success", button_text="Okay Then", on_my_custom_event=self.handle_custom_event) as card: with card.slot("card-header"): t("Success!") with card.slot(): t("Your operation worked") - with t.card(type="warning", on_my_custom_event=self.handle_custom_event) as card: + with t.card(type="warning", button_text="Got It", on_my_custom_event=self.handle_custom_event) as card: with card.slot("card-header"): t("Warning!") with card.slot(): t("Your operation may not work") - with t.card(type="error", on_my_custom_event=self.handle_custom_event) as card: + with t.card(type="error", button_text="Understood", on_my_custom_event=self.handle_custom_event) as card: with card.slot("card-header"): t("Failure!") with card.slot(): t("Your operation failed") if self.state["message"]: - t.p(self.state["message"]) + t.p(self.state["message"], id="result") def handle_custom_event(self, event): self.state["message"] = f"Custom event from card with type {event.detail.get('type')}" diff --git a/tests/integration/test_examples.py b/tests/integration/test_examples.py index 525e470..8b77388 100644 --- a/tests/integration/test_examples.py +++ b/tests/integration/test_examples.py @@ -56,7 +56,7 @@ def test_hello_world(http_server, page: Page): expect(page.locator("h1")).to_contain_text("Hello, World!") -def test_hello_name(page: Page) -> None: +def test_hello_name(page: Page): page.goto(f"http://localhost:{PORT}/") page.get_by_role("link", name="Example 2: Hello, Name").click() page.get_by_placeholder("name").click() @@ -65,7 +65,7 @@ def test_hello_name(page: Page) -> None: expect(page.locator("h1")).to_contain_text("Hello, Jack!") -def test_counter(page: Page) -> None: +def test_counter(page: Page): page.goto(f"http://localhost:{PORT}/") page.get_by_role("link", name="Example 3: Counter").click() page.get_by_role("button", name="+").click() @@ -80,7 +80,7 @@ def test_counter(page: Page) -> None: expect(page.locator(".count")).to_contain_text("-1") -def test_refs_problem(page: Page) -> None: +def test_refs_problem(page: Page): def input_is_active(): return page.evaluate("document.querySelector(\"[placeholder='Type a word']\") === document.activeElement") @@ -98,7 +98,7 @@ def input_is_active(): assert not input_is_active() -def test_refs_solution(page: Page) -> None: +def test_refs_solution(page: Page): def input_is_active(): return page.evaluate("document.querySelector(\"[placeholder='Type a word']\") === document.activeElement") @@ -109,3 +109,91 @@ def input_is_active(): assert input_is_active() page.get_by_placeholder("Type a word").fill("foobar") assert input_is_active() + + +def test_watchers(page: Page): + page.goto(f"http://localhost:{PORT}/") + page.get_by_role("link", name="Example 5: Watchers").click() + page.get_by_placeholder("Enter a guess").click() + page.get_by_placeholder("Enter a guess").fill("3") + page.get_by_text("Keep trying...").click() + page.get_by_placeholder("Enter a guess").click() + page.get_by_placeholder("Enter a guess").fill("4") + page.get_by_text("You guessed the number!").click() + + +def test_components(page: Page): + page.goto(f"http://localhost:{PORT}/") + page.get_by_role("link", name="Example 6: Components").click() + + page.get_by_role("button", name="Okay Then").click() + expect(page.locator("#result")).to_contain_text("Custom event from card with type success") + page.get_by_role("button", name="Got It").click() + expect(page.locator("#result")).to_contain_text("Custom event from card with type warning") + page.get_by_role("button", name="Understood").click() + expect(page.locator("#result")).to_contain_text("Custom event from card with type error") + + +def test_routing(page: Page): + page.goto(f"http://localhost:{PORT}/") + page.get_by_role("link", name="Example 7: Routing").click() + + page.get_by_role("heading", name="PuePy Routing Demo: Pet").click() + page.get_by_role("link", name="Scooby-Doo").click() + page.get_by_text("Scooby-Doo").click() + page.get_by_role("link", name="Back to Homepage").click() + page.get_by_role("link", name="Garfield").click() + page.get_by_text("Garfield").click() + page.get_by_role("link", name="Back to Homepage").click() + page.get_by_role("link", name="Snoopy").click() + page.get_by_text("Snoopy").click() + page.get_by_role("link", name="Back to Homepage").click() + page.get_by_role("heading", name="PuePy Routing Demo: Pet").click() + + +def test_pypi_libraries(page: Page): + page.goto(f"http://localhost:{PORT}/") + page.get_by_role("link", name="Example 8: PyPi Libraries").click() + page.get_by_role("textbox").first.click() + page.get_by_role("textbox").first.fill("\n\n

Hello, World!

\n\n\n") + page.get_by_role("button", name="Convert").click() + expect(page.locator("#pp-b")).to_have_value( + "from puepy import Application, Page, t\n\napp = Application()\n\n@app.page()\nclass DefaultPage(Page):\n " + "def populate(self):\n with t.html():\n with t.body():\n with t.h1():\n " + " t('Hello, World!')" + ) + page.get_by_text("Generate full file").click() + page.get_by_role("button", name="Convert").click() + expect(page.locator("#pp-b")).to_have_value( + "with t.html():\n with t.body():\n with t.h1():\n t('Hello, World!')" + ) + + +def test_webcomponents(page: Page): + page.goto(f"http://localhost:{PORT}/") + page.get_by_role("link", name="Example 9: WebComponents").click() + page.get_by_role("button", name="Open Dialog").click() + page.locator("sl-button").filter(has_text="Close").get_by_role("button").click() + + +def test_a_full_app(page: Page): + page.goto(f"http://localhost:{PORT}/") + + page.get_by_role("link", name="Example 10: A full-blown app").click() + page.get_by_label("Username").click() + page.get_by_label("Username").fill("foo") + page.get_by_label("Username").press("Tab") + page.get_by_label("Password").fill("bar") + page.get_by_role("button", name="Login").click() + page.get_by_role("heading", name="Hello, you are authenticated").click() + page.get_by_label("User Settings").click() + page.get_by_role("menuitem", name="Profile").locator("path").nth(3).click() + page.get_by_role("link", name="Dashboard").click() + page.get_by_role("heading", name="Hello, you are authenticated").click() + page.get_by_role("link", name="Charts").click() + page.get_by_role("link", name="Forms").click() + page.get_by_label("Name").click() + page.get_by_label("Name").fill("Hello") + page.get_by_role("button", name="Submit").click() + page.get_by_role("link", name="Dashboard").click() + page.get_by_role("heading", name="Hello, you are authenticated").click()