Skip to content

Commit

Permalink
Add an example test case with Locust + Playwright
Browse files Browse the repository at this point in the history
Refs #721
  • Loading branch information
atodorov committed Jan 23, 2025
1 parent 0e0cea0 commit 45a1a98
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements/devel.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ isort==5.13.2
colorama
black==24.10.0
locust
locust-plugins[playwright]
parameterized
robotframework
robotframework-seleniumlibrary
Expand Down
47 changes: 47 additions & 0 deletions tests/performance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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"]
Expand Down Expand Up @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions tests/performance/web_simulation_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2025 Alexander Todorov <[email protected]>
#
# 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)
1 change: 1 addition & 0 deletions tests/test_http.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 45a1a98

Please sign in to comment.