Strategy for using pytest + playwright to cycle through a large number of roles and pages
I'm looking for guidance. We have a site that has a number of roles, and pages each role can select. Multiplied out, this comes to 1,440 items to test. I have a playwright python solution that uses native playwright classes, and what I have works, but I'd like to see about solving this using pytest so that we can integrate with our CI/CD and take advantage of pytest's built-in features.
Generally, the flow would be:
- Log in
- Select the first role from the (dynamic) drop down
- Collect all possible pages (dynamic) this role can access
- Go to each page and assert on a status 200 and also the absence of an error
- Repeat until all pages for the role have been accessed
- Go to the next role until all roles have been selected
I don't know pytest very well, but it "feels" like there would be a general purpose test_ function that hangs on to logged-in context and cycles through a list. I started along the lines of a function to build a list of tuples after login, and then cycle through it: url_list = [('role_1', 'url_a'), ('role_1', 'url_b'), ('role_2', 'url_w'), ...]
I haven't had luck finding a guide through web search, partly because I'm not sure what to search for. Admittedly, I am very new to playwright, so there is a knowledge gap on my end as well. Is there a guide that helps handle a dynamic case like this? Thank you!
@thoolish You might want to check out the pytest plugin parameterized . This is with Django, so may not directly be suitable for your use case, but note how the 'parameterized.expand` decorator checks the return code for a given username:
class LocationProfileTestsParams(TestCase):
@parameterized.expand([(None, 302), ("bob", 200)])
def test_user_location_permissions(self, username: str, expect_response: int):
"""
Anonymous user cannot get project locations. Logged in users can.
"""
if username:
self.client.login(username=create_user(username).username, password="1234")
resp = self.client.get(reverse("clustermap"))
self.assertEqual(resp.status_code, expect_response)
This is more specific to Pytest and not to Playwright, see here for the Pytest docs: https://docs.pytest.org/en/6.2.x/parametrize.html