Skip to content

Commit

Permalink
fix: firefox 默认协议是 Webdriver-bidi,chrome 默认协议是 CDP
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyong920 committed Dec 20, 2024
1 parent 00e0237 commit 444589d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/ruiyun/jvppeteer/cdp/core/Puppeteer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ruiyun.jvppeteer.cdp.core;

import com.ruiyun.jvppeteer.api.core.Browser;
import com.ruiyun.jvppeteer.cdp.entities.Protocol;
import com.ruiyun.jvppeteer.common.BrowserRevision;
import com.ruiyun.jvppeteer.common.Environment;
import com.ruiyun.jvppeteer.common.Product;
Expand All @@ -15,6 +16,7 @@
import com.ruiyun.jvppeteer.util.StringUtil;
import com.ruiyun.jvppeteer.util.ValidateUtil;
import java.io.IOException;
import java.util.Objects;


import static com.ruiyun.jvppeteer.common.Constant.PRODUCT_ENV;
Expand Down Expand Up @@ -79,6 +81,14 @@ public static Browser rawLaunch(LaunchOptions options, Puppeteer puppeteer) thro
if (StringUtil.isNotBlank(options.getCacheDir())) {
puppeteer.setCacheDir(options.getCacheDir());
}
//默认协议,chrome -> CDP ,firefox -> WebDriverBiDi
if (Objects.isNull(options.getProtocol())) {
if (Objects.equals(options.getProduct(), Product.Firefox)) {
options.setProtocol(Protocol.WebDriverBiDi);
} else {
options.setProtocol(Protocol.CDP);
}
}
adoptLauncher(puppeteer);
return puppeteer.getLauncher().launch(options);
}
Expand Down
37 changes: 14 additions & 23 deletions src/main/java/com/ruiyun/jvppeteer/launch/BrowserLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,20 @@ protected Browser createBrowser(LaunchOptions options, List<String> chromeArgume
if (usePipe) {
throw new LaunchException("Not supported pipe connect to browser");
} else {
if (Objects.equals(options.getProduct(), Product.Firefox)) {
if (Protocol.CDP.equals(options.getProtocol())) {
runner.start();
String endpoint = this.waitForWSEndpoint(options.getTimeout(), options.getDumpio(), options.getProtocol(), runner.getProcess());
if (Protocol.WebDriverBiDi.equals(options.getProtocol())) {
ConnectionTransport transport = WebSocketTransportFactory.create(endpoint);
connection = new CdpConnection(endpoint, transport, options.getSlowMo(), options.getProtocolTimeout());
runner.setConnection(connection);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Connect to browser by websocket url: {}", endpoint);
}
return createCdpBrowser(options, defaultArgs, runner, connection);
} else {
if (Objects.equals(options.getProduct(), Product.Firefox)) {
runner.start();
String endpoint = this.waitForWSEndpoint(options.getTimeout(), options.getDumpio(), options.getProtocol(), runner.getProcess());
ConnectionTransport transport = WebSocketTransportFactory.create(endpoint + "/session");
connection = new BidiConnection(endpoint + "/session", transport, options.getSlowMo(), options.getProtocolTimeout());
runner.setConnection(connection);
Expand All @@ -169,27 +179,7 @@ protected Browser createBrowser(LaunchOptions options, List<String> chromeArgume
Runnable closeCallback = runner::closeBrowser;
return createBiDiBrowser((BidiConnection) connection, closeCallback, runner.getProcess(), options);
} else {
ConnectionTransport transport = WebSocketTransportFactory.create(endpoint);
connection = new CdpConnection(endpoint, transport, options.getSlowMo(), options.getProtocolTimeout());
runner.setConnection(connection);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Connect to browser by websocket url: {}", endpoint);
}
return createCdpBrowser(options, defaultArgs, runner, connection);
}
} else {
if (Objects.equals(options.getProtocol(), Protocol.CDP)) {
runner.start();
String endpoint = this.waitForWSEndpoint(options.getTimeout(), options.getDumpio(), options.getProtocol(), runner.getProcess());
ConnectionTransport transport = WebSocketTransportFactory.create(endpoint);
connection = new CdpConnection(endpoint, transport, options.getSlowMo(), options.getProtocolTimeout());
runner.setConnection(connection);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Connect to browser by websocket url: {}", endpoint);
}
return createCdpBrowser(options, defaultArgs, runner, connection);
} else {
throw new LaunchException("Chrome dose not support connect to browser by webdriver-bidi");
throw new LaunchException("Chrome dont not support protocol: " + options.getProtocol() + " yet");
}
}
}
Expand All @@ -198,6 +188,7 @@ protected Browser createBrowser(LaunchOptions options, List<String> chromeArgume
throw new LaunchException("Failed to launch the browser process: " + e.getMessage(), e);
}
}

/**
* waiting for browser ws url
*
Expand Down

0 comments on commit 444589d

Please sign in to comment.