|
| 1 | +import { expect, test, chromium } from '@playwright/test'; |
| 2 | + |
| 3 | +const chromium_flags = ['--enable-features=SharedArrayBuffer']; |
| 4 | + |
| 5 | +const iframe_selector = 'iframe[src*="webcontainer.io/"]'; |
| 6 | + |
| 7 | +test('.env file: no timeout error occurs when switching a tutorials without a .env file to one with it', async () => { |
| 8 | + const context = await chromium.launchPersistentContext('', { args: chromium_flags }); |
| 9 | + const page = context.pages()[0]; |
| 10 | + await page.bringToFront(); |
| 11 | + |
| 12 | + await page.goto('/tutorial/welcome-to-svelte'); |
| 13 | + |
| 14 | + const iframe_locator = page.frameLocator(iframe_selector); |
| 15 | + |
| 16 | + // wait for the iframe to load |
| 17 | + await iframe_locator.getByText('Welcome!').waitFor(); |
| 18 | + |
| 19 | + // switch to another tutorial with a .env file |
| 20 | + await page.click('header > h1', { delay: 200 }); |
| 21 | + await page.locator('button', { hasText: 'Part 4: Advanced SvelteKit' }).click({ delay: 200}); |
| 22 | + await page.locator('button', { hasText: 'Environment variables' }).click({ delay: 200}); |
| 23 | + await page.locator('a', { hasText: '$env/static/private' }).click({ delay: 200}); |
| 24 | + |
| 25 | + // wait for the iframe to load |
| 26 | + await iframe_locator.getByText('enter the passphrase').waitFor(); |
| 27 | + |
| 28 | + // wait for a bit, because when Vite dev server is restarted, learn.svelte.dev |
| 29 | + // will wait for 10 seconds, after which time a timeout error will occur. |
| 30 | + await page.waitForTimeout(11000); |
| 31 | + |
| 32 | + // expect no timeout error |
| 33 | + await expect(page.getByText('Yikes!')).toBeHidden(); |
| 34 | + await expect(iframe_locator.getByText('enter the passphrase')).toBeVisible(); |
| 35 | + |
| 36 | + await context.close(); |
| 37 | +}); |
| 38 | + |
| 39 | +test('.env file: environment variables are available when switching a tutorial without a .env file to one with it', async () => { |
| 40 | + const context = await chromium.launchPersistentContext('', { args: chromium_flags }); |
| 41 | + const page = context.pages()[0]; |
| 42 | + await page.bringToFront(); |
| 43 | + |
| 44 | + await page.goto('/tutorial/welcome-to-svelte'); |
| 45 | + |
| 46 | + const iframe_locator = page.frameLocator(iframe_selector); |
| 47 | + |
| 48 | + // wait for the iframe to load |
| 49 | + await iframe_locator.getByText('Welcome!').waitFor(); |
| 50 | + |
| 51 | + // switch to another tutorial with a .env file |
| 52 | + await page.click('header > h1', { delay: 200 }); |
| 53 | + await page.locator('button', { hasText: 'Part 4: Advanced SvelteKit' }).click({ delay: 200}); |
| 54 | + await page.locator('button', { hasText: 'Environment variables' }).click({ delay: 200}); |
| 55 | + await page.locator('a', { hasText: '$env/dynamic/private' }).click({ delay: 200}); |
| 56 | + |
| 57 | + // wait for the iframe to load |
| 58 | + await iframe_locator.getByText('enter the passphrase').waitFor(); |
| 59 | + await iframe_locator.locator('input[name="passphrase"]').waitFor(); |
| 60 | + |
| 61 | + await page.waitForTimeout(500); |
| 62 | + |
| 63 | + // login |
| 64 | + // 'open sesame' is the environment variables loaded from `.env` file |
| 65 | + await iframe_locator.locator('input[name="passphrase"]').fill('open sesame'); |
| 66 | + await page.keyboard.press('Enter', { delay: 200 }); |
| 67 | + |
| 68 | + // expect to be able to login. |
| 69 | + // Being able to log in means that environment variables are loaded. |
| 70 | + await expect(iframe_locator.getByText('wrong passphrase!')).toBeHidden(); |
| 71 | + await expect(iframe_locator.locator('button', { hasText: 'log out'})).toBeEnabled(); |
| 72 | + |
| 73 | + await context.close(); |
| 74 | +}); |
0 commit comments