When Playwright attaches to Chrome over CDP, it can see the browser's existing context. A script that opens a new tab there inherits that context's first-party storage for the origin. That can help a task continue in the right signed-in tab. It can also make a test for signed-out behavior pass through screens it should have tested.
An AI assistant helped run the experiment and prepare this write-up. We used a Cloudbrowser Chrome session and two pages on our own documentation site.
Put a marker in one context
This experiment uses a single throwaway localStorage key. Use an endpoint you control and two public pages on the same origin. Set CHROME_CDP_URL, TEST_PAGE, and SAME_ORIGIN_PAGE in your shell. Keep the CDP endpoint private and avoid logging it, since a hosted endpoint may carry a bearer credential.
import { randomUUID } from 'node:crypto';
import { chromium } from 'playwright';
const { CHROME_CDP_URL, TEST_PAGE, SAME_ORIGIN_PAGE } = process.env;
if (!CHROME_CDP_URL || !TEST_PAGE || !SAME_ORIGIN_PAGE) {
throw new Error('Set CHROME_CDP_URL, TEST_PAGE, and SAME_ORIGIN_PAGE.');
}
if (new URL(TEST_PAGE).origin !== new URL(SAME_ORIGIN_PAGE).origin) {
throw new Error('Use two pages on the same origin.');
}
const browser = await chromium.connectOverCDP(CHROME_CDP_URL);
const [existing] = browser.contexts();
const key = `context-check-${randomUUID()}`;
let writer, sibling, fresh;
try {
writer = await existing.newPage();
await writer.goto(TEST_PAGE);
await writer.evaluate(key => localStorage.setItem(key, 'present'), key);
sibling = await existing.newPage();
await sibling.goto(SAME_ORIGIN_PAGE);
console.log('Existing context:', await sibling.evaluate(key => localStorage.getItem(key), key));
fresh = await browser.newContext();
const cleanPage = await fresh.newPage();
await cleanPage.goto(SAME_ORIGIN_PAGE);
console.log('Fresh context:', await cleanPage.evaluate(key => localStorage.getItem(key), key));
} finally {
await writer?.evaluate(key => localStorage.removeItem(key), key).catch(() => {});
await sibling?.close().catch(() => {});
await writer?.close().catch(() => {});
await fresh?.close().catch(() => {});
await browser.close();
}
The second tab in the existing context printed present. The tab in the fresh context printed null. Both reads opened URLs on the same origin. The result measures first-party localStorage in one Chromium run. It leaves cookies, partitioned third-party storage, and other browser engines untested.
Match the context to the task
- To continue an existing flow, start by finding the expected tab and checking its origin and a page-specific landmark. Relying on the first open tab can send automation to an unrelated page.
- To test a fresh visit, open a new context explicitly. Open your first page in that context and close the context after the test.
- If parallel tests use the same remote account, separate browser contexts still send requests to the same service. Give write tests separate application records or accounts when their changes would collide.
The Cloudbrowser team has a separate explanation of the profile data a remote browser can retain between tasks and when a site may ask users to sign in again. How persistent browser profiles retain session state
Top comments (0)