What are the benefits of using fixtures with the page objects model in Playwright?

During our popular AMA on Playwright with @utchbe, a community member asked this thoughtful question on testing methodologies within Playwright:

“How can fixtures be effectively used alongside the page objects model in Playwright, and what are the advantages of implementing both?”

This query delves into the strategic integration of two concepts in automated testing, seeking insights into their combined efficacy. We’re keen to hear your experiences and insights on this. Have you implemented these techniques in your projects, and what advantages have you observed? Your shared knowledge could be invaluable to other community members.

1 Like

I was going to write an answer for this but the documentation honestly explains it way better than I ever could - Playwright-Fixtures

You will find a list of the pros along with some lovely code examples!

1 Like

Problem: I’ve seen a limitation of Playwright storageState management. One can’t manipulate it that easily with built-in controls/APIs.

Context:

  • In an application I’ve been automating against, my user session is handled by the server, with a few cookies/storage information locally. It updates the session token every 60s. This means storageState needs to update each app request doe after 60s since the last token update.
  • The initial Playwright ‘setup’ would only set the storageState once, I need it updated many times for the same user.

Solutions:

  • I tried a fixture as recommended by a few people online(Playwright Git and elsewhere): I check if the access to the login page would redirect the user to the app or not. If not log in.
  • But this was flaky, and slowing down due to navigations and redirect waits. So I tried API login, except that’s different in terms of session management than UI login - so sessions don’t work similarly;
  • I then switched to using a fixture where I store updated storageState after each ‘test’;

Currently, I’m still stuck with one user and 1 worker. It would be interesting to see how to manage 10-12 users and 4 workers later.

If anyone has any experience and suggestions on working with storageState & fixtures I’d be interested.