Skip to content

Commit

Permalink
Migrate redisplayed widgets test to playwright (streamlit#8894)
Browse files Browse the repository at this point in the history
## Describe your changes

Migrates the redisplayed_widgets cypress e2e test to playwright and
merges it with the `widget_state_heavy_usage` test into one test.

---

**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
lukasmasuch authored Jun 13, 2024
1 parent b09b419 commit 51cd0c9
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 124 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/playwright-changed-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ jobs:
- name: Check changed files
id: check_changed_files
env:
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_and_modified_files }}
CHANGED_FILES_COUNT: ${{ steps.changed-files.outputs.all_changed_and_modified_files_count }}
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
CHANGED_FILES_COUNT: ${{ steps.changed-files.outputs.all_changed_files_count }}
run: |
echo "Changed files count: ${CHANGED_FILES_COUNT}"
echo "$CHANGED_FILES"
Expand Down Expand Up @@ -74,7 +74,7 @@ jobs:
run: |
cd e2e_playwright;
rm -rf ./test-results;
pytest ${{ steps.changed-files.outputs.all_changed_and_modified_files }} --browser webkit --browser chromium --browser firefox --video retain-on-failure --screenshot only-on-failure --full-page-screenshot --tracing retain-on-failure --output ./test-results/ -n auto --durations=5 -r aR -v
pytest ${{ steps.changed-files.outputs.all_changed_files }} --browser webkit --browser chromium --browser firefox --video retain-on-failure --screenshot only-on-failure --full-page-screenshot --tracing retain-on-failure --output ./test-results/ -n auto --durations=5 -r aR -v
- name: Upload failed test results
uses: actions/upload-artifact@v4
if: always()
Expand Down
24 changes: 0 additions & 24 deletions e2e/scripts/redisplayed_widgets.py

This file was deleted.

65 changes: 0 additions & 65 deletions e2e/specs/redisplayed_widgets.spec.js

This file was deleted.

8 changes: 5 additions & 3 deletions e2e_playwright/shared/app_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_image(locator: Locator | Page, caption: str | Pattern[str]) -> Locator:
return element


def get_button(locator: Locator, label: str | Pattern[str]) -> Locator:
def get_button(locator: Locator | Page, label: str | Pattern[str]) -> Locator:
"""Get a button widget with the given label.
Parameters
Expand Down Expand Up @@ -232,8 +232,10 @@ def expect_markdown(
expected_markdown : str or Pattern[str]
The expected message to be displayed in the exception.
"""
markdown_el = locator.get_by_test_id("stMarkdownContainer").filter(
has_text=expected_message
markdown_el = (
locator.get_by_test_id("stMarkdown")
.get_by_test_id("stMarkdownContainer")
.filter(has_text=expected_message)
)
expect(markdown_el).to_be_visible()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@

import streamlit as st

# test for https://github.com/streamlit/streamlit/issues/4836
number = st.number_input("test", value=100)
st.header("Widget State - Heavy Usage Test")
# Test for https://github.com/streamlit/streamlit/issues/4836

number = st.number_input("test", value=0, step=1)
st.write(number)

time.sleep(1)
if number:
time.sleep(1)

st.header("Widget State - Redisplayed Widget Test")
# Test for https://github.com/streamlit/streamlit/issues/3512

if st.checkbox("Display widgets"):
if st.checkbox("Show hello"):
st.write("hello")

if st.checkbox("Show goodbye", key="c3"):
st.write("goodbye")
26 changes: 0 additions & 26 deletions e2e_playwright/widget_state_heavy_usage_test.py

This file was deleted.

72 changes: 72 additions & 0 deletions e2e_playwright/widget_state_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from playwright.sync_api import Page, expect

from e2e_playwright.shared.app_utils import click_checkbox, expect_markdown


def test_clicking_a_lot_still_keeps_state(app: Page):
"""Test the the widget state is correctly handled on very fast clicks.
Related to: https://github.com/streamlit/streamlit/issues/4836
"""
number_input_down_button = app.get_by_test_id("stNumberInput").locator(
"button.step-up"
)
for _ in range(40):
number_input_down_button.click()

expect_markdown(app, "40")


def test_doesnt_save_widget_state_on_redisplay(app: Page):
"""Test that widget state is not saved when a widget is redisplayed
after a rerun.
Related to: https://github.com/streamlit/streamlit/issues/3512
"""
click_checkbox(app, "Display widgets")
click_checkbox(app, "Show hello")
expect_markdown(app, "hello")

# Hide widgets:
click_checkbox(app, "Display widgets")

# Show widgets again:
click_checkbox(app, "Display widgets")

# Should not show hello again -> the widget state was not saved
markdown_el = app.get_by_test_id("stMarkdown").filter(has_text="hello")
expect(markdown_el).not_to_be_attached()


def test_doesnt_save_widget_state_on_redisplay_with_keyed_widget(app: Page):
"""Test that widget state is not saved when a keyed widget is redisplayed
after a rerun.
Related to: https://github.com/streamlit/streamlit/issues/3512
"""
click_checkbox(app, "Display widgets")
click_checkbox(app, "Show goodbye")
expect_markdown(app, "goodbye")

# Hide widgets:
click_checkbox(app, "Display widgets")

# Show widgets again:
click_checkbox(app, "Display widgets")

# Should not show goodbye again -> the widget state was not saved
markdown_el = app.get_by_test_id("stMarkdown").filter(has_text="goodbye")
expect(markdown_el).not_to_be_attached()

0 comments on commit 51cd0c9

Please sign in to comment.