Skip to content

Commit

Permalink
Migrate static file serving e2e test to playwright (streamlit#8811)
Browse files Browse the repository at this point in the history
## Describe your changes

This PR migrates the static file serving e2e test to playwright.

---

**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 3, 2024
1 parent eca9170 commit 1e1e5c7
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 95 deletions.
Binary file not shown.
57 changes: 0 additions & 57 deletions e2e/specs/staticfiles_app.spec.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,16 @@
# limitations under the License.

import streamlit as st
from streamlit import runtime

st.header("Main Page with static files")

if not st.get_option("server.enableStaticServing"):
st.error(
"""
**ERROR**. This test needs to be run with `--server.enableStaticServing`, like
this:
"**ERROR**. This test needs to be run with `--server.enableStaticServing`."
)

```
streamlit run
e2e/scripts/staticfiles_apps/streamlit_static_app.py
--server.enableStaticServing=true
```
st.markdown(
"""
)
Images served via static serving:
elif runtime.exists():
"""Static files serving works only when runtime exists"""
st.markdown(
"![Streamlit](http://localhost:8501/app/static/streamlit-mark-color.png)"
)
![Streamlit](./app/static/streamlit-logo.png)
"""
)
47 changes: 47 additions & 0 deletions e2e_playwright/config_static_serving_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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 get_markdown


def test_should_serve_existing_asset(app: Page, app_port: int):
"""Test that the static serving feature serves an existing asset."""
response = app.request.get(
f"http://localhost:{app_port}/app/static/streamlit-logo.png"
)
expect(response).to_be_ok()
# Assert is safe here since we don't need to wait for something here:
assert response.status == 200


def test_should_return_error_on_non_existing_asset(app: Page, app_port: int):
"""Test that the static serving feature returns error code for non-existing asset."""
response = app.request.get(
f"http://localhost:{app_port}/app/static/notexisting.jpeg"
)
expect(response).not_to_be_ok()
# Assert is safe here since we don't need to wait for something here:
assert response.status == 404


def test_static_served_image_embedded_in_markdown(app: Page):
"""Test that an image served via the static serving can be embedded into markdown."""
markdown_element = get_markdown(app, "Images served via static serving:")
image_element = markdown_element.locator("img")

expect(image_element).to_be_visible()
# Check that the image gets loaded correctly
app.expect_response("**/streamlit-logo.png")
2 changes: 2 additions & 0 deletions e2e_playwright/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ def app_server(
"false",
"--server.fileWatcherType",
"none",
"--server.enableStaticServing",
"true",
],
cwd=".",
)
Expand Down
24 changes: 24 additions & 0 deletions e2e_playwright/shared/app_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,30 @@ def get_expander(locator: Locator, label: str | Pattern[str]) -> Locator:
return element


def get_markdown(locator: Locator, text_inside_markdown: str | Pattern[str]) -> Locator:
"""Get a markdown element with the given text inside.
Parameters
----------
locator : Locator
The locator to search for the expander.
text_inside_markdown : str or Pattern[str]
Some text to use to identify the markdown element. The text should be contained
in the markdown content.
Returns
-------
Locator
The expander content.
"""
if isinstance(text_inside_markdown, str):
text_inside_markdown = re.compile(text_inside_markdown)

markdown_element = locator.get_by_test_id("stMarkdownContainer").filter(
has_text=text_inside_markdown
)
expect(markdown_element).to_be_visible()
return markdown_element


def expect_prefixed_markdown(
locator: Locator,
expected_prefix: str,
Expand Down
Binary file added e2e_playwright/static/streamlit-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
21 changes: 0 additions & 21 deletions scripts/run_e2e_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,27 +412,6 @@ def run_e2e_tests(
show_output=verbose,
)

elif basename(spec_path) == "staticfiles_app.spec.js":
test_name, _ = splitext(basename(spec_path))
test_name, _ = splitext(test_name)
test_path = join(
ctx.tests_dir,
"scripts",
"staticfiles_apps",
"streamlit_static_app.py",
)
if os.path.exists(test_path):
run_test(
ctx,
str(spec_path),
[
"streamlit",
"run",
"--server.enableStaticServing=true",
test_path,
],
show_output=verbose,
)
elif basename(spec_path) == "hostframe.spec.js":
test_name, _ = splitext(basename(spec_path))
test_name, _ = splitext(test_name)
Expand Down

0 comments on commit 1e1e5c7

Please sign in to comment.