Skip to content

Commit

Permalink
Use pytest-playwright (#2185)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored Nov 27, 2024
1 parent 0348999 commit 1247b5c
Show file tree
Hide file tree
Showing 24 changed files with 121 additions and 296 deletions.
2 changes: 1 addition & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ignore = [
]

[lint.per-file-ignores]
"betty/tests/*" = [
"betty/{tests,tests_playwright}/*" = [
'D100',
'D101',
'D102',
Expand Down
2 changes: 2 additions & 0 deletions betty/tests/coverage/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ async def _test_python_file(self, file_path: Path) -> AsyncIterable[str]:
# Skip tests.
if ROOT_DIRECTORY_PATH / "betty" / "tests" in file_path.parents:
return
if ROOT_DIRECTORY_PATH / "betty" / "tests_playwright" in file_path.parents:
return

src_module_path = file_path.resolve()
expected_test_module_path = (
Expand Down
7 changes: 1 addition & 6 deletions betty/tests/project/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ async def test_supports(self, expected: bool, resource: Any) -> None:
@pytest.mark.parametrize(
(
"expected",
"base_url",
"root_path",
"locales",
"clean_urls",
Expand All @@ -209,7 +208,6 @@ async def test_supports(self, expected: bool, resource: Any) -> None:
[
(
"/index.html",
"https://example.com",
"/",
{DEFAULT_LOCALE: DEFAULT_LOCALE},
False,
Expand All @@ -219,7 +217,6 @@ async def test_supports(self, expected: bool, resource: Any) -> None:
# Absolute URLs.
(
"https://example.com/index.html",
"https://example.com",
"/",
{DEFAULT_LOCALE: DEFAULT_LOCALE},
False,
Expand All @@ -229,7 +226,6 @@ async def test_supports(self, expected: bool, resource: Any) -> None:
# Clean URLs.
(
"/",
"https://example.com",
"/",
{DEFAULT_LOCALE: DEFAULT_LOCALE},
True,
Expand All @@ -241,12 +237,11 @@ async def test_supports(self, expected: bool, resource: Any) -> None:
async def test_generate(
self,
expected: str,
base_url: str,
root_path: str,
locales: Mapping[str, str],
clean_urls: bool,
resource: str,
absolute: bool,
) -> None:
sut = StaticUrlGenerator(base_url, root_path, locales, clean_urls)
sut = StaticUrlGenerator("https://example.com", root_path, locales, clean_urls)
assert sut.generate(resource, absolute=absolute) == expected
8 changes: 1 addition & 7 deletions betty/tests/url/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class TestGenerateFromPath:
@pytest.mark.parametrize(
(
"expected",
"base_url",
"root_path",
"locales",
"clean_urls",
Expand All @@ -28,7 +27,6 @@ class TestGenerateFromPath:
*[
(
expected,
"https://example.com",
"/",
{DEFAULT_LOCALE: DEFAULT_LOCALE},
False,
Expand All @@ -48,7 +46,6 @@ class TestGenerateFromPath:
*[
(
expected,
"https://example.com",
"/",
{DEFAULT_LOCALE: DEFAULT_LOCALE},
False,
Expand All @@ -68,7 +65,6 @@ class TestGenerateFromPath:
*[
(
expected,
"https://example.com",
"/",
{DEFAULT_LOCALE: DEFAULT_LOCALE},
True,
Expand All @@ -88,7 +84,6 @@ class TestGenerateFromPath:
*[
(
expected,
"https://example.com",
"/",
{DEFAULT_LOCALE: DEFAULT_LOCALE, "nl-NL": "nl"},
False,
Expand All @@ -109,7 +104,6 @@ class TestGenerateFromPath:
async def test(
self,
expected: str,
base_url: str,
root_path: str,
locales: Mapping[str, str],
clean_urls: bool,
Expand All @@ -122,7 +116,7 @@ async def test(
path,
absolute=absolute,
locale=locale,
base_url=base_url,
base_url="https://example.com",
root_path=root_path,
locales=locales,
clean_urls=clean_urls,
Expand Down
Empty file.
7 changes: 7 additions & 0 deletions betty/tests_playwright/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
Pytest configuration.
"""

from __future__ import annotations

from betty.tests.conftest import * # noqa F403
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from collections.abc import AsyncIterator

import pytest
from playwright.async_api import expect, Page

from betty import serve
from betty.ancestry.person import Person
from betty.ancestry.person_name import PersonName
from betty.app import App
from betty.project import Project
from betty.project.extension.cotton_candy import CottonCandy
from betty.project.generate import generate
from betty.serve import Server


class TestSearchUi:
@pytest.fixture(scope="session")
async def served_project(self) -> AsyncIterator[tuple[Project, Server]]:
person_id = "I0001"
person = Person(id=person_id)
person_individual_name = "Janet"
PersonName(individual=person_individual_name, person=person)
async with (
App.new_temporary() as app,
app,
Project.new_temporary(app) as project,
):
project.configuration.extensions.enable(CottonCandy)
project.ancestry[Person].add(person)
async with project:
await generate(project)
async with await serve.BuiltinProjectServer.new_for_project(
project
) as server:
yield project, server

@pytest.mark.asyncio(loop_scope="session")
async def test(self, page: Page, served_project: tuple[Project, Server]) -> None:
project, server = served_project
person = project.ancestry[Person]["I0001"]
await page.goto(server.public_url)
search_query = page.locator("#search-query")
individual_name = person.names[0].individual
assert individual_name
await search_query.fill(individual_name)
await search_query.press("ArrowDown")
await expect(page.locator("#search-results")).to_be_visible()
await page.keyboard.press("ArrowDown")
await page.locator(":focus").press("Enter")
assert page.url == f"{server.public_url}/person/{person.id}/index.html"
await page.close()
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from collections.abc import AsyncIterator

import pytest
from playwright.async_api import expect, Page

from betty import serve
from betty.app import App
from betty.project import Project
from betty.project.extension.http_api_doc import HttpApiDoc
from betty.project.generate import generate
from betty.serve import Server


class TestSwaggerUi:
@pytest.fixture(scope="session")
async def served_project(self) -> AsyncIterator[tuple[Project, Server]]:
async with (
App.new_temporary() as app,
app,
Project.new_temporary(app) as project,
):
project.configuration.extensions.enable(HttpApiDoc)
async with project:
await generate(project)
async with await serve.BuiltinProjectServer.new_for_project(
project
) as server:
yield project, server

@pytest.mark.asyncio(loop_scope="session")
async def test(self, page: Page, served_project: tuple[Project, Server]) -> None:
project, server = served_project
await page.goto(server.public_url + "/api/index.html")
locator = page.locator("#swagger-ui")
# Test a couple of keywords in the source.
await expect(locator).to_contain_text("Betty")
await expect(locator).to_contain_text("api/index.json")
# Test a couple of keywords shown after successful rendering.
await expect(locator).to_contain_text("Retrieve a single")
await expect(locator).to_contain_text("Retrieve the collection")
await page.close()
6 changes: 2 additions & 4 deletions bin/build-ci
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ cd "$(dirname "$0")/.."

# Install Python dependencies.
pip install '.[ci]'
./bin/build-playwright

# Install JavaScript dependencies.
npm install
npm install --no-save "@types/node@$(node -e 'console.log(process.versions.node.split(".")[0])')"
npx playwright install --with-deps
./bin/build-dev-npm
3 changes: 0 additions & 3 deletions bin/build-dev-npm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ cd "$(dirname "$0")/.."
# Install JavaScript dependencies.
npm install
npm install --no-save "@types/node@$(node -e 'console.log(process.versions.node.split(".")[0])')"
# Install Playwright browser dependencies, but allow those
# to fail as Playwright runs on very few systems only.
npx playwright install --with-deps || true
1 change: 1 addition & 0 deletions bin/build-dev-pip
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ cd "$(dirname "$0")/.."

# Install Python dependencies.
pip install -e '.[development]'
./bin/build-playwright
9 changes: 9 additions & 0 deletions bin/build-playwright
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -Eeuo pipefail

cd "$(dirname "$0")/.."

# Install Playwright browser dependencies, but allow those
# to fail as Playwright runs on very few systems only.
playwright install --with-deps || true
2 changes: 1 addition & 1 deletion bin/fix
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ ruff format .
./node_modules/.bin/stylelint --fix "./betty/**/*.css" "./betty/**/*.scss"

# Fix JS code style violations.
./node_modules/.bin/eslint --fix -c ./eslint.config.js ./betty ./playwright
./node_modules/.bin/eslint --fix -c ./eslint.config.js ./betty
2 changes: 1 addition & 1 deletion bin/test-eslint
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ cd "$(dirname "$0")/.."

echo 'Running ESLint...'

./node_modules/.bin/eslint -c ./eslint.config.js ./betty ./playwright "$@"
./node_modules/.bin/eslint -c ./eslint.config.js ./betty "$@"
2 changes: 1 addition & 1 deletion bin/test-playwright
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ cd "$(dirname "$0")/.."

echo 'Running Playwright...'

./node_modules/.bin/playwright test "$@"
pytest ./betty/tests_playwright "$@"
2 changes: 0 additions & 2 deletions documentation/development/betty/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ Helpful commands
Run `ESLint <https://eslint.org/>`_ tests.
``./bin/test-mypy``
Run `mypy <https://www.mypy-lang.org/>`_ tests.
``./bin/test-playwright``
Run `Playwright <https://playwright.dev>`_ tests.
``./bin/test-pytest``
Run `pytest <https://docs.pytest.org/en/stable/>`_ tests.
``./bin/test-ruff``
Expand Down
10 changes: 0 additions & 10 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import eslint from '@eslint/js'
import stylistic from '@stylistic/eslint-plugin'
import globals from 'globals'
import playwright from 'eslint-plugin-playwright'
import tseslint from 'typescript-eslint'

const typescriptFiles = [
Expand Down Expand Up @@ -39,15 +38,6 @@ export default [
},
},

// Playwright tests.
{
files: [
'playwright/tests/**',
],
...playwright.configs['flat/recommended'],

},

// Generic EcmaScript.
{
plugins: {
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"node": ">= 20"
},
"devDependencies": {
"@playwright/test": "^1.49.0",
"@stylistic/eslint-plugin": "^2.10.1",
"@types/node": "*",
"@types/webpack": "^5.28.5",
Expand All @@ -15,7 +14,6 @@
"eslint": "^9.15.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-n": "^17.13.2",
"eslint-plugin-playwright": "^2.1.0",
"eslint-plugin-promise": "^7.1.0",
"stylelint": "^16.10.0",
"stylelint-config-standard": "^36.0.1",
Expand Down
26 changes: 0 additions & 26 deletions playwright/fixtures/gramps.xml

This file was deleted.

42 changes: 0 additions & 42 deletions playwright/tests/cotton_candy/search.spec.ts

This file was deleted.

Loading

0 comments on commit 1247b5c

Please sign in to comment.