Skip to content

Instantly share code, notes, and snippets.

@oofnivek
Last active June 16, 2025 14:52
Show Gist options
  • Select an option

  • Save oofnivek/13a7c718f9ddd7068a75dffef09053c8 to your computer and use it in GitHub Desktop.

Select an option

Save oofnivek/13a7c718f9ddd7068a75dffef09053c8 to your computer and use it in GitHub Desktop.
import { test, expect } from '@playwright/test';
import * as fs from 'fs';
import * as path from 'path';
const TOKEN_PATH = '../playwright/.auth/user.json'
let access_token: string; // Declare accessToken
// This hook runs before all tests in THIS specific file
test.beforeAll(async () => {
// Read the token from the file saved by global setup
const authFile = path.join(__dirname, TOKEN_PATH); // Adjust path if needed
try {
const authData = JSON.parse(fs.readFileSync(authFile, 'utf-8'));
access_token = authData.access_token;
expect(access_token).toBeTruthy(); // Ensure token is loaded
} catch (error) {
console.error('Failed to load access token from global setup file:', error);
throw new Error('Authentication token not available. Global setup might have failed.');
}
});
test('Go to localhost, get 200', async ({ request }) => {
const response = await request.get(`/hello_world/`, {
headers: {
'Authorization': `Bearer ${access_token}`, // Assuming Bearer token for subsequent calls
},
}
);
expect(response.status()).toBe(200);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment