From c6c42027366efb7a84a23a3feec72ea63efc6b8d Mon Sep 17 00:00:00 2001 From: Anis Jellazi <48861995+anisjellazi@users.noreply.github.com> Date: Wed, 5 Feb 2025 17:02:42 +0100 Subject: [PATCH 1/2] Added StarletteClient driver for FastAPI support --- tests/test_starletteclient.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/test_starletteclient.py diff --git a/tests/test_starletteclient.py b/tests/test_starletteclient.py new file mode 100644 index 000000000..ced94c94c --- /dev/null +++ b/tests/test_starletteclient.py @@ -0,0 +1,11 @@ +from splinter import Browser +from fastapi import FastAPI + +def test_starlette_client(): + app = FastAPI() + browser = Browser("starlette", app=app) # Create the browser instance + + browser.visit("http://example.com") # Try to visit a page + assert browser.title == "Example Domain" # Check if title is correct + + browser.quit() From fc592db512b8a7681fbc44790b278aaf01eff9b0 Mon Sep 17 00:00:00 2001 From: Anis Jellazi <48861995+anisjellazi@users.noreply.github.com> Date: Thu, 6 Feb 2025 01:20:11 +0100 Subject: [PATCH 2/2] Add test for Starlette client --- tests/conftest.py | 4 +++ tests/test_starletteclient.py | 55 +++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 9189df4d1..f8ce17e5d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,10 @@ from urllib import parse from urllib.request import urlopen +import os +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') + + import pytest from tests.fake_webapp_server import start_server, stop_server diff --git a/tests/test_starletteclient.py b/tests/test_starletteclient.py index ced94c94c..c0a8a1573 100644 --- a/tests/test_starletteclient.py +++ b/tests/test_starletteclient.py @@ -1,11 +1,54 @@ -from splinter import Browser from fastapi import FastAPI +from starlette.responses import HTMLResponse +from splinter import Browser +import pytest +import logging + +# ----- Step 1: Define a minimal FastAPI application ----- +app = FastAPI() +@app.get("/", response_class=HTMLResponse) +def read_root(): + # Return an HTML page with a title set to "Example Domain" + return """ + + +
+This is a sample page for testing.
+ + + """ + +# ----- Step 2: Write the test function using the StarletteClient ----- def test_starlette_client(): - app = FastAPI() - browser = Browser("starlette", app=app) # Create the browser instance + browser = Browser("starlette", app=app) + + try: + # Visit the local route "/" served by our FastAPI app. + browser.visit("/") + + # Check the title to match the expected "Example Domain" + assert browser.title == "Example Domain" + + # Assert that some content is present on the page (the header in this case) + assert browser.is_text_present("Welcome to Example Domain") + + # Check for the presence of a paragraph (new test case) + assert browser.is_text_present("This is a sample page for testing.") + + # Optionally, check that the number of