Skip to content

Commit

Permalink
Migrate st_session_state to playwright (streamlit#8918)
Browse files Browse the repository at this point in the history
## Describe your changes
- title
## GitHub Issue Link (if applicable)

## Testing Plan

- Explanation of why no additional tests are needed
- Unit Tests (JS and/or Python)
- E2E Tests
- Any manual testing needed?

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
  • Loading branch information
willhuang1997 authored Jun 20, 2024
1 parent c034943 commit 8c31db8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 84 deletions.
83 changes: 0 additions & 83 deletions e2e/specs/st_session_state.spec.js

This file was deleted.

27 changes: 27 additions & 0 deletions e2e_playwright/st_session_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,30 @@ def on_checkbox_change(changed_checkbox_number):
st.checkbox(
label="Checkbox2", key="checkbox2", on_change=on_checkbox_change, args=(2,)
)

if "initialized" not in st.session_state:
st.session_state["item_counter"] = 0
st.session_state.attr_counter = 0

st.session_state.initialized = True

if st.button("inc_item_counter"):
st.session_state["item_counter"] += 1

if st.button("inc_attr_counter"):
st.session_state.attr_counter += 1

if st.button("del_item_counter"):
del st.session_state["item_counter"]

if st.button("del_attr_counter"):
del st.session_state.attr_counter

if "item_counter" in st.session_state:
st.write(f"item_counter: {st.session_state['item_counter']}")

if "attr_counter" in st.session_state:
st.write(f"attr_counter: {st.session_state.attr_counter}")

st.write(f"len(st.session_state): {len(st.session_state)}")
st.write(st.session_state)
51 changes: 50 additions & 1 deletion e2e_playwright/st_session_state_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from playwright.sync_api import Page, expect

from e2e_playwright.shared.app_utils import click_checkbox, get_checkbox
from e2e_playwright.shared.app_utils import click_button, click_checkbox, get_checkbox


def test_checking_checkbox_unchecks_other(app: Page):
Expand All @@ -29,3 +29,52 @@ def test_checking_checkbox_unchecks_other(app: Page):

expect(first_checkbox.locator("input")).to_have_attribute("aria-checked", "false")
expect(second_checkbox.locator("input")).to_have_attribute("aria-checked", "true")


def test_has_correct_starting_values(app: Page):
expect(app.get_by_text("item_counter: 0")).to_have_count(1)
expect(app.get_by_text("attr_counter: 0")).to_have_count(1)
expect(app.get_by_text("len(st.session_state): 5")).to_have_count(1)
expect(app.get_by_test_id("stJson")).to_be_visible()


def test_can_do_CRUD_for_session_state_items(app: Page):
expect(app.get_by_text("item_counter: 0")).to_have_count(1)
expect(app.get_by_text("attr_counter: 0")).to_have_count(1)

click_button(app, "inc_item_counter")

expect(app.get_by_text("item_counter: 1")).to_have_count(1)
expect(app.get_by_text("attr_counter: 0")).to_have_count(1)

click_button(app, "inc_item_counter")

expect(app.get_by_text("item_counter: 2")).to_have_count(1)
expect(app.get_by_text("attr_counter: 0")).to_have_count(1)

click_button(app, "del_item_counter")

expect(app.get_by_text("item_counter: 2")).to_have_count(0)
expect(app.get_by_text("attr_counter: 0")).to_have_count(1)
expect(app.get_by_text("len(st.session_state): 4")).to_have_count(1)


def test_can_do_CRUD_for_session_state_attributes(app: Page):
expect(app.get_by_text("item_counter: 0")).to_have_count(1)
expect(app.get_by_text("attr_counter: 0")).to_have_count(1)

click_button(app, "inc_attr_counter")

expect(app.get_by_text("item_counter: 0")).to_have_count(1)
expect(app.get_by_text("attr_counter: 1")).to_have_count(1)

click_button(app, "inc_attr_counter")

expect(app.get_by_text("item_counter: 0")).to_have_count(1)
expect(app.get_by_text("attr_counter: 2")).to_have_count(1)

click_button(app, "del_attr_counter")

expect(app.get_by_text("item_counter: 0")).to_have_count(1)
expect(app.get_by_text("attr_counter: 2")).to_have_count(0)
expect(app.get_by_text("len(st.session_state): 4")).to_have_count(1)

0 comments on commit 8c31db8

Please sign in to comment.