Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added tests/__init__.py
Empty file.
75 changes: 75 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

import sys
import os
import socket
from typing import Generator, Optional, Type
import pytest
import threading
import http.server
import socketserver

pytest_plugins = ["pytester"]

Expand All @@ -29,3 +35,72 @@
playwright_browser_path = f"{user_profile}\\AppData\\Local\\ms-playwright"

os.environ["PLAYWRIGHT_BROWSERS_PATH"] = playwright_browser_path


class HTTPTestServer:
PREFIX = ""
EMPTY_PAGE = ""

def __init__(self) -> None:
self._server: Optional[socketserver.TCPServer] = None
self._server_thread: Optional[threading.Thread] = None
self._port: int = 0

def start(self) -> None:
"""Start the test server."""

# Efficiently find an available port using a raw socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("localhost", 0))
self._port = s.getsockname()[1]

# Create the actual server
self._server = socketserver.TCPServer(
("localhost", self._port), self._create_handler()
)
self._server_thread = threading.Thread(target=self._server.serve_forever)
self._server_thread.daemon = True
self._server_thread.start()

self.PREFIX = f"http://localhost:{self._port}"
self.EMPTY_PAGE = f"{self.PREFIX}/empty.html"
self.CROSS_PROCESS_PREFIX = f"http://127.0.0.1:{self._port}"

def stop(self) -> None:
"""Stop the test server."""
if self._server:
self._server.shutdown()
self._server.server_close()

def _create_handler(self) -> Type[http.server.SimpleHTTPRequestHandler]:
"""Create a request handler class."""

class SimpleHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self) -> None:
"""Handle GET requests and return simple HTML with the path."""
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()

html_content = f"""<!DOCTYPE html>
<html>
<head>
<title>Test Server</title>
</head>
<body>
<h1>Test Server Response</h1>
<p>Path: {self.path}</p>
<span id="foo">bar</span>
</body>
</html>"""
self.wfile.write(html_content.encode("utf-8"))

return SimpleHTTPRequestHandler


@pytest.fixture(scope="session")
def test_server() -> Generator[HTTPTestServer, None, None]:
server = HTTPTestServer()
server.start()
yield server
server.stop()
28 changes: 16 additions & 12 deletions tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import pytest

from tests.conftest import HTTPTestServer


@pytest.fixture
def pytester(pytester: pytest.Pytester) -> pytest.Pytester:
Expand Down Expand Up @@ -257,37 +259,39 @@ async def test_is_webkit(page, browser_name, is_chromium, is_firefox, is_webkit)
result.assert_outcomes(passed=1)


def test_goto(testdir: pytest.Testdir) -> None:
def test_goto(testdir: pytest.Testdir, test_server: HTTPTestServer) -> None:
testdir.makepyfile(
"""
f"""
import pytest
@pytest.mark.asyncio
async def test_base_url(page, base_url):
assert base_url == "https://example.com"
assert base_url == "{test_server.PREFIX}"
await page.goto("/foobar")
assert page.url == "https://example.com/foobar"
await page.goto("https://example.org")
assert page.url == "https://example.org/"
assert page.url == "{test_server.PREFIX}/foobar"
await page.goto("{test_server.CROSS_PROCESS_PREFIX}")
assert page.url == "{test_server.CROSS_PROCESS_PREFIX}/"
"""
)
result = testdir.runpytest("--base-url", "https://example.com")
result = testdir.runpytest("--base-url", test_server.PREFIX)
result.assert_outcomes(passed=1)


def test_base_url_via_fixture(testdir: pytest.Testdir) -> None:
def test_base_url_via_fixture(
testdir: pytest.Testdir, test_server: HTTPTestServer
) -> None:
testdir.makepyfile(
"""
f"""
import pytest

@pytest.fixture(scope="session")
def base_url():
return "https://example.com"
return "{test_server.PREFIX}"

@pytest.mark.asyncio
async def test_base_url(page, base_url):
assert base_url == "https://example.com"
assert base_url == "{test_server.PREFIX}"
await page.goto("/foobar")
assert page.url == "https://example.com/foobar"
assert page.url == "{test_server.PREFIX}/foobar"
"""
)
result = testdir.runpytest()
Expand Down
28 changes: 16 additions & 12 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import pytest

from tests.conftest import HTTPTestServer


@pytest.fixture
def pytester(pytester: pytest.Pytester) -> pytest.Pytester:
Expand Down Expand Up @@ -283,34 +285,36 @@ def test_is_webkit(page, browser_name, is_chromium, is_firefox, is_webkit):
result.assert_outcomes(passed=1)


def test_goto(testdir: pytest.Testdir) -> None:
def test_goto(testdir: pytest.Testdir, test_server: HTTPTestServer) -> None:
testdir.makepyfile(
"""
f"""
def test_base_url(page, base_url):
assert base_url == "https://example.com"
assert base_url == "{test_server.PREFIX}"
page.goto("/foobar")
assert page.url == "https://example.com/foobar"
page.goto("https://example.org")
assert page.url == "https://example.org/"
assert page.url == "{test_server.PREFIX}/foobar"
page.goto("{test_server.CROSS_PROCESS_PREFIX}")
assert page.url == "{test_server.CROSS_PROCESS_PREFIX}/"
"""
)
result = testdir.runpytest("--base-url", "https://example.com")
result = testdir.runpytest("--base-url", test_server.PREFIX)
result.assert_outcomes(passed=1)


def test_base_url_via_fixture(testdir: pytest.Testdir) -> None:
def test_base_url_via_fixture(
testdir: pytest.Testdir, test_server: HTTPTestServer
) -> None:
testdir.makepyfile(
"""
f"""
import pytest

@pytest.fixture(scope="session")
def base_url():
return "https://example.com"
return "{test_server.PREFIX}"

def test_base_url(page, base_url):
assert base_url == "https://example.com"
assert base_url == "{test_server.PREFIX}"
page.goto("/foobar")
assert page.url == "https://example.com/foobar"
assert page.url == "{test_server.PREFIX}/foobar"
"""
)
result = testdir.runpytest()
Expand Down
Loading