[BUG] Can't connect to running browserServer from another process
Context:
- Playwright Version: 1.5.1
- Operating System: Mac
- Node.js version: 14.1.0
- Browser: Chromium
- Extra:
Code Snippet
run-server.js
//Adapted from https://playwright.dev/#version=v1.6.1&path=docs%2Fapi.md&q=browsertypelaunchserveroptions
const { chromium } = require('playwright'); // Or 'webkit' or 'firefox'.
(async () => {
const browserServer = await chromium.launchServer();
const wsEndpoint = browserServer.wsEndpoint();
console.log(wsEndpoint);
// Use web socket endpoint later to establish a connection.
const browser = await chromium.connect({ wsEndpoint });
const context = await browser.newContext();
console.log("Number of contexts currently running in this browser: " + browser.contexts().length);
// Close browser instance.
// await browserServer.close();
})();
connect-to-server.js
const { chromium } = require('playwright'); // Or 'webkit' or 'firefox'.
(async () => {
let wsEndpoint = "##### COPY/PASTE WS ENDPOINT HERE #####";
let browser = await chromium.connect({ wsEndpoint });
let contexts = browser.contexts();
console.log("Number of contexts currently running in this browser: " + browser.contexts().length);
})();
Describe the bug
I can't connect to contexts created with browserServer in another Playwright process. From Issue 1985, I understand that we can't connect to "vanilla" WS endpoints created by Chromium. However, I was under the impression that the browserServer would enable us to use a "Playwright wsEndpoint" to connect to the running browserServer and access its contexts in a cross-process scenario. If not, then what's the benefit of using browserServer? (I suppose using it might reduce resource requirements since only one Chromium is running...)
Rinogo:scraper-test rinogo$ node run-server.js
ws://127.0.0.1:65406/1d00cf042a66e1a5c0524457c5ba5e61
Number of contexts currently running in this browser: 1
^Z
[1]+ Stopped node run-server.js
Rinogo:scraper-test rinogo$ bg
[1]+ node run-server.js &
Rinogo:scraper-test rich$ node connect-to-server.js
Number of contexts currently running in this browser: 0 #(Editorial note: Expecting this to be 1)
^C
Rinogo:scraper-test rinogo$
From reading the code, it looks like browser.contexts() just returns the contexts created by that particular client. So, I tried to extend Playwright's functionality to return all contexts created on the server.
This is as far as I got: https://github.com/rinogo/playwright/commit/46d4816f625d881da7d8f44b5c414ec556ecce89
Is there any chance of this working? Or, are there architectural reasons that prevent this from being feasible?
Maybe the best approach is to wait for this? https://github.com/microsoft/playwright/pull/4344
The idea behind browser server is that independent clients can reuse the same browser instance while remaining isolated from each other. There are no supported scenarios where two playwright clients would control the same context or page. Can I ask what your use case is?
Hi, @pavelfeldman! Thanks for your response.
In short, I'm trying to find a way to share "session" information (cookies, Local Storage, (potentially) cache, etc.) between multiple simultaneously-executing Playwright instances.
I can use browserType.launchPersistentContext(userDataDir) to persist session information in the "usual" Chromium fashion. After a Playwright script executes, I can run another and it will "inherit" the session used by the previous instance.
However, I have trouble when running more than one Playwright instance simultaneously. The first Playwright instance executes as expected. When I start a second Playwright instance that uses the same userDataDir, a new tab opens in the original browser (Yes!), but I then immediately receive the following error (Doh):
browserType.launchPersistentContext: Protocol error (Browser.getVersion): Target closed.
This error message didn't point me in any particular direction, so I gave up.
When I happened upon BrowserServer, it seemed like it might be the solution to my problem. I could open a single "server" and leave it running. Then, I could connect to it from any number of scripts and share the same "session" between them. Even if the "session" was technically Incognito, my hope was that it would be shared among the various scripts. Unfortunately, I haven't found a way to share this "session" between different pages opened via BrowserServer in different scripts.
So, I thought it might be possible to acquire the contexts from the BrowserServer using browser.contexts(). However, it appears that only contexts opened by the current script are accessible.
Then, as a last-ditch effort, I was trying to implement a way to access and use the contexts that exist within the server instead of the client.
(Phew! Lots of background. Hopefully that made sense.)
Ultimately, all I'm trying to do is to share "session" information between different simultaneously running instances of Playwright. It doesn't matter to me how this session information is shared - persisting the data in the userDataDir works just as well as a shared Incognito session, or even a Playwright-assisted solution (manually transferring cookies, etc.) The most natural, "out of the box" solution to me seems to be allowing multiple instances to use browserType.launchPersistentContext(userDataDir) simultaneously. The "target closed" error prevents me from doing so, and I'm not experienced enough with Playwright to know if/how this error could be fixed.
Thank you for any insight you can offer! I'm loving Playwright and am impressed with the responsiveness of your team. :)
Potentially related, 1.7 will have browserContext.storageState and ability to create new context with this stored state.
Ultimately, all I'm trying to do is to share "session" information between different simultaneously running instances of Playwright.
That's not currently possible - Playwright is opinionated when it comes to setting up contexts and each instance would think it 'owns' the context. There is no Playwright-light mode where Playwright connects to the already running page that is owned by other Playwright or a standalone browser. But I'm curious what you use case is and why you need two instances of Playwright for that, are they running on different machines?
Cool. browserContext.storageState() might help. Even something as simple as a combination of browserContext.cookies and browserContext.addCookies() might be enough to achieve 80% of what we're trying to do.
But I'm curious what you use case is and why you need two instances of Playwright for that, are they running on different machines?
Nope - Same machine - We're simply trying to reduce execution time by running scripts in parallel instead of series.
If state were not an issue, then Playwright's existing functionality would be sufficient to accomplish this. However, I haven't been able to find a way to run scripts simultaneously with shared state being a requirement.
Of course, there is always another solution - Instead of maintaining a group of independent scripts, we could rearchitect our codebase to have a "master script" (so to speak) that incorporates and oversees all of the simultaneously-running "worker scripts". While I believe this would be a functional solution, it complicates things.
Before pursuing this option, I'll look into the various functions on browserContext. It looks like browserContext.storageState() is a "getter" only - we can use browserContext.addCookies() to add the cookies - is anything similar in the works for LocalStorage? If not, I'm assuming we could use something like browserContext.addInitScript() to initialize the LocalStorage, correct?
P.S. Just re-read your message about how 1.7 will also have the "ability to create new context with this stored state." I was able to track that down at browser.newContext({storageState}). Thanks!
@pavelfeldman I went ahead and put together a demo of this new functionality. Seems to be working so far. I published my demo to "give back" a bit to the community.
https://github.com/rinogo/playwright-state-example
Thanks for pointing me in the right direction!
The idea behind browser server is that independent clients can reuse the same browser instance while remaining isolated from each other. There are no supported scenarios where two playwright clients would control the same context or page. Can I ask what your use case is?
We have a drivers logic which is stateless multiple instances, wish to access back the same context for logged in state + multiple steps web form.