|
| 1 | +import type { Page } from '@playwright/test'; |
| 2 | +import { test, expect } from '../../playwright'; |
| 3 | +import type { PlaygroundComponent } from '../../components/playground.component'; |
| 4 | +import type { RequestPage } from '../../pages/request.page'; |
| 5 | +import type { CodeEditorComponent } from '../../components/code-editor/code-editor.component'; |
| 6 | + |
| 7 | +const markPageLoad = (page: Page) => page.evaluate(() => { |
| 8 | + (window as unknown as { __sameLoad: boolean }).__sameLoad = true; |
| 9 | +}); |
| 10 | + |
| 11 | +const isSamePageLoad = (page: Page) => |
| 12 | + page.evaluate(() => (window as unknown as { __sameLoad?: boolean }).__sameLoad === true); |
| 13 | + |
| 14 | +const closeAndReopen = async (page: Page, playground: PlaygroundComponent, requestPage: RequestPage): Promise<void> => { |
| 15 | + await markPageLoad(page); |
| 16 | + await playground.close(); |
| 17 | + await expect(playground.header).toHaveCount(0); |
| 18 | + await requestPage.urlBar.tryButton.click(); |
| 19 | + await expect(playground.header).toBeVisible(); |
| 20 | + await playground.view.waitFor({ state: 'visible' }); |
| 21 | + expect(await isSamePageLoad(page)).toBe(true); |
| 22 | +}; |
| 23 | + |
| 24 | +const setEditorScript = async (page: Page, editor: CodeEditorComponent, script: string): Promise<void> => { |
| 25 | + await editor.focus(); |
| 26 | + await page.keyboard.press('ControlOrMeta+a'); |
| 27 | + await page.keyboard.insertText(script); |
| 28 | +}; |
| 29 | + |
| 30 | +test.describe('Playground edits survive closing and reopening, not a reload', () => { |
| 31 | + test.beforeEach(async ({ page, requestPage, playground }) => { |
| 32 | + await page.goto('/?fixture=vars'); |
| 33 | + await requestPage.sidebar.open(['Customers', 'Variables Demo']); |
| 34 | + await requestPage.root.waitFor({ state: 'visible' }); |
| 35 | + await requestPage.urlBar.tryButton.click(); |
| 36 | + await playground.view.waitFor({ state: 'visible' }); |
| 37 | + await playground.envSwitcher.selectEnvironment('Dev'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('a typed secret environment value is still set after close and reopen', async ({ |
| 41 | + page, |
| 42 | + playground, |
| 43 | + requestPage |
| 44 | + }) => { |
| 45 | + await playground.variable.hoverInputToken('unsetSecret'); |
| 46 | + await playground.variable.editTo('typed-secret'); |
| 47 | + await expect(playground.variable.value).toHaveText('*'.repeat('typed-secret'.length)); |
| 48 | + |
| 49 | + await closeAndReopen(page, playground, requestPage); |
| 50 | + |
| 51 | + await playground.variable.hoverInputToken('unsetSecret'); |
| 52 | + await playground.variable.revealToggle.click(); |
| 53 | + await expect(playground.variable.value).toHaveText('typed-secret'); |
| 54 | + }); |
| 55 | + |
| 56 | + test('an edited environment value and request URL are kept after close and reopen', async ({ |
| 57 | + page, |
| 58 | + playground, |
| 59 | + requestPage |
| 60 | + }) => { |
| 61 | + await playground.variable.hoverInputToken('host'); |
| 62 | + await playground.variable.editTo('https://edited.example.com'); |
| 63 | + await expect(playground.variable.value).toHaveText('https://edited.example.com'); |
| 64 | + |
| 65 | + await playground.urlInput.click(); |
| 66 | + await page.keyboard.press('End'); |
| 67 | + await page.keyboard.type('&edited=1'); |
| 68 | + await expect(playground.urlInput).toHaveValue(/&edited=1$/); |
| 69 | + |
| 70 | + await closeAndReopen(page, playground, requestPage); |
| 71 | + |
| 72 | + await expect(playground.urlInput).toHaveValue(/&edited=1$/); |
| 73 | + await playground.variable.hoverInputToken('host'); |
| 74 | + await expect(playground.variable.value).toHaveText('https://edited.example.com'); |
| 75 | + }); |
| 76 | + |
| 77 | + test('an edit made in the Environments view is kept after close and reopen', async ({ |
| 78 | + page, |
| 79 | + playground, |
| 80 | + requestPage, |
| 81 | + envEditor |
| 82 | + }) => { |
| 83 | + const envTable = playground.keyValueTable; |
| 84 | + await playground.openEnvironments(); |
| 85 | + await envEditor.selectEnvironment('Dev'); |
| 86 | + await expect(envTable.nameInputs.first()).toHaveValue('host'); |
| 87 | + await envTable.valueInputs.first().fill('https://from-env-view.example.com'); |
| 88 | + |
| 89 | + await closeAndReopen(page, playground, requestPage); |
| 90 | + |
| 91 | + await playground.openEnvironments(); |
| 92 | + await envEditor.selectEnvironment('Dev'); |
| 93 | + await expect(envTable.valueInputs.first()).toHaveValue('https://from-env-view.example.com'); |
| 94 | + }); |
| 95 | + |
| 96 | + test('a folder settings edit is kept after close and reopen', async ({ page, playground, requestPage }) => { |
| 97 | + await playground.openTreeItem(['Customers']); |
| 98 | + await playground.folderSettingsTab('variables').click(); |
| 99 | + await expect(playground.preRequestVars.nameInputs.first()).toHaveValue('folderScope'); |
| 100 | + await playground.preRequestVars.valueInputs.first().fill('edited-folder'); |
| 101 | + |
| 102 | + await closeAndReopen(page, playground, requestPage); |
| 103 | + |
| 104 | + await playground.openTreeItem(['Customers']); |
| 105 | + await playground.folderSettingsTab('variables').click(); |
| 106 | + await expect(playground.preRequestVars.valueInputs.first()).toHaveValue('edited-folder'); |
| 107 | + }); |
| 108 | + |
| 109 | + test('a collection settings edit is kept after close and reopen', async ({ |
| 110 | + page, |
| 111 | + playground, |
| 112 | + requestPage, |
| 113 | + collectionSettings |
| 114 | + }) => { |
| 115 | + await playground.ensureSidebarOpen(); |
| 116 | + await collectionSettings.open(); |
| 117 | + await collectionSettings.openTab('variables'); |
| 118 | + await expect(playground.preRequestVars.nameInputs.first()).toHaveValue('apiVersion'); |
| 119 | + await playground.preRequestVars.valueInputs.first().fill('2099-01'); |
| 120 | + |
| 121 | + await closeAndReopen(page, playground, requestPage); |
| 122 | + |
| 123 | + await playground.ensureSidebarOpen(); |
| 124 | + await collectionSettings.open(); |
| 125 | + await collectionSettings.openTab('variables'); |
| 126 | + await expect(playground.preRequestVars.valueInputs.first()).toHaveValue('2099-01'); |
| 127 | + }); |
| 128 | + |
| 129 | + test('a value set by a script with bru.setEnvVar is kept after close and reopen', async ({ |
| 130 | + page, |
| 131 | + playground, |
| 132 | + requestPage, |
| 133 | + responsePane |
| 134 | + }) => { |
| 135 | + await page.route('**/customers/**', (route) => |
| 136 | + route.fulfill({ status: 200, headers: { 'content-type': 'application/json' }, body: '{}' }) |
| 137 | + ); |
| 138 | + |
| 139 | + await playground.selectTab('scripts'); |
| 140 | + await playground.selectScriptTab('post-response'); |
| 141 | + await setEditorScript( |
| 142 | + page, |
| 143 | + playground.postResponseScriptEditor, |
| 144 | + 'bru.setEnvVar(\'host\', \'https://from-script.example.com\');' |
| 145 | + ); |
| 146 | + await responsePane.send(); |
| 147 | + |
| 148 | + await playground.variable.hoverInputToken('host'); |
| 149 | + await expect(playground.variable.value).toHaveText('https://from-script.example.com'); |
| 150 | + |
| 151 | + await closeAndReopen(page, playground, requestPage); |
| 152 | + |
| 153 | + await playground.variable.hoverInputToken('host'); |
| 154 | + await expect(playground.variable.value).toHaveText('https://from-script.example.com'); |
| 155 | + }); |
| 156 | + |
| 157 | + test('a reload restores the collection\'s original values', async ({ page, playground }) => { |
| 158 | + await playground.variable.hoverInputToken('unsetSecret'); |
| 159 | + await playground.variable.editTo('typed-secret'); |
| 160 | + await expect(playground.variable.value).toHaveText('*'.repeat('typed-secret'.length)); |
| 161 | + await playground.variable.hoverInputToken('host'); |
| 162 | + await expect(playground.variable.name).toHaveText('host'); |
| 163 | + await playground.variable.editTo('https://edited.example.com'); |
| 164 | + await expect(playground.variable.value).toHaveText('https://edited.example.com'); |
| 165 | + |
| 166 | + await page.reload(); |
| 167 | + await playground.runner.waitFor({ state: 'visible' }); |
| 168 | + await playground.openTreeItem(['Customers', 'Variables Demo']); |
| 169 | + await playground.view.waitFor({ state: 'visible' }); |
| 170 | + await playground.envSwitcher.selectEnvironment('Dev'); |
| 171 | + |
| 172 | + await playground.variable.hoverInputToken('host'); |
| 173 | + await expect(playground.variable.value).toHaveText('https://api.dev.example.com'); |
| 174 | + await playground.variable.hoverInputToken('unsetSecret'); |
| 175 | + await expect(playground.variable.name).toHaveText('unsetSecret'); |
| 176 | + await expect(playground.variable.value).toHaveText(''); |
| 177 | + }); |
| 178 | +}); |
0 commit comments