convert test_table to use playwright (#4241)

* convert test_table to use playwright

* clean up test
This commit is contained in:
Thomas Brandého 2024-10-25 09:43:24 -07:00 committed by GitHub
parent d85236b9b0
commit 3fba4101e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,10 +3,18 @@
from typing import Generator from typing import Generator
import pytest import pytest
from selenium.webdriver.common.by import By from playwright.sync_api import Page
from reflex.testing import AppHarness from reflex.testing import AppHarness
expected_col_headers = ["Name", "Age", "Location"]
expected_row_headers = ["John", "Jane", "Joe"]
expected_cells_data = [
["30", "New York"],
["31", "San Fransisco"],
["32", "Los Angeles"],
]
def Table(): def Table():
"""App using table component.""" """App using table component."""
@ -17,11 +25,6 @@ def Table():
@app.add_page @app.add_page
def index(): def index():
return rx.center( return rx.center(
rx.input(
id="token",
value=rx.State.router.session.client_token,
is_read_only=True,
),
rx.table.root( rx.table.root(
rx.table.header( rx.table.header(
rx.table.row( rx.table.row(
@ -53,7 +56,7 @@ def Table():
@pytest.fixture() @pytest.fixture()
def table(tmp_path_factory) -> Generator[AppHarness, None, None]: def table_app(tmp_path_factory) -> Generator[AppHarness, None, None]:
"""Start Table app at tmp_path via AppHarness. """Start Table app at tmp_path via AppHarness.
Args: Args:
@ -71,47 +74,27 @@ def table(tmp_path_factory) -> Generator[AppHarness, None, None]:
yield harness yield harness
@pytest.fixture def test_table(page: Page, table_app: AppHarness):
def driver(table: AppHarness):
"""GEt an instance of the browser open to the table app.
Args:
table: harness for Table app
Yields:
WebDriver instance.
"""
driver = table.frontend()
try:
token_input = driver.find_element(By.ID, "token")
assert token_input
# wait for the backend connection to send the token
token = table.poll_for_value(token_input)
assert token is not None
yield driver
finally:
driver.quit()
def test_table(driver, table: AppHarness):
"""Test that a table component is rendered properly. """Test that a table component is rendered properly.
Args: Args:
driver: Selenium WebDriver open to the app table_app: Harness for Table app
table: Harness for Table app page: Playwright page instance
""" """
assert table.app_instance is not None, "app is not running" assert table_app.frontend_url is not None, "frontend url is not available"
thead = driver.find_element(By.TAG_NAME, "thead") page.goto(table_app.frontend_url)
# poll till page is fully loaded. table = page.get_by_role("table")
table.poll_for_content(element=thead)
# check headers # Check column headers
assert thead.find_element(By.TAG_NAME, "tr").text == "Name Age Location" headers = table.get_by_role("columnheader").all_inner_texts()
# check first row value assert headers == expected_col_headers
assert (
driver.find_element(By.TAG_NAME, "tbody") # Check rows headers
.find_elements(By.TAG_NAME, "tr")[0] rows = table.get_by_role("rowheader").all_inner_texts()
.text assert rows == expected_row_headers
== "John 30 New York"
) # Check cells
rows = table.get_by_role("cell").all_inner_texts()
for i, expected_row in enumerate(expected_cells_data):
assert [rows[idx := i * 2], rows[idx + 1]] == expected_row