-
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 12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import os | ||
import time | ||
from multiprocessing import Process | ||
|
||
import httpx | ||
import panel as pn | ||
import pytest | ||
import uvicorn | ||
from playwright.sync_api import expect, sync_playwright | ||
|
||
from ragna.assistants import RagnaDemoAssistant | ||
from ragna.core._utils import default_user | ||
from ragna.deploy import ApiConfig, Config, UiConfig | ||
from ragna.deploy._api import app as api_app | ||
from ragna.deploy._ui import app as ui_app | ||
|
||
TEST_API_PORT = "38769" | ||
TEST_UI_PORT = "38770" | ||
blakerosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class TestAssistant(RagnaDemoAssistant): | ||
def answer(self, prompt, sources, *, multiple_answer_chunks: bool): | ||
content = next(super().answer(prompt, sources)) | ||
|
||
if multiple_answer_chunks: | ||
for chunk in content.split(" "): | ||
yield f"{chunk} " | ||
else: | ||
yield content | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def headed_mode(pytestconfig): | ||
return pytestconfig.getoption("headed") or False | ||
blakerosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
@pytest.fixture | ||
def config(tmp_local_root): | ||
return Config( | ||
local_root=tmp_local_root, | ||
assistants=[TestAssistant], | ||
ui=UiConfig(port=TEST_UI_PORT), | ||
api=ApiConfig(port=TEST_API_PORT), | ||
blakerosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
|
||
class ApiServer: | ||
def __init__(self, config): | ||
self.config = config | ||
|
||
def start_server(self): | ||
uvicorn.run( | ||
api_app( | ||
config=self.config, | ||
ignore_unavailable_components=True, | ||
), | ||
host=self.config.api.hostname, | ||
port=self.config.api.port, | ||
) | ||
|
||
def server_up(self): | ||
try: | ||
return httpx.get(self.config.api.url).is_success | ||
except httpx.ConnectError: | ||
return False | ||
|
||
def start(self): | ||
self.proc = Process(target=self.start_server, args=(), daemon=True) | ||
self.proc.start() | ||
|
||
timeout = 5 | ||
while timeout > 0 and not self.server_up(): | ||
print(f"Waiting for API server to come up on {self.config.api.url}") | ||
time.sleep(1) | ||
timeout -= 1 | ||
blakerosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def stop(self): | ||
self.proc.kill() | ||
|
||
|
||
@pytest.fixture | ||
def api_server(config): | ||
server = ApiServer(config) | ||
try: | ||
server.start() | ||
yield server | ||
finally: | ||
server.stop() | ||
|
||
|
||
def auth_header(base_url): | ||
username = default_user() | ||
token = ( | ||
httpx.post( | ||
base_url + "/token", | ||
data={ | ||
"username": username, | ||
"password": os.environ.get( | ||
"RAGNA_DEMO_AUTHENTICATION_PASSWORD", username | ||
), | ||
}, | ||
) | ||
.raise_for_status() | ||
.json() | ||
) | ||
|
||
return f"Bearer {token}" | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def page(config, api_server, headed_mode): | ||
server = ui_app(config=config, open_browser=False) | ||
blakerosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
with sync_playwright() as playwright: | ||
server.serve() | ||
browser = playwright.chromium.launch(headless=not headed_mode) | ||
context = browser.new_context() | ||
page = context.new_page() | ||
|
||
yield page | ||
|
||
context.close() | ||
browser.close() | ||
pn.state.kill_all_servers() | ||
|
||
|
||
def test_health(config, page) -> None: | ||
health_url = config.ui.origins[0] + "/health" | ||
blakerosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
page.goto(health_url) | ||
expect(page.get_by_role("heading", name="Ok")).to_be_visible() | ||
blakerosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_index_with_blank_credentials(config, page) -> None: | ||
# Index page, no auth | ||
index_url = config.ui.origins[0] | ||
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() | ||
|
||
|
||
@pytest.mark.skip(reason="Need to figure out how to set the auth token") | ||
def test_index_with_auth_token(config, page) -> None: | ||
auth = auth_header(config.api.url) | ||
assert len(auth) > len("Bearer ") | ||
|
||
page.set_extra_http_headers({"Authorization": auth}) # this doesn't work | ||
pn.state.cookies["auth_token"] = "" # or this | ||
pn.state.headers["Authorization"] = auth # or this | ||
|
||
index_url = config.ui.origins[0] | ||
page.goto(index_url) | ||
expect(page.get_by_role("button", name=" New Chat")).to_be_visible() | ||
blakerosenthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
@pytest.mark.skip(reason="TODO: figure out best locators") | ||
def test_new_chat(config, page) -> None: | ||
index_url = config.ui.origins[0] | ||
page.goto(index_url) | ||
expect(page.get_by_role("button", name="Sign In")).to_be_visible() | ||
page.get_by_role("button", name="Sign In").click() | ||
|
||
expect(page.get_by_role("button", name=" New Chat")).to_be_visible() | ||
page.get_by_role("button", name=" New Chat").click() | ||
|
||
expect(page.locator("#fileUpload-p4447")).to_be_visible() | ||
page.locator("#fileUpload-p4447").click() | ||
|
||
page.locator("#fileUpload-p4447").set_input_files() | ||
page.get_by_role("button", name="Start Conversation").click() | ||
page.get_by_text("How can I help you with the").click() | ||
page.get_by_placeholder("Ask a question about the").click() | ||
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() |
Oops, something went wrong.
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.