|
| 1 | +// tests/helpers/sandbox.js |
| 2 | +import { request, test as base } from '@playwright/test'; |
| 3 | + |
| 4 | +async function setupSandbox(context: any) { |
| 5 | + // Create sandbox |
| 6 | + const requestContext = await request.newContext(); |
| 7 | + const response = await requestContext.post('http://localhost:4000/sandbox', { |
| 8 | + headers: { |
| 9 | + 'Cache-Control': 'no-store' |
| 10 | + } |
| 11 | + }); |
| 12 | + |
| 13 | + const sessionId = await response.text(); |
| 14 | + |
| 15 | + // Set up the route interception to add sessionId to all requests |
| 16 | + await context.route('**/*', async (route: any, request: any) => { |
| 17 | + const headers = request.headers(); |
| 18 | + headers['x-session-id'] = sessionId; |
| 19 | + await route.continue({ headers }); |
| 20 | + }); |
| 21 | + |
| 22 | + // Store the sessionId for LiveView connections |
| 23 | + await context.addInitScript(({ sessionId }) => { |
| 24 | + window.sessionId = sessionId; |
| 25 | + }, { sessionId }); |
| 26 | + |
| 27 | + return sessionId; |
| 28 | +} |
| 29 | + |
| 30 | +async function teardownSandbox(sessionId: any) { |
| 31 | + const requestContext = await request.newContext(); |
| 32 | + await requestContext.delete('http://localhost:4000/sandbox', { |
| 33 | + headers: { |
| 34 | + 'x-session-id': sessionId |
| 35 | + } |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +// module.exports = { setupSandbox, teardownSandbox }; |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +const test = base.extend({ |
| 44 | + context: async ({ context }, use) => { |
| 45 | + const sessionId = await setupSandbox(context); |
| 46 | + console.log("sessionId is >>>>>>>>>>: ", sessionId) |
| 47 | + await use(context); |
| 48 | + await teardownSandbox(sessionId); |
| 49 | + } |
| 50 | +}); |
| 51 | + |
| 52 | +const expect = base.expect |
| 53 | + |
| 54 | +export { |
| 55 | + test, |
| 56 | + expect |
| 57 | +} |
0 commit comments