This folder contains example implementations showing how to use playwright-ms-auth in your projects.
A helper module for Microsoft/SharePoint authentication with convenient utility functions.
Features:
- ✅ Simple login function
- ✅ Check for valid authentication
- ✅ Get auth file path
- ✅ Clear saved authentication
- ✅ CLI execution support
Usage:
import { loginToMicrosoft, hasValidAuth, getAuthFilePath } from "./msLogin";
// Login
await loginToMicrosoft();
// Check if authenticated
const isAuthenticated = await hasValidAuth();
// Get storage state path
const authPath = await getAuthFilePath();CLI Usage:
# Run from examples directory
cd examples
npm install
# Headless mode (default)
npm run login
# Headful mode (visible browser window)
npm run login -- --headfulTemplate for environment variables configuration. Copy to .env and fill in your credentials.
Your actual credentials (
-
Install dependencies:
npm install playwright-ms-auth dotenv npm install -D tsx @types/node
-
Configure environment:
cp .env.example .env # Edit .env with your credentials -
Run authentication:
npx tsx msLogin.ts
-
Use in your tests:
import { test } from "@playwright/test"; import { getAuthFilePath } from "./msLogin"; test.use({ storageState: await getAuthFilePath(), }); test("authenticated test", async ({ page }) => { await page.goto("https://yourcompany.sharepoint.com"); // Already authenticated! });
MS_AUTH_EMAIL- Your Microsoft emailMS_SHAREPOINT_URL- SharePoint URL to authenticate againstMS_USER_PASSWORD- Your password (for simple auth)
MS_AUTH_CREDENTIAL_TYPE-passwordorcertificate(default: password)MS_AUTH_CREDENTIAL_PROVIDER- Provider type (default: environment)MS_AUTH_OUTPUT_DIR- Custom output directoryMS_AUTH_WAIT_FOR_MSAL_TOKENS- Wait for MSAL tokens (default: true)MS_AUTH_MSAL_TOKEN_TIMEOUT- MSAL token timeout in ms (default: 30000)SYSTEM_DEBUG- Enable debug logging
- Never commit
.envfiles with real credentials - Use Azure KeyVault for production environments
- The password method is suitable for development/testing only
Authentication fails:
- Verify your credentials in
.env - Check the SharePoint URL is correct
- Enable debug mode:
SYSTEM_DEBUG=true
Storage state not found:
- Run
npx tsx msLogin.tsfirst to authenticate - Check the auth file path with
getAuthFilePath()
Module not found:
- Install dependencies:
npm install playwright-ms-auth dotenv - For TypeScript:
npm install -D tsx @types/node
MSAL tokens not found in localStorage:
- For SPAs like Power Platform, the library automatically waits for MSAL tokens
- If your app takes longer than 30 seconds, increase the timeout:
MS_AUTH_MSAL_TOKEN_TIMEOUT=60000 npm run login
- Enable debug mode to see which tokens were found:
SYSTEM_DEBUG=true npm run login