How to Run a Playwright Test on an Already-Open Browser Session
You can run a Playwright test on an already-open browser session by connecting Playwright to an existing Chrome browser using the Chrome DevTools Protocol (CDP).
This allows you to reuse the existing browser session, including:
- Existing login session
- Cookies and authentication
- Open browser tabs
- Local storage and session storage
- Existing browser state
1. Start Chrome with Remote Debugging Enabled
First, close all Chrome instances.
Windows (Command Prompt)
chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\chrome-debug-profile"
macOS
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
--remote-debugging-port=9222 \
--user-data-dir="/tmp/chrome-debug-profile"
Linux
google-chrome \
--remote-debugging-port=9222 \
--user-data-dir="/tmp/chrome-debug-profile"
Chrome will open with remote debugging enabled. You can also log in to your application manually.
2. Connect Playwright to the Existing Chrome Session
Install Playwright
npm install -D @playwright/test
Create tests/existing-browser.spec.ts
import { test, expect, chromium } from '@playwright/test';
test('Run test on existing Chrome session', async () => {
// Connect to existing Chrome
const browser = await chromium.connectOverCDP(
'http://localhost:9222'
);
// Get existing browser context
const context = browser.contexts()[0];
// Get already-open tabs
const pages = context.pages();
// Select the existing tab
const page = pages[0];
console.log('Current URL:', page.url());
// Perform automation
await page.goto('https://example.com');
console.log('Page title:', await page.title());
await expect(page).toHaveTitle(/Example Domain/);
// Disconnect from the existing browser
await browser.close();
});
3. Run the Playwright Test
npx playwright test tests/existing-browser.spec.ts
4. Work with Multiple Existing Tabs
You can access all currently open tabs in the existing Chrome browser.
const browser = await chromium.connectOverCDP(
'http://localhost:9222'
);
const context = browser.contexts()[0];
const pages = context.pages();
for (const page of pages) {
console.log('Tab URL:', page.url());
console.log('Tab Title:', await page.title());
}
Access a Specific Tab
const page = pages.find(
p => p.url().includes('dashboard')
);
if (!page) {
throw new Error('Dashboard tab not found');
}
await page.locator('#username').fill('sumit');
5. Reuse an Existing Login Session
Suppose you have already logged into an application manually. You can use the same authenticated browser context without performing the login again.
const browser = await chromium.connectOverCDP(
'http://localhost:9222'
);
const context = browser.contexts()[0];
const page = context.pages()[0];
// Existing login session
console.log(await page.url());
// Access authenticated application
await page.goto('https://example.com/dashboard');
await expect(
page.locator('h1')
).toContainText('Dashboard');
// Disconnect from the browser
await browser.close();
You do not need to perform the login again if the existing browser context has a valid authenticated session.
6. Important Playwright Methods
| Method | Description |
|---|---|
chromium.connectOverCDP() |
Connect to an existing Chrome browser. |
browser.contexts() |
Get existing browser contexts. |
context.pages() |
Get existing browser tabs. |
page.url() |
Get the current tab URL. |
page.title() |
Get the current page title. |
page.goto() |
Navigate the existing tab. |
browser.close() |
Close the CDP-connected browser connection. It does not normally terminate the external Chrome process. |
7. Important Limitations
connectOverCDP()is Chromium-specific.- CDP connection is lower fidelity than Playwright's native browser protocol connection.
- You must start Chrome with remote debugging enabled.
- Existing browser sessions cannot generally be attached to if Chrome was started without remote debugging.
- Do not expose port 9222 to untrusted networks because it provides powerful browser access.
8. Recommended Architecture
Existing Chrome Browser
|
| Manual Login
| Cookies
| Open Tabs
|
v
CDP Connection (Port 9222)
|
v
Playwright Automation
|
| TypeScript
| Locators
| Assertions
| Tests
|
v
Existing Authenticated Application
|
v
Reuse Session Without Logging In Again
9. Frequently Asked Questions
Q1. Can Playwright connect to an already-open Chrome browser?
Yes. Playwright can connect to an existing Chrome browser using
chromium.connectOverCDP(), provided Chrome was started
with remote debugging enabled.
Q2. Can I reuse existing cookies?
Yes. Playwright can access cookies from the connected browser context
using context.cookies().
Q3. Can I access multiple open tabs?
Yes. Use context.pages() to retrieve all open tabs.
Q4. Do I need to log in again?
No, provided the existing browser context has a valid authenticated session.
Q5. Can I connect to Chrome without remote debugging?
Generally, no. Chrome must be launched with remote debugging enabled before Playwright can connect through CDP.
Q6. Does browser.close() close my existing Chrome browser?
For a CDP connection, Playwright closes the connection rather than normally terminating the externally launched Chrome process. Verify this behavior with your environment.

No comments:
Post a Comment