Playwright provides a way to reuse the signed-in state in the tests. That way you can log in only once and then skip the log in step for all of the tests.
In this video, Jared from commit quality demonstrates how to log in once before all tests and use that state using Playwright's storage state capabilities. This is beneficial for test automation since it can significantly reduce the amount of time and flakiness associated with logging in before each test.
To get started, Jared creates a basic test that logs into the demo Blaze website and checks if the log out button is visible. He then duplicates the test, removing the login functionality, and demonstrates that it will fail as it hasn't logged in. However, using Playwright's storage state capabilities, we don't have to log in before each test.
Jared goes on to create a global setup file which is a function that will run once before all tests. Inside this function, he sets up the Playwright browser, context, and page, and then logs in. After logging in, he saves the state of the web page as a JSON file called login_auth.json
.
Next, Jared updates the Playwright config to point to the global setup file and to use the stored state from the login_auth.json
file. When running the tests, the state from the JSON file is used, and all tests pass without having to log in before each individual test.
Jared also demonstrates how to use the code generator to autogenerate the auth.json file and how to switch between different state files for various use cases or environments.
In conclusion, using Playwright's storage state capabilities allows for more efficient and stable test automation by logging in once and reusing that state across all tests. This can save time and reduce the risk of flakiness in test automation.
Key Takeaways:
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].