Logging in via the UI and then reusing authentication state can be combined to
implement **login once and run multiple scenarios**. The lifecycle looks like:
1. Run tests (for example, with `npm run test`).
1. Login via UI and retrieve authentication state.
* In Jest, this can be executed in [`globalSetup`](https://jestjs.io/docs/en/configuration#globalsetup-string).
1. In each test, load authentication state in `beforeEach` or `beforeAll` step.
This approach will also **work in CI environments**, since it does not rely
on any external state.
### Example
[This example script](examples/authentication.js) logs in on GitHub.com with
Chromium, and then reuses the logged in cookie state in WebKit.
### API reference
- [BrowserContext]
- [`method: BrowserContext.cookies`]
- [`method: BrowserContext.addCookies`]
- [`method: Page.evaluate`]
- [`method: BrowserContext.addInitScript`]
## Multi-factor authentication
Accounts with multi-factor authentication (MFA) cannot be fully automated, and need
manual intervention. Persistent authentication can be used to partially automate
MFA scenarios.
### Persistent authentication
Web browsers use a directory on disk to store user history, cookies, IndexedDB
and other local state. This disk location is called the [User data directory](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md).
Note that persistent authentication is not suited for CI environments since it
relies on a disk location. User data directories are specific to browser types
and cannot be shared across browser types.
User data directories can be used with the `launchPersistentContext` API.