Skip to content

Commit 5e9d4f4

Browse files
committed
Add mod and non-mod tests for input components
1 parent c3fcac0 commit 5e9d4f4

26 files changed

+643
-25
lines changed

shiny/playwright/controller/_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def set_text(
120120
# TODO-future; Composable set() method
121121
loc.fill("", timeout=timeout) # Reset the value
122122
loc.type(text, delay=delay, timeout=timeout) # Type the value
123+
loc.press("Enter", timeout=timeout)
123124

124125

125126
def _expect_multiple(loc: Locator, multiple: bool, timeout: Timeout = None) -> None:

shiny/playwright/controller/_input_buttons.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(
4747
super().__init__(
4848
page,
4949
id=id,
50-
loc=f"button#{id}.action-button.shiny-bound-input",
50+
loc=f'button[id="{id}"].action-button.shiny-bound-input',
5151
)
5252

5353
def expect_disabled(self, value: bool, *, timeout: Timeout = None):
@@ -75,7 +75,7 @@ class InputBookmarkButton(
7575
def __init__(
7676
self,
7777
page: Page,
78-
id: str,
78+
id: str = "`._bookmark_`",
7979
) -> None:
8080
"""
8181
Initializes the input bookmark button.
@@ -85,7 +85,7 @@ def __init__(
8585
page
8686
The page where the input bookmark button is located.
8787
id
88-
The id of the input bookmark button.
88+
The id of the input bookmark button. Defaults to "._bookmark_".
8989
"""
9090
super().__init__(
9191
page,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from starlette.requests import Request
2+
3+
from shiny import App, Inputs, Outputs, Session, module, ui
4+
from shiny.bookmark import BookmarkState, RestoreState
5+
6+
7+
@module.ui
8+
def mod_ui(label: str):
9+
return ui.div(
10+
ui.input_text("text", f"{label} Text", "Initial Module Text"),
11+
ui.input_numeric("num", f"{label} Numeric", 1),
12+
ui.input_text("excluded", f"{label} Excluded", "Module Excluded"),
13+
)
14+
15+
16+
@module.server
17+
def mod_server(input: Inputs, output: Outputs, session: Session):
18+
session.bookmark.exclude.append("excluded")
19+
20+
@session.bookmark.on_bookmark
21+
async def _(state: BookmarkState):
22+
state.values["module_num"] = input.num()
23+
24+
@session.bookmark.on_restore
25+
def _(state: RestoreState):
26+
if "module_num" in state.values:
27+
ui.update_numeric("num", value=state.values["module_num"])
28+
if "text" in state.input:
29+
print(f"module on_restore: text: {state.input['text']}")
30+
31+
32+
def app_ui(request: Request):
33+
return ui.page_fluid(
34+
mod_ui("mod1", "Module 1"),
35+
mod_ui("mod2", "Module 2"),
36+
ui.input_bookmark_button(),
37+
)
38+
39+
40+
def server(input: Inputs, output: Outputs, session: Session):
41+
mod_server("mod1")
42+
mod_server("mod2")
43+
44+
@session.bookmark.on_bookmarked
45+
async def _(url: str):
46+
await session.bookmark.update_query_string(url)
47+
48+
49+
app = App(app_ui, server, bookmark_store="url")

tests/playwright/ai_generated_apps/bookmark_tests/input_checkbox/app-express.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from shiny.express import app_opts, input, module, render, session, ui
22

3-
app_opts(bookmark_store="url")
3+
app_opts(bookmark_store="url", debug=True)
44

55
with ui.card():
66
ui.card_header("Bookmarking Checkbox Demo")
@@ -30,7 +30,7 @@ def checkbox_text():
3030

3131
checkbox_module("first")
3232

33-
ui.input_bookmark_button(id="bookmark_button", label="Bookmark this page")
33+
ui.input_bookmark_button()
3434

3535

3636
@session.bookmark.on_bookmarked

tests/playwright/ai_generated_apps/bookmark_tests/input_checkbox/test_input_checkbox_core_express_bookmarking.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ def test_checkbox_demo(page: Page, app: ShinyAppProc) -> None:
3333
module_checkbox.expect_checked(True)
3434
module_text.expect_value("Checkbox value: True")
3535

36-
# Test bookmark button
37-
bookmark_button = controller.InputActionButton(page, "bookmark_button")
38-
bookmark_button.click()
36+
# TODO-Karan: Implement bookmark button controller
37+
# bookmark_button = controller.InputBookmarkButton(page)
38+
# bookmark_button.click()
39+
40+
page.get_by_role("button", name="🔗 Bookmark...").click()
3941

4042
# reload the page to test bookmark
4143
page.reload()

tests/playwright/ai_generated_apps/bookmark_tests/input_checkbox_group/app-express.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def checkbox_text():
3838

3939
checkbox_module("first")
4040

41-
ui.input_bookmark_button(id="bookmark_button", label="Bookmark this page")
41+
ui.input_bookmark_button()
4242

4343

4444
@session.bookmark.on_bookmarked
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from playwright.sync_api import Page
2+
3+
from shiny.playwright import controller
4+
from shiny.pytest import create_app_fixture
5+
from shiny.run import ShinyAppProc
6+
7+
app = create_app_fixture(["app-express.py"])
8+
9+
10+
def test_checkbox_group_demo(page: Page, app: ShinyAppProc) -> None:
11+
page.goto(app.url)
12+
13+
# Test basic checkbox group
14+
basic_group = controller.InputCheckboxGroup(page, "basic")
15+
basic_group.expect_label("Basic checkbox group")
16+
basic_group.expect_selected([])
17+
18+
basic_text = controller.OutputText(page, "basic_text")
19+
basic_text.expect_value("Checkbox group values: ()")
20+
21+
# Test module checkbox group
22+
module_group = controller.InputCheckboxGroup(page, "first-module_checkbox")
23+
module_group.expect_label("Module checkbox group")
24+
module_group.expect_selected([])
25+
26+
module_text = controller.OutputText(page, "first-checkbox_text")
27+
module_text.expect_value("Checkbox group values: ()")
28+
29+
# Select values
30+
basic_group.set(["Option 1", "Option 3"])
31+
basic_group.expect_selected(["Option 1", "Option 3"])
32+
basic_text.expect_value("Checkbox group values: ('Option 1', 'Option 3')")
33+
34+
module_group.set(["Choice A", "Choice C"])
35+
module_group.expect_selected(["Choice A", "Choice C"])
36+
module_text.expect_value("Checkbox group values: ('Choice A', 'Choice C')")
37+
38+
# Bookmark the state
39+
page.get_by_role("button", name="🔗 Bookmark...").click()
40+
41+
# Reload the page to test bookmark
42+
page.reload()
43+
44+
# Check if selections are preserved
45+
basic_group.expect_selected(["Option 1", "Option 3"])
46+
basic_text.expect_value("Checkbox group values: ('Option 1', 'Option 3')")
47+
48+
module_group.expect_selected(["Choice A", "Choice C"])
49+
module_text.expect_value("Checkbox group values: ('Choice A', 'Choice C')")

tests/playwright/ai_generated_apps/bookmark_tests/input_dark_mode/app-express.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
ui.h3("Non-Module Section")
1010

1111
# Basic dark mode toggle
12-
ui.input_dark_mode(id="basic", label="Basic dark mode toggle")
12+
ui.input_dark_mode(id="basic", label="Basic dark mode toggle", mode="dark")
1313

1414
@render.text
1515
def basic_text():
@@ -22,15 +22,17 @@ def dark_mode_module(input, output, session):
2222
ui.h3("Dark Mode Module")
2323

2424
# Module dark mode toggle
25-
ui.input_dark_mode(id="module_dark_mode", label="Module dark mode toggle")
25+
ui.input_dark_mode(
26+
id="module_dark_mode", label="Module dark mode toggle", mode="dark"
27+
)
2628

2729
@render.text
2830
def dark_mode_text():
2931
return f"Dark mode value: {input.module_dark_mode()}"
3032

3133
dark_mode_module("first")
3234

33-
ui.input_bookmark_button(id="bookmark_button", label="Bookmark this page")
35+
ui.input_bookmark_button()
3436

3537

3638
@session.bookmark.on_bookmarked
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from playwright.sync_api import Page
2+
3+
from shiny.playwright import controller
4+
from shiny.pytest import create_app_fixture
5+
from shiny.run import ShinyAppProc
6+
7+
app = create_app_fixture(["app-express.py"])
8+
9+
10+
def test_dark_mode_demo(page: Page, app: ShinyAppProc) -> None:
11+
page.goto(app.url)
12+
13+
# Test basic checkbox group
14+
basic_dark_mode = controller.InputDarkMode(page, "basic")
15+
basic_text = controller.OutputText(page, "basic_text")
16+
17+
basic_text.expect_value("Dark mode value: dark")
18+
19+
# Test module dark mode
20+
module_group = controller.InputDarkMode(page, "first-module_dark_mode")
21+
module_text = controller.OutputText(page, "first-dark_mode_text")
22+
23+
module_text.expect_value("Dark mode value: dark")
24+
25+
module_group.click()
26+
basic_text.expect_value("Dark mode value: light")
27+
28+
# Bookmark the state
29+
page.get_by_role("button", name="🔗 Bookmark...").click()
30+
31+
basic_dark_mode.click()
32+
module_text.expect_value("Dark mode value: dark")
33+
34+
# Reload the page to test bookmark
35+
page.reload()
36+
37+
basic_text.expect_value("Dark mode value: light")
38+
module_text.expect_value("Dark mode value: light")

tests/playwright/ai_generated_apps/bookmark_tests/input_date/app-express.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
ui.input_date(
1313
id="basic",
1414
label="Basic date input",
15+
value="2024-01-01",
1516
)
1617

1718
@render.text
@@ -28,6 +29,7 @@ def date_module(input, output, session):
2829
ui.input_date(
2930
id="module_date",
3031
label="Module date input",
32+
value="2024-01-01",
3133
)
3234

3335
@render.text
@@ -36,7 +38,7 @@ def date_text():
3638

3739
date_module("first")
3840

39-
ui.input_bookmark_button(id="bookmark_button", label="Bookmark this page")
41+
ui.input_bookmark_button()
4042

4143

4244
@session.bookmark.on_bookmarked

0 commit comments

Comments
 (0)