Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/playwright-core/src/server/debugController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ function wireListeners(recorder: Recorder, debugController: DebugController) {
actions.push(action);
actionsChanged();
});
recorder.on(RecorderEvent.ModeChanged, (mode: Mode) => {
// Recording session has ended: drop the accumulated actions, so that the
// next session does not leak them into the emitted source (the client
// would re-insert the stale last action into the editor).
if (mode === 'none')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this include 'standby' as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, added — standby is what the toolbar's record toggle switches to when you pause, so it clears the list now as well.

One wrinkle: the protocol only accepts inspecting/recording/none, so the test drives the actual record button in the toolbar instead of calling setRecorderMode directly.

The firefox failure turned out to be my test's fault, not the fix — setContent is recorded as a goto('about:blank') there, so the second session was legitimately emitting one extra action. Switched that part to swapping innerHTML directly and it's green on both engines now.

actions.length = 0;
});
recorder.on(RecorderEvent.SignalAdded, (signal: actions.SignalInContext) => {
const lastAction = actions.findLast(a => a.pageGuid === signal.pageGuid);
if (lastAction)
Expand Down
33 changes: 33 additions & 0 deletions tests/library/debug-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,39 @@ test('test', async ({ page }) => {
});
});

test('should not leak actions from the previous recording session', async ({ backend, connectedBrowser }, testInfo) => {
testInfo.annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42218' });

const events: any[] = [];
backend.on('sourceChanged', event => events.push(event));

// Session 1: "Record at cursor" and record a click.
await backend.setRecorderMode({ mode: 'recording' }, undefined);
const context = await connectedBrowser.newContextForReuse();
const [page] = context.pages();
await page.setContent('<button>Submit</button>');
await page.getByRole('button', { name: 'Submit' }).click();
await expect.poll(() => events[events.length - 1]?.actions).toEqual([
` await page.goto('about:blank');`,
` await page.getByRole('button', { name: 'Submit' }).click();`,
]);

// Stop recording, like "Record at cursor" toggle off.
await backend.setRecorderMode({ mode: 'none' }, undefined);

// Session 2 on the same page: "Record at cursor" again, record another click.
events.length = 0;
await backend.setRecorderMode({ mode: 'recording' }, undefined);
await page.setContent('<button>Other</button>');
await page.getByRole('button', { name: 'Other' }).click();

// New session starts from scratch: previous session's actions must not be
// re-sent, otherwise the client re-inserts the stale last action into the editor.
await expect.poll(() => events[events.length - 1]?.actions).toEqual([
` await page.getByRole('button', { name: 'Other' }).click();`,
]);
});

test('should reset routes before reuse', async ({ server, connectedBrowserFactory }) => {
const browser1 = await connectedBrowserFactory();
const context1 = await browser1.newContextForReuse();
Expand Down
Loading