This project demonstrates various strategies for handling user authentication and sessions in Playwright tests. It provides examples of different approaches to logging in, from global setup to custom fixtures, allowing you to choose the best method for your testing needs.
Before you begin, ensure you have the following installed:
-
Clone the repository:
git clone <repository_url> cd LOGIN_SETUP_ALL
-
Install dependencies:
npm install
-
Install Playwright browsers:
npx playwright install
-
Environment Variables This project uses
dotenvto manage environment variables. Create a.envfile in the root of the project and add the necessary variables. For example:BASE_URL=https://automationexercise.com/
This project showcases several different ways to handle logins in Playwright:
This method uses a global setup file to authenticate a user once before any tests are run. The authentication state (like cookies and local storage) is then saved to a file and reused by the tests.
- Relevant Files:
global-setup.ts,playwright.config.ts(seeglobalSetupandprojectsusingstorageState),tests/Global.spec.ts - How it works: The
globalSetupfunction inplaywright.config.tspoints toglobal-setup.ts. This script launches a browser, performs the login, and saves the context's storage state to a JSON file (e.g.,playwright/.auth/admin-auth.json). The projects inplaywright.config.tsare then configured to use thisstorageStatefor all tests. - Run the tests:
npm run test_SGlobal npm run test_AGlobal
This approach uses Playwright's "projects" feature to run the same test file with different authentication setups for different user roles (e.g., 'admin' and 'standard user'). A custom fixture is used to handle the login for each role.
- Relevant Files:
playwright.config.ts(seeprojectswithuserRole),authFixture.ts,tests/ProjectWay.spec.ts - How it works: The
playwright.config.tsfile defines different projects, each with a specificuserRolein itsuseoptions. TheauthFixture.tsfile defines a test fixture that performs a login based on thisuserRolebefore each test run. - Run the tests:
npm run test_Standard npm run test_Admin
This method defines a fixture directly within the test file. The fixture is responsible for logging in the user, and tests can then use this fixture to run in an authenticated state.
- Relevant Files:
tests/login.spec.ts - How it works: The test file extends the base
testobject from Playwright to create a newloggedInUserfixture. This fixture contains the logic to navigate to the login page, fill in credentials, and log in. Tests that need to be authenticated can then request this fixture. - Run the tests:
npm run test_SLogin npm run test_ALogin
This is similar to the project-based approach but uses a more generic custom fixture defined in a separate file.
- Relevant Files:
fixtures.ts,tests/CustomFixtureLogin.spec.ts - How it works: The
fixtures.tsfile defines a custom test fixture that handles the login process. Test files can then import this customtestobject instead of the one from@playwright/test. - Run the tests:
npm run test_SCustomLogin npm run test_ACustomLogin
This method uses a beforeAll hook within a test suite to run a login function once before all the tests in that suite are executed.
- Relevant Files:
loginUtils.ts,tests/Way1example.spec.ts - How it works: A
beforeAllblock at the top of the test suite calls a helper function (loginAndSaveSessionfromloginUtils.ts) that performs the login and saves the session state. - Run the test:
npm run test_way1
This method involves running a separate script to perform the login before running the Playwright test command.
- Relevant Files:
loginUtils.ts,loginUtils.js,tests/Way2example.spec.ts,package.json(seetest_way2script) - How it works: The
test_way2script inpackage.jsonfirst compilesloginUtils.tsto JavaScript, then executes the resultingloginUtils.jsfile (which performs the login and saves the state), and finally runs the Playwright tests. - Run the test:
npm run test_way2
You can run the tests for each strategy using the npm scripts defined in package.json:
# Run tests for Global Setup
npm run test_SGlobal
npm run test_AGlobal
# Run tests for Project-Based Authentication
npm run test_Standard
npm run test_Admin
# Run tests for Test-Level Fixture
npm run test_SLogin
npm run test_ALogin
# Run tests for Custom Fixture
npm run test_SCustomLogin
npm run test_ACustomLogin
# Run tests for beforeAll Hook
npm run test_way1
# Run tests for Pre-Test Script
npm run test_way2After running the tests, a detailed HTML report will be generated in the playwright-report directory. You can view the report by opening the index.html file in your browser:
npx playwright show-report