From 45a1a98d61a9e1ca72aee36c4026c3733c9a43d7 Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Thu, 9 Jan 2025 15:57:22 +0200 Subject: [PATCH] Add an example test case with Locust + Playwright Refs #721 --- .github/workflows/testing.yml | 4 ++ requirements/devel.txt | 1 + tests/performance/base.py | 47 ++++++++++++++++++++++++ tests/performance/web_simulation_test.py | 24 ++++++++++++ tests/test_http.sh | 1 + 5 files changed, 77 insertions(+) create mode 100644 tests/performance/web_simulation_test.py diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 73cbc6684b..23872f509e 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -320,6 +320,10 @@ jobs: EOF sudo apt-get install firefox firefox-geckodriver + # Locust + Playwright setup + pip uninstall --yes trio + playwright install firefox + - name: Docker version info run: | docker --version diff --git a/requirements/devel.txt b/requirements/devel.txt index 004aa84f71..52b4f24324 100644 --- a/requirements/devel.txt +++ b/requirements/devel.txt @@ -5,6 +5,7 @@ isort==5.13.2 colorama black==24.10.0 locust +locust-plugins[playwright] parameterized robotframework robotframework-seleniumlibrary diff --git a/tests/performance/base.py b/tests/performance/base.py index 1c1eff8176..d1ac4c2848 100644 --- a/tests/performance/base.py +++ b/tests/performance/base.py @@ -4,6 +4,7 @@ # https://www.gnu.org/licenses/agpl-3.0.html from locust import FastHttpUser, between, task +from locust_plugins.users.playwright import PlaywrightUser from requests.utils import dict_from_cookiejar @@ -15,6 +16,9 @@ class LoggedInTestCase(FastHttpUser): login_url = "/accounts/login/" def on_start(self): + self.do_login() + + def do_login(self): with self.client.get(self.login_url, catch_response=True): cookies = dict_from_cookiejar(self.client.cookiejar) csrf_middleware_token = cookies["csrftoken"] @@ -44,6 +48,49 @@ def json_rpc(self, rpc_method, rpc_args): return self.client.post("/json-rpc/", json=payload).json()["result"] +class BrowserTestCase(PlaywrightUser, LoggedInTestCase): + """ + Required setup: + pip uninstall trio + playwright install firefox + """ + + abstract = True + browser_type = "firefox" + multiplier = 10 + wait_time = between(1, 5) + session_cookie = None + + def do_login(self): + pass + + async def _pwprep(self): + await super()._pwprep() + + # login via the browser + browser_context = await self.browser.new_context( + ignore_https_errors=True, base_url=self.host + ) + page = await browser_context.new_page() + page.set_default_timeout(60000) + + await page.goto(self.login_url) + await page.wait_for_load_state() + + await page.locator("#inputUsername").fill(self.username) + await page.locator("#inputPassword").fill(self.password) + await page.get_by_role("button").click() + + # store this for later use b/c @pw creates + # a new context & page for every task! + for cookie in await page.context.cookies(): + if cookie["name"] == "sessionid": + self.session_cookie = cookie + + await page.close() + await browser_context.close() + + class ExampleTestCase(LoggedInTestCase): wait_time = between(1, 5) diff --git a/tests/performance/web_simulation_test.py b/tests/performance/web_simulation_test.py new file mode 100644 index 0000000000..250b7c5cae --- /dev/null +++ b/tests/performance/web_simulation_test.py @@ -0,0 +1,24 @@ +# Copyright (c) 2025 Alexander Todorov +# +# Licensed under GNU Affero General Public License v3 or later (AGPLv3+) +# https://www.gnu.org/licenses/agpl-3.0.html + +from datetime import datetime + +from base import BrowserTestCase +from locust import task +from locust.exception import StopUser +from locust_plugins.users.playwright import event, pw + + +class UserActionsTestCase(BrowserTestCase): + @task + @pw + async def visit_cases_search_page(self, page): + await page.context.add_cookies([self.session_cookie]) + + await page.goto("/cases/search/") + await page.wait_for_load_state() + + html = await page.content() + print(html) diff --git a/tests/test_http.sh b/tests/test_http.sh index d6741b9ded..43957118e6 100755 --- a/tests/test_http.sh +++ b/tests/test_http.sh @@ -191,6 +191,7 @@ _EOF_ # this is designed to check that these files don't crash, rlRun -t -c "locust --headless --users 1 --spawn-rate 1 --run-time 5s -H https://localhost/ --locustfile tests/performance/base.py" rlRun -t -c "locust --headless --users 1 --spawn-rate 1 --run-time 5s -H https://localhost/ --locustfile tests/performance/api_write_test.py" + rlRun -t -c "locust --headless --users 1 --spawn-rate 1 --run-time 5s -H https://localhost/ --locustfile tests/performance/web_simulation_test.py" rlPhaseEnd rlPhaseStartCleanup