Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing with Playwright #483

Open
jrycw opened this issue Oct 4, 2024 · 0 comments
Open

Testing with Playwright #483

jrycw opened this issue Oct 4, 2024 · 0 comments

Comments

@jrycw
Copy link
Contributor

jrycw commented Oct 4, 2024

Hello team,

While we have achieved excellent test coverage, I’m curious if it would be beneficial to test the table from the perspective of different browsers.

My idea is to serve an index.html file generated by Great Tables and test it using Playwright, like so:

import http.server
import socketserver
from multiprocessing import Process
from pathlib import Path

import pytest
from great_tables import GT
from great_tables.data import exibble
from playwright.sync_api import expect, sync_playwright

test_schema = "http"
test_hostname = "127.0.0.1"
test_port = 8000
test_url = f"{test_schema}://{test_hostname}:{test_port}/"


title = "title"
subtitle = "subtitle"
stubhead = "stubhead"
spanner = "spanner"
spanner_columns = ["date", "time", "datetime"]
source_note = "source_note"
columns = exibble.columns.to_list()
columns.remove("group")  # used in tab_stub
columns.remove("row")  # used in tab_stub


def run_server():
    with socketserver.TCPServer(
        (test_hostname, test_port), http.server.SimpleHTTPRequestHandler
    ) as httpd:
        httpd.serve_forever()


@pytest.fixture(scope="module", autouse=True)
def start_server():
    process = Process(target=run_server, daemon=True)
    process.start()
    yield
    process.terminate()


@pytest.fixture(scope="module")
def gtbl_html():
    return (
        GT(exibble)
        .tab_stub(groupname_col="group", rowname_col="row")
        .tab_stubhead(stubhead)
        .tab_spanner(spanner, columns=spanner_columns)
        .tab_header(title=title, subtitle=subtitle)
        .tab_source_note(source_note)
    ).as_raw_html()


@pytest.fixture(scope="module")
def html_content(gtbl_html):
    return """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    {}
</body>
</html>
""".format(gtbl_html)


@pytest.fixture(scope="module", autouse=True)
def write_index_html(html_content):
    index_html = Path("index.html")
    index_html.write_text(html_content, encoding="utf-8")
    yield
    index_html.unlink()


@pytest.mark.parametrize("core", ["chromium", "firefox"])
def test_page(core):
    with sync_playwright() as p:
        browser = getattr(p, core).launch(headless=True)
        page = browser.new_page()
        page.goto(test_url)

        # group
        group_loc = page.locator("tr:has(th.gt_group_heading)").first
        expect(group_loc).to_have_text("grp_a")

        # row
        row_1_loc = page.locator(".gt_stub").first
        expect(row_1_loc).to_have_text("row_1")

        # stubhead
        stubhead_loc = page.locator("#stubhead")
        expect(stubhead_loc).to_have_text(stubhead)

        # spanner
        spanner_loc = page.locator(".gt_column_spanner")
        expect(spanner_loc).to_have_text(spanner)

        for s_col in spanner_columns:
            spanner_col_loc = page.locator(f"#{s_col}")
            rowspan = spanner_col_loc.get_attribute("rowspan")
            assert rowspan == "1"

        # title
        title_loc = page.locator(".gt_title")
        expect(title_loc).to_have_text(title)

        # subtitle
        subtitle_loc = page.locator(".gt_subtitle")
        expect(subtitle_loc).to_have_text(subtitle)

        # sourcenote
        sourcenote_loc = page.locator(".gt_sourcenote")
        expect(sourcenote_loc).to_have_text(source_note)

        # columns
        for column in columns:
            column_loc = page.locator(f"#{column}")
            expect(column_loc).to_have_text(column)

        browser.close()

Do you think this is a valuable addition, and should we include this test in our CI pipeline? If there’s interest, I’d be happy to work on a PR for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant