-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Missing files for Playwright test, refs #19
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |