diff --git a/browse/src/write-commands.ts b/browse/src/write-commands.ts index 571957a69..b1178cd6d 100644 --- a/browse/src/write-commands.ts +++ b/browse/src/write-commands.ts @@ -996,13 +996,18 @@ export async function handleWriteCommand( } else if (args[i] === '--cleanup') { doCleanup = true; } else if (args[i] === '--hide' && i + 1 < args.length) { - // Collect all following non-flag args as selectors to hide + // Collect all following non-flag args as selectors to hide. A + // trailing image path is the output path, not a selector (#1419). + const values: string[] = []; i++; while (i < args.length && !args[i].startsWith('--')) { - hideSelectors.push(args[i]); + values.push(args[i]); i++; } i--; // Back up since the for loop will increment + const last = values[values.length - 1]; + if (!outputPath && values.length > 1 && /^(\/|\.\.?\/|~)|\.(png|jpe?g|webp)$/i.test(last)) outputPath = values.pop(); + hideSelectors.push(...values); } else if (args[i] === '--width' && i + 1 < args.length) { viewportWidth = parseInt(args[++i], 10); if (isNaN(viewportWidth)) throw new Error('--width must be a number'); diff --git a/browse/test/commands.test.ts b/browse/test/commands.test.ts index c0f72128f..b400ab60a 100644 --- a/browse/test/commands.test.ts +++ b/browse/test/commands.test.ts @@ -580,6 +580,20 @@ describe('Interaction', () => { expect(await handleReadCommand('js', ['document.querySelector("#name").value'], bm)).toBe(''); }); + test('prettyscreenshot --hide keeps a trailing image path as the output path (#1419)', async () => { + await handleWriteCommand('goto', [baseUrl + '/basic.html'], bm); + const out = path.join(os.tmpdir(), `pretty-hide-${process.pid}.png`); + fs.rmSync(out, { force: true }); + try { + const result = await handleWriteCommand('prettyscreenshot', ['--hide', '#title', '#content', out], bm); + expect(result).toContain(out); + expect(fs.existsSync(out)).toBe(true); + expect(await handleReadCommand('js', ['getComputedStyle(document.querySelector("#content")).display'], bm)).toBe('none'); + } finally { + fs.rmSync(out, { force: true }); + } + }); + test('type -- types text that starts with -- literally; plain text gets no hint', async () => { await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm); await handleWriteCommand('click', ['#bio'], bm);