-
Notifications
You must be signed in to change notification settings - Fork 32
UI tests with Playwright #413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 18 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
7c58c7f
wip
blakerosenthal e36f4b4
working health page
blakerosenthal 45a7f33
working api server
blakerosenthal b83aafe
fix headless mode
blakerosenthal 463f564
fix timeout logic
blakerosenthal c6f2153
add more tests, auth header not working
blakerosenthal c5f3eee
assign text ports and skip failing auth test
blakerosenthal f80cadb
Merge branch 'main' into ui-tests
blakerosenthal c9ccca8
placeholder test for ui chat
blakerosenthal 1a6f840
install playwright to gh env
blakerosenthal 1dc7548
make an api wrapper to try to get around the pickling issue on mac/wi…
blakerosenthal 1c81128
always install playwright
blakerosenthal cd68b85
some PR review fixes
blakerosenthal fc61dbe
move shared stuff to utils
blakerosenthal 6dc91eb
wip
blakerosenthal d9f19cb
wip: replace server with cli function
blakerosenthal b7e59a5
remove breakpoint
blakerosenthal d62bf5e
increase timeout
pmeier e6ab71b
server mods
blakerosenthal 382d71b
use playwright Page directly
blakerosenthal b378a51
check to make sure document is in database
blakerosenthal 53b4c4f
increase timeout
blakerosenthal 0d80801
increase timeout again
blakerosenthal aae2f22
use consistent ports
blakerosenthal 36e6f43
try with slowmo
blakerosenthal 050dc72
reorder element expectations; remove slowmo
blakerosenthal a983307
hack
blakerosenthal f49bdd8
upload playwright video on failing tests
blakerosenthal 648823d
syntax fix
blakerosenthal 7038f30
run upload on failure
blakerosenthal 855c0d3
get tests to pass again
blakerosenthal 7672c0c
Merge branch 'main' into ui-tests
blakerosenthal 50c0ae1
separate ui and non-ui tests
blakerosenthal 4dade55
avoid relative imports; better func name
blakerosenthal 162e982
use function scope for test ports
blakerosenthal 8c82a4d
review nits
blakerosenthal dcd4339
make Server a context manager
blakerosenthal c8ba9dc
try to fix CI matrix
blakerosenthal d2dfcce
attempt #2
blakerosenthal 8a60430
nits
pmeier 37e9812
remove unused func
blakerosenthal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,182 @@ | ||
import socket | ||
import subprocess | ||
import sys | ||
import time | ||
|
||
import httpx | ||
import panel as pn | ||
import pytest | ||
from playwright.sync_api import expect, sync_playwright | ||
|
||
from ragna._utils import timeout_after | ||
from ragna.deploy import Config | ||
|
||
from ..utils import TestAssistant | ||
blakerosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def get_available_port(): | ||
with socket.socket() as s: | ||
s.bind(("", 0)) | ||
return s.getsockname()[1] | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def headed_mode(pytestconfig): | ||
return pytestconfig.getoption("headed") or False | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def ui_port(): | ||
return get_available_port() | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def api_port(): | ||
return get_available_port() | ||
|
||
|
||
@pytest.fixture | ||
def config(tmp_local_root, ui_port, api_port): | ||
config = Config( | ||
local_root=tmp_local_root, | ||
assistants=[TestAssistant], | ||
ui=dict(port=ui_port), | ||
api=dict(port=api_port), | ||
) | ||
path = tmp_local_root / "ragna.toml" | ||
config.to_file(path) | ||
return config | ||
|
||
|
||
class Server: | ||
def __init__(self, config, base_url): | ||
self.config = config | ||
self.base_url = base_url | ||
|
||
def server_up(self): | ||
try: | ||
return httpx.get(self.base_url).is_success | ||
except httpx.ConnectError: | ||
return False | ||
|
||
@timeout_after(30) | ||
def start(self): | ||
self.proc = subprocess.Popen( | ||
[ | ||
sys.executable, | ||
"-m", | ||
"ragna", | ||
"ui", | ||
"--config", | ||
self.config.local_root / "ragna.toml", | ||
"--start-api", | ||
"--ignore-unavailable-components", | ||
"--no-open-browser", | ||
], | ||
stdout=sys.stdout, | ||
stderr=sys.stderr, | ||
) | ||
|
||
while not self.server_up(): | ||
time.sleep(1) | ||
|
||
def stop(self): | ||
self.proc.kill() | ||
pn.state.kill_all_servers() | ||
|
||
|
||
@pytest.fixture | ||
def base_ui_url(ui_port): | ||
return f"http://127.0.0.1:{ui_port}" | ||
|
||
|
||
@pytest.fixture | ||
def server(config, base_ui_url): | ||
server = Server(config, base_ui_url) | ||
try: | ||
server.start() | ||
yield server | ||
finally: | ||
server.stop() | ||
|
||
|
||
@pytest.fixture | ||
def context(server, headed_mode): | ||
with sync_playwright() as playwright: | ||
browser = playwright.chromium.launch(headless=not headed_mode) | ||
context = browser.new_context() | ||
|
||
yield context | ||
|
||
context.close() | ||
browser.close() | ||
|
||
|
||
def test_health(base_ui_url, context) -> None: | ||
page = context.new_page() | ||
health_url = base_ui_url + "/health" | ||
response = page.goto(health_url) | ||
assert response.ok | ||
|
||
|
||
def test_index(base_ui_url, context, config) -> None: | ||
# Index page, no auth | ||
page = context.new_page() | ||
index_url = base_ui_url | ||
page.goto(index_url) | ||
expect(page.get_by_role("button", name="Sign In")).to_be_visible() | ||
|
||
# Authorize with no credentials | ||
page.get_by_role("button", name="Sign In").click() | ||
expect(page.get_by_role("button", name=" New Chat")).to_be_visible() | ||
|
||
# expect auth token to be set | ||
cookies = context.cookies() | ||
assert len(cookies) == 1 | ||
cookie = cookies[0] | ||
assert cookie.get("name") == "auth_token" | ||
auth_token = cookie.get("value") | ||
assert auth_token is not None | ||
|
||
# New page button | ||
new_chat_button = page.get_by_role("button", name=" New Chat") | ||
expect(new_chat_button).to_be_visible() | ||
new_chat_button.click() | ||
|
||
document_root = config.local_root / "documents" | ||
document_root.mkdir() | ||
document_name = "test.txt" | ||
document_path = document_root / document_name | ||
with open(document_path, "w") as file: | ||
file.write("!\n") | ||
|
||
# File upload selector | ||
with page.expect_file_chooser() as fc_info: | ||
page.locator(".fileUpload").click() | ||
file_chooser = fc_info.value | ||
# file_chooser.set_files(document_path) | ||
file_chooser.set_files( | ||
files=[ | ||
{"name": "test.txt", "mimeType": "text/plain", "buffer": b"this is a test"} | ||
] | ||
) | ||
|
||
# Upload file and expect to see it listed | ||
file_list = page.locator(".fileListContainer") | ||
expect(file_list.first).to_have_text(str(document_name)) | ||
|
||
start_chat_button = page.get_by_role("button", name="Start Conversation") | ||
expect(start_chat_button).to_be_visible() | ||
start_chat_button.click() | ||
|
||
# chat_box = page.get_by_placeholder("Ask a question about the") | ||
# expect(chat_box).to_be_visible() | ||
|
||
# page.get_by_placeholder("Ask a question about the").fill( | ||
# "Tell me about the documents" | ||
# ) | ||
# page.get_by_role("button", name="").click() | ||
# page.get_by_role("button", name=" Source Info").click() | ||
# page.locator("#main div").filter(has_text="Source Info ¶ This response").nth( | ||
# 3 | ||
# ).click() |
This file contains hidden or 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,40 @@ | ||
import os | ||
import time | ||
|
||
from fastapi.testclient import TestClient | ||
|
||
from ragna.assistants import RagnaDemoAssistant | ||
from ragna.core._utils import default_user | ||
|
||
|
||
class TestAssistant(RagnaDemoAssistant): | ||
def answer(self, prompt, sources, *, multiple_answer_chunks: bool): | ||
blakerosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Simulate a "real" assistant through a small delay. See | ||
# https://github.com/Quansight/ragna/pull/401#issuecomment-2095851440 | ||
# for why this is needed. | ||
time.sleep(1e-3) | ||
content = next(super().answer(prompt, sources)) | ||
|
||
if multiple_answer_chunks: | ||
for chunk in content.split(" "): | ||
yield f"{chunk} " | ||
else: | ||
yield content | ||
|
||
|
||
def authenticate(client: TestClient) -> None: | ||
blakerosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
username = default_user() | ||
token = ( | ||
client.post( | ||
"/token", | ||
data={ | ||
"username": username, | ||
"password": os.environ.get( | ||
"RAGNA_DEMO_AUTHENTICATION_PASSWORD", username | ||
), | ||
}, | ||
) | ||
.raise_for_status() | ||
.json() | ||
) | ||
client.headers["Authorization"] = f"Bearer {token}" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.