Skip to content

Commit d681398

Browse files
authored
chore: refactor playwright tests (sveltejs#229)
1 parent e9c1730 commit d681398

File tree

3 files changed

+27
-51
lines changed

3 files changed

+27
-51
lines changed

playwright.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const config: PlaywrightTestConfig = {
44
webServer: {
55
command: 'pnpm preview',
66
port: 4173
7-
}
7+
},
8+
timeout: 60000
89
};
910

1011
export default config;

tests/env_file.spec.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import { expect, test, chromium } from '@playwright/test';
2-
3-
const chromium_flags = ['--enable-features=SharedArrayBuffer'];
1+
import { expect, test } from '@playwright/test';
42

53
const iframe_selector = 'iframe[src*="webcontainer.io/"]';
64

7-
test('.env file: no timeout error occurs when switching a tutorials without a .env file to one with it', async () => {
8-
test.setTimeout(60000);
9-
10-
const context = await chromium.launchPersistentContext('', { args: chromium_flags });
11-
const page = context.pages()[0];
5+
test('.env file: no timeout error occurs when switching a tutorials without a .env file to one with it', async ({
6+
page
7+
}) => {
128
await page.bringToFront();
139

1410
await page.goto('/tutorial/welcome-to-svelte');
@@ -20,9 +16,9 @@ test('.env file: no timeout error occurs when switching a tutorials without a .e
2016

2117
// switch to another tutorial with a .env file
2218
await page.click('header > h1', { delay: 200 });
23-
await page.locator('button', { hasText: 'Part 4: Advanced SvelteKit' }).click({ delay: 200});
24-
await page.locator('button', { hasText: 'Environment variables' }).click({ delay: 200});
25-
await page.locator('a', { hasText: '$env/static/private' }).click({ delay: 200});
19+
await page.locator('button', { hasText: 'Part 4: Advanced SvelteKit' }).click({ delay: 200 });
20+
await page.locator('button', { hasText: 'Environment variables' }).click({ delay: 200 });
21+
await page.locator('a', { hasText: '$env/static/private' }).click({ delay: 200 });
2622

2723
// wait for the iframe to load
2824
await iframe_locator.getByText('enter the passphrase').waitFor();
@@ -34,13 +30,11 @@ test('.env file: no timeout error occurs when switching a tutorials without a .e
3430
// expect no timeout error
3531
await expect(page.getByText('Yikes!')).toBeHidden();
3632
await expect(iframe_locator.getByText('enter the passphrase')).toBeVisible();
37-
38-
await context.close();
3933
});
4034

41-
test('.env file: environment variables are available when switching a tutorial without a .env file to one with it', async () => {
42-
const context = await chromium.launchPersistentContext('', { args: chromium_flags });
43-
const page = context.pages()[0];
35+
test('.env file: environment variables are available when switching a tutorial without a .env file to one with it', async ({
36+
page
37+
}) => {
4438
await page.bringToFront();
4539

4640
await page.goto('/tutorial/welcome-to-svelte');
@@ -52,25 +46,22 @@ test('.env file: environment variables are available when switching a tutorial w
5246

5347
// switch to another tutorial with a .env file
5448
await page.click('header > h1', { delay: 200 });
55-
await page.locator('button', { hasText: 'Part 4: Advanced SvelteKit' }).click({ delay: 200});
56-
await page.locator('button', { hasText: 'Environment variables' }).click({ delay: 200});
57-
await page.locator('a', { hasText: '$env/dynamic/private' }).click({ delay: 200});
49+
await page.locator('button', { hasText: 'Part 4: Advanced SvelteKit' }).click({ delay: 200 });
50+
await page.locator('button', { hasText: 'Environment variables' }).click({ delay: 200 });
51+
await page.locator('a', { hasText: '$env/dynamic/private' }).click({ delay: 200 });
5852

5953
// wait for the iframe to load
6054
await iframe_locator.getByText('enter the passphrase').waitFor();
61-
await iframe_locator.locator('input[name="passphrase"]').waitFor();
6255

63-
await page.waitForTimeout(500);
56+
await page.waitForTimeout(3000);
6457

6558
// login
6659
// 'open sesame' is the environment variables loaded from `.env` file
6760
await iframe_locator.locator('input[name="passphrase"]').fill('open sesame');
68-
await page.keyboard.press('Enter', { delay: 200 });
61+
await page.keyboard.press('Enter', { delay: 1000 });
6962

7063
// expect to be able to login.
7164
// Being able to log in means that environment variables are loaded.
7265
await expect(iframe_locator.getByText('wrong passphrase!')).toBeHidden();
73-
await expect(iframe_locator.locator('button', { hasText: 'log out'})).toBeEnabled();
74-
75-
await context.close();
66+
await expect(iframe_locator.locator('button', { hasText: 'log out' })).toBeEnabled();
7667
});

tests/focus_management.spec.ts

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import { expect, test, chromium } from '@playwright/test';
2-
3-
const chromium_flags = ['--enable-features=SharedArrayBuffer'];
1+
import { expect, test } from '@playwright/test';
42

53
const editor_selector = 'div.monaco-scrollable-element.editor-scrollable';
64
const editor_focus_selector = 'textarea.inputarea.monaco-mouse-cursor-text';
75
const iframe_selector = 'iframe[src*="webcontainer.io/"]';
86

9-
test('focus management: the editor keeps focus when iframe is loaded', async () => {
10-
const context = await chromium.launchPersistentContext('', { args: chromium_flags });
11-
const page = context.pages()[0];
7+
test('focus management: the editor keeps focus when iframe is loaded', async ({ page }) => {
128
await page.bringToFront();
139

1410
await page.goto('/tutorial/your-first-component');
@@ -27,13 +23,9 @@ test('focus management: the editor keeps focus when iframe is loaded', async ()
2723

2824
// expect focus to be on the editor
2925
await expect(page.locator(editor_focus_selector)).toBeFocused();
30-
31-
await context.close();
3226
});
3327

34-
test('focus management: input inside the iframe gets focus when clicking it', async () => {
35-
const context = await chromium.launchPersistentContext('', { args: chromium_flags });
36-
const page = context.pages()[0];
28+
test('focus management: input inside the iframe gets focus when clicking it', async ({ page }) => {
3729
await page.bringToFront();
3830

3931
await page.goto('/tutorial/named-form-actions');
@@ -49,21 +41,19 @@ test('focus management: input inside the iframe gets focus when clicking it', as
4941

5042
// then, click a input in the iframe
5143
const input = iframe.locator('input[name="description"]');
52-
await input.click({ delay: 500 });
44+
await input.click({ delay: 1000 });
5345

5446
// wait a little, because there may be a script that manipulates focus
5547
await page.waitForTimeout(1000);
5648

5749
// expect focus to be on the input in the iframe, not the editor.
5850
await expect(input).toBeFocused();
5951
await expect(page.locator(editor_focus_selector)).not.toBeFocused();
60-
61-
await context.close();
6252
});
6353

64-
test('focus management: body inside the iframe gets focus when clicking a link inside the iframe', async () => {
65-
const context = await chromium.launchPersistentContext('', { args: chromium_flags });
66-
const page = context.pages()[0];
54+
test('focus management: body inside the iframe gets focus when clicking a link inside the iframe', async ({
55+
page
56+
}) => {
6757
await page.bringToFront();
6858

6959
await page.goto('/tutorial/layouts');
@@ -88,13 +78,9 @@ test('focus management: body inside the iframe gets focus when clicking a link i
8878

8979
// expect focus to be on body in the iframe, not the editor.
9080
await expect(iframe.locator('body')).toBeFocused();
91-
92-
await context.close();
9381
});
9482

95-
test('focus management: the editor keeps focus while typing', async () => {
96-
const context = await chromium.launchPersistentContext('', { args: chromium_flags });
97-
const page = context.pages()[0];
83+
test('focus management: The editor keeps focus while typing', async ({ page }) => {
9884
await page.bringToFront();
9985

10086
await page.goto('/tutorial/your-first-component');
@@ -114,7 +100,7 @@ test('focus management: the editor keeps focus while typing', async () => {
114100
await page.waitForTimeout(500);
115101

116102
// type the code as a person would do it manually
117-
await page.keyboard.type(` export let data;`, { delay: 100 });
103+
await page.keyboard.type(` export let data;`, { delay: 150 });
118104

119105
// wait a little, because there may be a script that manipulates focus
120106
await page.waitForTimeout(1000);
@@ -125,6 +111,4 @@ test('focus management: the editor keeps focus while typing', async () => {
125111
const expected = '<script>\n export let data;\n</script>\n<h1>Hello world!</h1>';
126112

127113
expect(received).toBe(expected);
128-
129-
await context.close();
130114
});

0 commit comments

Comments
 (0)