Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BrowserType.connect websocket endpoint not working #396

Closed
Zanis7 opened this issue Apr 14, 2021 · 5 comments
Closed

BrowserType.connect websocket endpoint not working #396

Zanis7 opened this issue Apr 14, 2021 · 5 comments

Comments

@Zanis7
Copy link

Zanis7 commented Apr 14, 2021

Connecting to existing chrome not working. BrowserType.launch() works fine

Sample code to reproduce the issue:

package com.test;

import com.microsoft.playwright.*;

public class GoogleSearch {
	public static void main(String[] args) {
		try (Playwright playwright = Playwright.create()) {
			BrowserType browserType = playwright.chromium();
			Browser browser = browserType
					.connect("ws://127.0.0.1:9002/devtools/browser/1109fd4e-b326-4b8d-bed6-61a76d7bd11c");
			Page page = browser.newPage();
			page.navigate("http://google.com");
			System.out.println(page.title());
		}
	}
}

getting the following exception

Exception in thread "main" com.microsoft.playwright.PlaywrightException: Playwright connection closed
	at com.microsoft.playwright.impl.WebSocketTransport.checkIfClosed(WebSocketTransport.java:116)
	at com.microsoft.playwright.impl.WebSocketTransport.poll(WebSocketTransport.java:84)
	at com.microsoft.playwright.impl.Connection.processOneMessage(Connection.java:183)
	at com.microsoft.playwright.impl.Connection.waitForObjectWithKnownName(Connection.java:162)
	at com.microsoft.playwright.impl.BrowserTypeImpl.connectImpl(BrowserTypeImpl.java:66)
	at com.microsoft.playwright.impl.BrowserTypeImpl.lambda$connect$1(BrowserTypeImpl.java:55)
	at com.microsoft.playwright.impl.LoggingSupport.withLogging(LoggingSupport.java:47)
	at com.microsoft.playwright.impl.BrowserTypeImpl.connect(BrowserTypeImpl.java:55)
	at com.microsoft.playwright.BrowserType.connect(BrowserType.java:559)
	at com.test.GoogleSearch.main(GoogleSearch.java:9)

Also "Playwright.create()" always take time

@Zanis7
Copy link
Author

Zanis7 commented Apr 14, 2021

Found the reason for connection issue. It is because I used the chrome devtools websocket endpoint instead of playwright websocket server endpoint . In node js, there is an api to start the server and get the websocket endpoint , is there any available in java.

From the docs

const { chromium } = require('playwright');  // Or 'webkit' or 'firefox'.

(async () => {
  const browserServer = await chromium.launchServer();
  const wsEndpoint = browserServer.wsEndpoint();
  // Use web socket endpoint later to establish a connection.
  const browser = await chromium.connect({ wsEndpoint });
  // Close browser instance.
  await browserServer.close();
})();

My use case is to run chrome in container and connect using websocket endpoint

@yury-s
Copy link
Member

yury-s commented Apr 14, 2021

There's connectOverCDP if you need to connect to chromium browser via devtools websocket (the api is only available in 1.11.0-SNAPSHOT at the moment).

As for connect you need to start the server separately, you can use npx playwright run-server webkit to start the server from command line. There's no launchServer api in java.

@yury-s yury-s closed this as completed Apr 14, 2021
@hlr1983
Copy link

hlr1983 commented Apr 28, 2021

There's connectOverCDP if you need to connect to chromium browser via devtools websocket (the api is only available in 1.11.0-SNAPSHOT at the moment).

As for connect you need to start the server separately, you can use npx playwright run-server webkit to start the server from command line. There's no launchServer api in java.

Is there a plan for a launchServer api in java or a BrowserServer class in java?

Thanks.

@yury-s
Copy link
Member

yury-s commented Apr 28, 2021

No, we were not planning to add launchServer and BrowserServer to java. We assume that is should be sufficient to have it in node.js and allow to launch it from command line with npx playwright run-server chromium (not released yet). There is also an alternative approach to remote connection that we are considering: microsoft/playwright#4687

@manishmnit01
Copy link

manishmnit01 commented Aug 9, 2023

@Zanis7 @yury-s I have the same requirement where I need to create a browser instance in the container and connect using web socket endpoint from the client side. How do we achieve this without having the launch server API in the playwright java library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants