from locust import task
from locust_plugins.users.playwright import PlaywrightUser, PageWithRetry, pw, event
class NavigationUser(PlaywrightUser):
@task(3)
@pw
async def home_to_products_flow(self, page: PageWithRetry):
"""Navigate from home to products section"""
async with event(self, "Homepage Load"):
await page.goto("/")
async with event(self, "Products Page"):
async with page.expect_navigation():
await page.click('a:has-text("Products")')
async with event(self, "Product Detail"):
async with page.expect_navigation():
await page.click('.product-item:first-child a')
@task(2)
@pw
async def about_contact_flow(self, page: PageWithRetry):
"""Navigate through about and contact pages"""
async with event(self, "About Page"):
await page.goto("/about")
async with event(self, "Contact Page"):
async with page.expect_navigation():
await page.click('a[href="/contact"]')
# Test different navigation methods
async with event(self, "Services Page"):
async with page.expect_navigation():
await page.click('nav ul li:nth-child(2) a')
@task(1)
@pw
async def complex_navigation_flow(self, page: PageWithRetry):
"""Test complex multi-step navigation"""
# Start from homepage
async with event(self, "Homepage"):
await page.goto("/")
# Navigate to blog
async with event(self, "Blog Section"):
async with page.expect_navigation():
await page.click('//nav//a[contains(., "Blog")]')
# Click on first blog post
async with event(self, "Blog Post"):
async with page.expect_navigation():
await page.click('.blog-post:first-child .read-more')
# Navigate back using browser back
async with event(self, "Back to Blog"):
await page.go_back()
# Navigate to homepage using logo
async with event(self, "Back to Home"):
async with page.expect_navigation():
await page.click('.logo')
@task(1)
@pw
async def search_and_navigate(self, page: PageWithRetry):
"""Test search functionality and result navigation"""
async with event(self, "Search Page"):
await page.goto("/")
# Perform search
await page.fill('input[name="search"]', 'test query')
async with event(self, "Search Results"):
async with page.expect_navigation():
await page.click('button:has-text("Search")')
# Click on first result
if await page.locator('.search-result').count() > 0:
async with event(self, "Search Result Detail"):
async with page.expect_navigation():
await page.click('.search-result:first-child a')