question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Is it possible to customize both user-data-dir and websocket port

See original GitHub issue

Use case: The goal is to launch a browser on server that’s always up. I want to ensure that I can sign in to that browser and it will retain my credentials. Even after I restart my VM, the browser will retain all the user preferences, cookies etc. The way to achieve this would be to use both wsEndpoint and userDataDir.

From what I have understood:

  • use launchPersistentContext to launch with userDataDir but it doesn’t allow wsEndpoint
  • use launchServer to launch with wsEndpoint but it doesn’t allow userDataDir

I tried using websocket port with launchPersistentContext but the code crashes with following error log of timeout:

TimeoutError: Timeout 10000ms exceeded during browserType.launchPersistentContext.
[1] app: [browser] <launched> pid=75403
[1] app: [browser] 
[1] app: [browser] DevTools listening on ws://127.0.0.1:45555/devtools/browser/f44a1f4d-ce74-4965-b8e3-bb0546bd46d2
[1] app: [browser] [75403:50179:0716/205303.955435:ERROR:ssl_client_socket_impl.cc(959)] handshake failed; returned -1, SSL error code 1, net_error -202

Is there a way to achieve my use case with Playwright?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
VikramTiwaricommented, Aug 13, 2020

After some more digging through the code I found this env variable PLAYWRIGHT_CHROMIUM_DEBUG_PORT. I can use it to set a port during chromium.launchPersistentContext and the browser does launch with websocket port as well as userDataDirectory.

// PLAYWRIGHT_CHROMIUM_DEBUG_PORT=12345 parent.js
const { chromium } = require("playwright");

(async () => {
	const context = await chromium.launchPersistentContext(`./udd`, {
		headless: false,
	});
	const page = await context.newPage();
	await page.goto("https://google.com");
})();

At this point, I can use the webSocketDebuggerUrl from localhost:12345/json/version to connect to the browser. But from this browser, I can’t get the existing contexts and can only create new incognito contexts.

// node child.js
const { chromium } = require("playwright");

(async () => {
	const browser = await chromium.connect({
		// replace with webSocketDebuggerUrl from localhost:12345/json/version
		wsEndpoint: `ws://localhost:12345/devtools/browser/489bad93-902d-4f2d-b8ad-0be5868d9313`,
	});
	console.log("browser", browser); // CRBrowser instance
	const contexts = browser.contexts();
	console.log("contexts", contexts); // []
})();

This defeats the use case that I am trying to achieve.

I have time to contribute a fix but I don’t fully understand why browser server instances don’t have access to the pre-existing contexts and why are they limited to just incognito contexts. These seems like design decisions and I am sure they are valid ones.

I am hoping that we can add another way to connect. Something like chromium.launchPersistentContextServer which:

  • enables usage of user’s data directory
  • connects using websocket port
  • allows new connections through websocket port/URL
  • can utilize both normal and incognito contexts
  • can see and use existing contexts for the connected browser

Any pointers on where to look for design docs or how to achieve this would be really helpful and much appreciated. Thanks folks!

1reaction
pavelfeldmancommented, Mar 30, 2021

You can now pass --remote-debugging-port as an argument and use connectOverCDP.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can WebSockets and HTTP server both run on the SAME port ...
To provide this question with an answer: Yes, they can. See also: Is it possible to enable tcp, http and websocket all using...
Read more >
Rufaydium WebDriver 1.7.2 (no selenium/websocket) - Page 18
This works if I have no chrome window open. However if there are other Chrome windows open (not created by chromedriver.exe) it stops ......
Read more >
Puppeteer documentation - DevDocs
Puppeteer 7.1.0 API documentation with instant search, offline support, keyboard shortcuts, mobile version, and more.
Read more >
Writing WebSocket servers - Web APIs | MDN
A WebSocket server is nothing more than an application listening on any port of a TCP server that follows a specific protocol.
Read more >
WebSockets support in ASP.NET Core - Microsoft Learn
The default is two minutes. AllowedOrigins - A list of allowed Origin header values for WebSocket requests. By default, all origins are allowed....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found