Skip to content

Commit

Permalink
Missing files for Playwright test, refs #19
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 8, 2024
1 parent f11098d commit 04089a1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from pathlib import Path
import pytest
import sqlite_utils
from subprocess import Popen, PIPE
import sys
import time
import httpx


@pytest.fixture
def db_path(tmpdir):
path = str(tmpdir / "data.db")
db = sqlite_utils.Database(path)
db["creatures"].insert_all(
[
{"name": "Cleo", "description": "A medium sized dog"},
{"name": "Siroco", "description": "A troublesome Kakapo"},
]
)
return path


@pytest.fixture
def ds_server(db_path):
process = Popen(
[
sys.executable,
"-m",
"datasette",
"--port",
"8126",
str(db_path),
],
stdout=PIPE,
)
wait_until_responds("http://localhost:8126/")
yield "http://localhost:8126"

process.terminate()
process.wait()


def wait_until_responds(url, timeout=5.0, **kwargs):
start = time.time()
while time.time() - start < timeout:
try:
httpx.get(url, **kwargs)
return
except httpx.ConnectError:
time.sleep(0.1)
raise AssertionError("Timed out waiting for {} to respond".format(url))
13 changes: 13 additions & 0 deletions tests/test_playwright.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
try:
from playwright import sync_api
except ImportError:
sync_api = None
import pytest

@pytest.mark.skipif(sync_api is None, reason="playwright not installed")
def test_search(ds_server):
with sync_api.sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page()
page.goto(ds_server + "/")
assert page.title() == "Datasette: data"

0 comments on commit 04089a1

Please sign in to comment.