|
| 1 | +import { expect, test, chromium } from '@playwright/test'; |
| 2 | + |
| 3 | +const chromium_flags = ['--enable-features=SharedArrayBuffer']; |
| 4 | + |
| 5 | +const editor_selector = 'div.monaco-scrollable-element.editor-scrollable'; |
| 6 | +const editor_focus_selector = 'textarea.inputarea.monaco-mouse-cursor-text'; |
| 7 | +const iframe_selector = 'iframe[src*="webcontainer.io/"]'; |
| 8 | + |
| 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]; |
| 12 | + await page.bringToFront(); |
| 13 | + |
| 14 | + await page.goto('/tutorial/your-first-component'); |
| 15 | + |
| 16 | + // first, focus the editor before the iframe is loaded |
| 17 | + await page.locator(editor_selector).click({ delay: 1000 }); |
| 18 | + |
| 19 | + // at this time, expect focus to be on the editor |
| 20 | + await expect(page.locator(editor_focus_selector)).toBeFocused(); |
| 21 | + |
| 22 | + // wait for the iframe to load |
| 23 | + await page.frameLocator(iframe_selector).getByText('Hello world!').waitFor(); |
| 24 | + |
| 25 | + // wait a little, because there may be a script that manipulates focus |
| 26 | + await page.waitForTimeout(1000); |
| 27 | + |
| 28 | + // expect focus to be on the editor |
| 29 | + await expect(page.locator(editor_focus_selector)).toBeFocused(); |
| 30 | + |
| 31 | + await context.close(); |
| 32 | +}); |
| 33 | + |
| 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]; |
| 37 | + await page.bringToFront(); |
| 38 | + |
| 39 | + await page.goto('/tutorial/named-form-actions'); |
| 40 | + |
| 41 | + const iframe = page.frameLocator(iframe_selector); |
| 42 | + |
| 43 | + // wait for the iframe to load |
| 44 | + await iframe.getByText('todos').waitFor(); |
| 45 | + |
| 46 | + // first, focus the editor |
| 47 | + await page.locator(editor_selector).click({ delay: 1000 }); |
| 48 | + await expect(page.locator(editor_focus_selector)).toBeFocused(); |
| 49 | + |
| 50 | + // then, click a input in the iframe |
| 51 | + const input = iframe.locator('input[name="description"]'); |
| 52 | + await input.click({ delay: 500 }); |
| 53 | + |
| 54 | + // wait a little, because there may be a script that manipulates focus |
| 55 | + await page.waitForTimeout(1000); |
| 56 | + |
| 57 | + // expect focus to be on the input in the iframe, not the editor. |
| 58 | + await expect(input).toBeFocused(); |
| 59 | + await expect(page.locator(editor_focus_selector)).not.toBeFocused(); |
| 60 | + |
| 61 | + await context.close(); |
| 62 | +}); |
| 63 | + |
| 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]; |
| 67 | + await page.bringToFront(); |
| 68 | + |
| 69 | + await page.goto('/tutorial/layouts'); |
| 70 | + |
| 71 | + const iframe = page.frameLocator(iframe_selector); |
| 72 | + |
| 73 | + // wait for the iframe to load |
| 74 | + await iframe.getByText('this is the home page.').waitFor(); |
| 75 | + |
| 76 | + // first, focus the editor |
| 77 | + await page.locator(editor_selector).click({ delay: 1000 }); |
| 78 | + await expect(page.locator(editor_focus_selector)).toBeFocused(); |
| 79 | + |
| 80 | + // then, click a link in the iframe |
| 81 | + await iframe.locator('a[href="/about"]').click({ delay: 500 }); |
| 82 | + |
| 83 | + // wait for navigation |
| 84 | + await iframe.getByText('this is the about page.').waitFor(); |
| 85 | + |
| 86 | + // wait a little, because there may be a script that manipulates focus |
| 87 | + await page.waitForTimeout(1000); |
| 88 | + |
| 89 | + // expect focus to be on body in the iframe, not the editor. |
| 90 | + await expect(iframe.locator('body')).toBeFocused(); |
| 91 | + |
| 92 | + await context.close(); |
| 93 | +}); |
| 94 | + |
| 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]; |
| 98 | + await page.bringToFront(); |
| 99 | + |
| 100 | + await page.goto('/tutorial/your-first-component'); |
| 101 | + |
| 102 | + // wait for the iframe to load |
| 103 | + await page.frameLocator(iframe_selector).getByText('Hello world!').waitFor(); |
| 104 | + |
| 105 | + // first, write script tag |
| 106 | + const code = '<script>\n\n</script>\n'; |
| 107 | + await page.locator(editor_focus_selector).fill(code); |
| 108 | + |
| 109 | + // move the cursor into the script tag |
| 110 | + await page.keyboard.press('PageUp', { delay: 500 }); |
| 111 | + await page.keyboard.press('ArrowDown', { delay: 500 }); |
| 112 | + |
| 113 | + // wait a little because the above operation is flaky |
| 114 | + await page.waitForTimeout(500); |
| 115 | + |
| 116 | + // type the code as a person would do it manually |
| 117 | + await page.keyboard.type(` export let data;`, { delay: 100 }); |
| 118 | + |
| 119 | + // wait a little, because there may be a script that manipulates focus |
| 120 | + await page.waitForTimeout(1000); |
| 121 | + |
| 122 | + // get code from DOM, then replace nbsp with normal space |
| 123 | + const received = (await page.locator(editor_selector).innerText()).replace(/\u00a0/g, ' '); |
| 124 | + |
| 125 | + const expected = '<script>\n export let data;\n</script>\n<h1>Hello world!</h1>'; |
| 126 | + |
| 127 | + expect(received).toBe(expected); |
| 128 | + |
| 129 | + await context.close(); |
| 130 | +}); |
0 commit comments